Mailinglist Archive: yast-commit (2114 mails)

< Previous Next >
[yast-commit] <rest-service> master : make better interface paths (/resources/org-opensuse-yast-commandlines.xml)
  • From: Martin Kudlvasr <mkudlvasr@xxxxxxx>
  • Date: Wed, 16 Sep 2009 14:59:32 +0200
  • Message-id: <E1MqjuC-0002ts-K7@xxxxxxxxxxxxxxxx>
ref: refs/heads/master
commit 6e909f219a50d5b3338357ce39bddefec166fd52
Author: Martin Kudlvasr <mkudlvasr@xxxxxxx>
Date: Wed Sep 16 14:59:32 2009 +0200

make better interface paths (/resources/org-opensuse-yast-commandlines.xml)
make better resource xml structure (resources => resource =>
implementations => implementation)
---
webservice/app/controllers/resources_controller.rb | 17 +++++---
webservice/app/models/resource.rb | 47 ++++++++++++-------
webservice/app/views/resources/index.html.erb | 4 +-
webservice/app/views/resources/show.html.erb | 2 +-
webservice/config/routes.rb | 3 +-
5 files changed, 45 insertions(+), 28 deletions(-)

diff --git a/webservice/app/controllers/resources_controller.rb
b/webservice/app/controllers/resources_controller.rb
index ae90e04..8cdf227 100644
--- a/webservice/app/controllers/resources_controller.rb
+++ b/webservice/app/controllers/resources_controller.rb
@@ -3,10 +3,6 @@ class ResourcesController < ApplicationController

def index
@resources = Resource.all
- do_respond
- end
-
- def do_respond
@node = "Yast"

respond_to do |format|
@@ -16,8 +12,17 @@ class ResourcesController < ApplicationController
end
end

+ def do_respond
+ end
+
def show
- @resources = Resource.find(params[:id])
- do_respond
+ @resource = Resource.find(params[:id].tr('-','.'))
+ @node = "Yast"
+
+ respond_to do |format|
+ format.html
+ format.xml { render :xml => @resource.to_xml }
+ format.json{ render :json=> @resource.to_json}
+ end
end
end
diff --git a/webservice/app/models/resource.rb
b/webservice/app/models/resource.rb
index 7b4f37b..ae9f028 100644
--- a/webservice/app/models/resource.rb
+++ b/webservice/app/models/resource.rb
@@ -4,7 +4,7 @@

class Resource
require 'resource_registration'
- attr_accessor :implementations
+ attr_accessor :implementations, :interface

class Implementation
attr_accessor :controller, :interface
@@ -27,9 +27,10 @@ class Resource
@singular ? :show : :index
end

- def to_xml(xml_builder = nil)
- xml = xml_builder || Builder::XmlMarkup.new
- xml.resource do
+ def to_xml( options = {} )
+ xml = options[:builder] ||= Builder::XmlMarkup.new(options)
+ xml.instruct! unless options[:skip_instruct]
+ xml.implementation do
xml.interface(@interface)
xml.policy(@policy)
xml.singular(@singular, :type => :boolean)
@@ -43,17 +44,13 @@ class Resource
end

def initialize (interface, implementations)
+ @interface = interface
@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
+ resources = ResourceRegistration.resources.sort.collect
{|interface,implementations| new(interface,implementations)}
+ Resources.new resources
end

def self.find(interface)
@@ -61,12 +58,15 @@ class Resource
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)
+ def to_xml( options = {} )
+ xml = options[:builder] ||= Builder::XmlMarkup.new(options)
+ xml.instruct! unless options[:skip_instruct]
+ xml.resource do
+ xml.interface(@interface)
+ xml.implementations(:type => :array) do
+ @implementations.each do |implementation|
+ implementation.to_xml(:builder => xml, :skip_instruct => true)
+ end
end
end
end
@@ -74,5 +74,18 @@ class Resource
def to_json( options = {} )
Hash.from_xml(to_xml).to_json
end
+end

+class Resources < Array
+ def to_xml( options = {} )
+ xml = options[:builder] ||= Builder::XmlMarkup.new(options)
+ xml.instruct! unless options[:skip_instruct]
+ xml.resources(:type => :array) do
+ each {|resource| resource.to_xml(:builder => xml, :skip_instruct =>
true)}
+ end
+ end
+
+ def to_json
+ 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 e8b8e32..0c4328e 100644
--- a/webservice/app/views/resources/index.html.erb
+++ b/webservice/app/views/resources/index.html.erb
@@ -1,4 +1,4 @@
<h1><%= @node -%> resources</h1>
-<% @resources.implementations.each do |impl| -%>
+<% @resources.each do |rsrc| rsrc.implementations.each do |impl| -%>
<%= link_to impl.interface, :only_path => :true, :controller =>
impl.controller, :action => impl.action -%><br/>
-<% end -%>
+<% end end -%>
diff --git a/webservice/app/views/resources/show.html.erb
b/webservice/app/views/resources/show.html.erb
index e8b8e32..e38c18e 100644
--- a/webservice/app/views/resources/show.html.erb
+++ b/webservice/app/views/resources/show.html.erb
@@ -1,4 +1,4 @@
<h1><%= @node -%> resources</h1>
-<% @resources.implementations.each do |impl| -%>
+<% @resource.implementations.each do |impl| -%>
<%= link_to impl.interface, :only_path => :true, :controller =>
impl.controller, :action => impl.action -%><br/>
<% end -%>
diff --git a/webservice/config/routes.rb b/webservice/config/routes.rb
index 03573e6..fe04edc 100644
--- a/webservice/config/routes.rb
+++ b/webservice/config/routes.rb
@@ -1,8 +1,7 @@
# route sessions statically, it is a singleton controller
ActionController::Routing::Routes.draw do |map|
map.resource :session
- map.resources :resources, :requirements => { :id => /[\w.]+/ },
- :member => { :show => :get }
+ map.resources :resources, :requirements => { :id => /[-\w]+/ }
map.resources :permissions, :requirements => {:id => /.*/}
map.resources :vendor_settings

--
To unsubscribe, e-mail: yast-commit+unsubscribe@xxxxxxxxxxxx
For additional commands, e-mail: yast-commit+help@xxxxxxxxxxxx

< Previous Next >
This Thread
  • No further messages