module StudioApi::StudioResource

Adds ability to ActiveResource::Base (short as ARes) to easy set connection to studio in dynamic way, which is not so easy as ARes is designed for static values. Also modify a few expectation of ActiveResource to fit studio API ( like missing xml suffix in calls ).

@example Add new Studio Resource

# enclose it in module allows to automatic settings with Util
module StudioApi
  class NewCoolResource < ActiveResource::Base
    extend StudioResource
  end
end

Public Class Methods

extended(base) click to toggle source

hooks when module extend and ActiveResource based class @param (ActiveResource::Base) extended class

   # File lib/studio_api/studio_resource.rb
31 def self.extended(base)
32   base.format = :xml #fix ARes 3.1 default ( json )
33   # ensure that dasherize is not called as studio use in some keys '-'
34   # need to extend it after inclusion
35   base.class_eval do
36     alias_method :original_encode, :encode
37     def encode(options={})
38       options[:dasherize] = false
39       original_encode options
40     end
41   end
42 end

Public Instance Methods

collection_path(prefix_options = {}, query_options = nil) click to toggle source

We need to overwrite the paths methods because susestudio doesn't use the standard .xml filename extension which is expected by ActiveResource.

   # File lib/studio_api/studio_resource.rb
82 def collection_path(prefix_options = {}, query_options = nil)
83   inspect_connection
84   prefix_options, query_options = split_options(prefix_options) if query_options.nil?
85   "#{prefix(prefix_options)}#{collection_name}#{query_string(query_options)}"
86 end
element_path(id, prefix_options = {}, query_options = nil) click to toggle source

We need to overwrite the paths methods because susestudio doesn't use the standard .xml filename extension which is expected by ActiveResource.

   # File lib/studio_api/studio_resource.rb
74 def element_path(id, prefix_options = {}, query_options = nil)
75   inspect_connection
76   prefix_options, query_options = split_options(prefix_options) if query_options.nil?
77   "#{prefix(prefix_options)}#{collection_name}/#{id}#{query_string(query_options)}"
78 end
encode(options={}) click to toggle source
   # File lib/studio_api/studio_resource.rb
37 def encode(options={})
38   options[:dasherize] = false
39   original_encode options
40 end
studio_connection() click to toggle source

Gets studio connection. Mostly useful internally. @return (StudioApi::Connection,nil) object of studio connection or nil if not yet set

   # File lib/studio_api/studio_resource.rb
25 def studio_connection
26   @studio_connection
27 end
studio_connection=(connection) click to toggle source

Takes information from connection and sets it to ActiveResource::Base. Also take care properly of prefix as it need to join path from site with api prefix like appliance/:appliance_id . @param (StudioApi::Connection) connection source for connection in

activeResource

@return (StudioApi::Connection) unmodified parameter

   # File lib/studio_api/studio_resource.rb
50 def studio_connection= connection
51   self.site = connection.uri.to_s
52   # there is general problem, that when specified prefix in model, it doesn't
53   # contain uri.path as it is not know and uri is set during runtime, so we
54   # must add here manually adapt prefix otherwise site.path is ommitted in
55   # models which has own prefix in API
56   unless @original_prefix
57     if self.prefix_source == Util.join_relative_url(connection.uri.path,'/')
58       @original_prefix = "/"
59     else
60       @original_prefix = self.prefix_source
61     end
62   end
63   self.prefix = Util.join_relative_url connection.uri.path, @original_prefix
64   self.user = connection.user
65   self.password = connection.password
66   self.timeout = connection.timeout
67   self.proxy = connection.proxy.to_s if connection.proxy
68   self.ssl_options = connection.ssl
69   @studio_connection = connection
70 end

Private Instance Methods

inspect_connection() click to toggle source
   # File lib/studio_api/studio_resource.rb
90 def inspect_connection
91   unless @studio_connection
92     raise RuntimeError, 'Connection to Studio is not set
93     Try: StudioApi::Util.studio_connection = StudioApi::Connection.new username, api_key, api_uri'
94   end
95 end