Mailinglist Archive: yast-commit (2114 mails)

< Previous Next >
[yast-commit] <rest-service> master : remade resources to be more restfull
  • From: Martin Kudlvasr <mkudlvasr@xxxxxxx>
  • Date: Tue, 15 Sep 2009 15:00:34 +0200
  • Message-id: <E1MqjuB-0002sf-K3@xxxxxxxxxxxxxxxx>
ref: refs/heads/master
commit 3f74fd6967a9f70acfd9925d08afe032751a8082
Author: Martin Kudlvasr <mkudlvasr@xxxxxxx>
Date: Tue Sep 15 15:00:34 2009 +0200

remade resources to be more restfull
/resources
/resources.xml
/resources/interface
/resources/interface#show.xml
---
webservice/app/controllers/resources_controller.rb | 25 +++++--
webservice/app/models/resource.rb | 72 ++++++++++++++++++++
webservice/app/views/resources/index.html.erb | 12 +--
webservice/app/views/resources/index.xml.erb | 14 ----
webservice/app/views/resources/show.html.erb | 4 +
webservice/config/routes.rb | 3 +-
6 files changed, 98 insertions(+), 32 deletions(-)

diff --git a/webservice/app/controllers/resources_controller.rb
b/webservice/app/controllers/resources_controller.rb
index d419eb0..087010a 100644
--- a/webservice/app/controllers/resources_controller.rb
+++ b/webservice/app/controllers/resources_controller.rb
@@ -2,15 +2,24 @@ class ResourcesController < ApplicationController
require "resource_registration"

def index
- iface = params[:interface]
- if iface
- # return single resource if specific interface requested
- rsrc = ResourceRegistration.resources[iface]
- @resources = rsrc ? { iface => rsrc } : Hash.new
- else
- # return all known resources
- @resources = ResourceRegistration.resources
+ @resources = Resource.all
+ @node = "Yast"
+
+ logger.debug("Ahoj!")
+ debugger
+ respond_to do |format|
+ format.html
+ format.xml { render :xml => @resources.to_xml }
end
+ end
+
+ def show
+ @resources = Resource.find(params[:id])
@node = "Yast"
+
+ respond_to do |format|
+ format.html
+ format.xml { render :xml => @resources.to_xml }
+ end
end
end
diff --git a/webservice/app/models/resource.rb
b/webservice/app/models/resource.rb
new file mode 100644
index 0000000..8cd8b1e
--- /dev/null
+++ b/webservice/app/models/resource.rb
@@ -0,0 +1,72 @@
+#
+# Resource class
+#
+
+class Resource
+ require 'resource_registration'
+ attr_accessor :implementations
+
+ class Implementation
+ def initialize(interface, impl_hash)
+ @interface = interface
+ @policy = impl_hash[:policy]
+ @singular = impl_hash[:singular]
+ @controller= impl_hash[:controller]
+ end
+
+ def link_to
+ "/#{@controller}/#{@singular ? "show" : "index"}"
+ # url_for :only_path => :true,
+ # :controller => @controller,
+ # :action => (@singular ? :show : :index)
+ end
+
+ def to_xml(xml_builder = nil)
+ xml = xml_builder || Builder::XmlMarkup.new
+ xml.resource do
+ xml.interface(@interface)
+ xml.policy(@policy)
+ xml.singular (:type => :boolean) @singular.to_s
+ xml.href(link_to)
+ end
+ end
+
+ def to_json
+ Hash.from_xml(to_xml).to_json
+ end
+ end
+
+ def initialize (interface, implementations)
+ @implementations = (implementations or []).collect {|impl|
Implementation.new(interface, impl)}
+ end
+
+ def self.all
+ all_implementations = []
+ ResourceRegistration.resources.sort.each do |interface,implementations|
+ all_implementations += implementations.collect {|impl|
Implementation.new(interface, impl)}
+ end
+ all_resources = new(nil,[])
+ all_resources.implementations = all_implementations
+ all_resources
+ end
+
+ def self.find(interface)
+ implementations = ResourceRegistration.resources[interface] or []
+ new(interface, implementations)
+ end
+
+ def to_xml
+ xml = Builder::XmlMarkup.new
+ xml.instruct!
+ xml.resources(:type => :array) do
+ @implementations.each do |implementation|
+ implementation.to_xml(xml)
+ end
+ end
+ end
+
+ def to_json( options = {} )
+ Hash.from_xml(to_xml).to_json
+ end
+
+end
diff --git a/webservice/app/views/resources/index.html.erb
b/webservice/app/views/resources/index.html.erb
index 6c4ae0b..9bbe825 100644
--- a/webservice/app/views/resources/index.html.erb
+++ b/webservice/app/views/resources/index.html.erb
@@ -1,9 +1,5 @@
<h1><%= @node -%> resources</h1>
-<% @resources.keys.sort.each do |interface|
- implementations = @resources[interface]
- implementations.each do |implementation|
- action = (implementation[:singular] ? :show : :index)
- controller = implementation[:controller] -%>
- <%= link_to interface, :controller => controller, :action => action -%><br/>
-<% end
- end -%>
+<%= @resources.inspect -%>
+<% @resources.implementations do |implementation| -%>
+ <%= url_for :href => implementation.link_to -%><br/>
+<% end -%>
diff --git a/webservice/app/views/resources/index.xml.erb
b/webservice/app/views/resources/index.xml.erb
deleted file mode 100644
index b641709..0000000
--- a/webservice/app/views/resources/index.xml.erb
+++ /dev/null
@@ -1,14 +0,0 @@
-<resources type="array">
-<% @resources.each do |interface,implementations|
- implementations.each do |implementation|
- action = (implementation[:singular] ? :show : :index)
- controller = implementation[:controller] -%>
- <resource>
- <interface><%= interface %></interface>
- <policy><%= implementation[:policy] %></policy>
- <singular type="boolean"><%= implementation[:singular].to_s %></singular>
- <href><%= url_for :only_path => true, :controller => controller, :action
=> action -%></href>
- </resource>
-<% end
- end -%>
-</resources>
diff --git a/webservice/app/views/resources/show.html.erb
b/webservice/app/views/resources/show.html.erb
new file mode 100644
index 0000000..38e27f7
--- /dev/null
+++ b/webservice/app/views/resources/show.html.erb
@@ -0,0 +1,4 @@
+<h1><%= @node -%> resources</h1>
+<% @resources.implementations do |implementation| -%>
+ <%= url_for :href => implementation.link_to -%><br/>
+<% end -%>
diff --git a/webservice/config/routes.rb b/webservice/config/routes.rb
index be16af4..d5c1414 100644
--- a/webservice/config/routes.rb
+++ b/webservice/config/routes.rb
@@ -1,12 +1,11 @@
# route sessions statically, it is a singleton controller
ActionController::Routing::Routes.draw do |map|
map.resource :session
- map.resources :resources
+ map.resources :resources, :requirements => { :id => /[\w.]+/ }
map.resources :permissions, :requirements => {:id => /.*/}
map.resources :vendor_settings

# login uses POST for both
map.login "/login.:format", :controller => 'sessions', :action => 'create'
map.logout "/logout.:format", :controller => 'sessions', :action => 'destroy'
-
end
--
To unsubscribe, e-mail: yast-commit+unsubscribe@xxxxxxxxxxxx
For additional commands, e-mail: yast-commit+help@xxxxxxxxxxxx

< Previous Next >
This Thread
  • No further messages