Class: RightScaleSelfService::Api::Resource
- Inherits:
-
Object
- Object
- RightScaleSelfService::Api::Resource
- Defined in:
- lib/rightscale_selfservice/api/resource.rb
Instance Attribute Summary collapse
-
#name ⇒ Object
Returns the value of attribute name.
-
#service ⇒ Object
Returns the value of attribute service.
Instance Method Summary collapse
-
#initialize(name, service) ⇒ Resource
constructor
A new instance of Resource.
-
#method_missing(name, *args) ⇒ Object
Finds the specified action and executes it for this resource.
Constructor Details
#initialize(name, service) ⇒ Resource
Returns a new instance of Resource.
26 27 28 29 |
# File 'lib/rightscale_selfservice/api/resource.rb', line 26 def initialize(name, service) @name = name @service = service end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args) ⇒ Object
Finds the specified action and executes it for this resource
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/rightscale_selfservice/api/resource.rb', line 33 def method_missing(name, *args) actions_with_name = @service.client.interface["services"][@service.name][@service.version][@name]["controller"]["actions"].select{|a| a["name"] == name.to_s} actions = @service.client.interface["services"][@service.name][@service.version][@name]["controller"]["actions"].map{|a| a["name"]} unless actions_with_name.length > 0 raise "No action named \"#{name}\" was found on resource \"#{@name}\" in version \"#{@service.version}\" of service \"#{@service.name}\". Available actions are [#{actions.join(',')}]" end action = actions_with_name.first method = action["urls"].first.first.downcase.to_sym url = @service.base_url url += action["urls"].first.last tokens = RightScaleSelfService::Api::Client.get_known_account_id_tokens tokens.each do |token| url.gsub!(token, @service.client.account_id) end params = {:method => method, :url => url, :headers => {"X_API_VERSION" => @service.version}} if args.length > 0 args[0].each do |k,v| if url.include? ":#{k}" url.gsub!(":#{k}",v) args[0].delete(k) end end if args[0].length > 0 # Detect if a param is a file, using the same mechanism as # rest-client # # https://github.com/rest-client/rest-client/blob/master/lib/restclient/payload.rb#L33 if args[0].select{|k,v| v.respond_to?(:path) && v.respond_to?(:read) }.length > 0 params[:payload] = args[0] else params[:payload] = URI.encode_www_form(args[0]) args[0].each do |k,v| if v.is_a?(Array) params[:payload].gsub!("#{k}=","#{k}[]=") end end end end if method == :get && params.has_key?(:payload) params[:url] += "?#{params[:payload]}" params.delete(:payload) end end request = @service.client.(params) if args.length > 1 && args[1] request else response = request.execute end end |
Instance Attribute Details
#name ⇒ Object
Returns the value of attribute name.
23 24 25 |
# File 'lib/rightscale_selfservice/api/resource.rb', line 23 def name @name end |
#service ⇒ Object
Returns the value of attribute service.
24 25 26 |
# File 'lib/rightscale_selfservice/api/resource.rb', line 24 def service @service end |