Class: RightScaleSelfService::Api::Resource

Inherits:
Object
  • Object
show all
Defined in:
lib/rightscale_selfservice/api/resource.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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.
  tokens.each do |token|
    url.gsub!(token, @service.client.)
  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.get_authorized_rest_client_request(params)
  if args.length > 1 && args[1]
    request
  else
    response = request.execute
  end
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



23
24
25
# File 'lib/rightscale_selfservice/api/resource.rb', line 23

def name
  @name
end

#serviceObject

Returns the value of attribute service.



24
25
26
# File 'lib/rightscale_selfservice/api/resource.rb', line 24

def service
  @service
end