Class: Maestrano::API::Resource

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

Instance Attribute Summary

Attributes inherited from Object

#api_token

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Object

#[], #[]=, #_dump, _load, #as_json, construct_from, #each, #initialize, #inspect, #keys, #refresh_from, #respond_to?, #to_hash, #to_json, #to_s, #values

Methods included from Preset

included

Constructor Details

This class inherits a constructor from Maestrano::API::Object

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Maestrano::API::Object

Class Method Details

.class_nameObject



4
5
6
# File 'lib/maestrano/api/resource.rb', line 4

def self.class_name
  self.name.split('::').reject { |w| w.to_s == "Maestrano" }
end

.retrieve(id, api_token = nil) ⇒ Object



32
33
34
35
36
# File 'lib/maestrano/api/resource.rb', line 32

def self.retrieve(id, api_token=nil)
  instance = self.new(id, api_token)
  instance.refresh
  instance
end

.underscore(string_val) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/maestrano/api/resource.rb', line 38

def self.underscore(string_val)
  string_val.gsub(/::/, '/').
  gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
  gsub(/([a-z\d])([A-Z])/,'\1_\2').
  tr("-", "_").
  downcase
end

.urlObject



8
9
10
11
12
13
14
15
16
17
# File 'lib/maestrano/api/resource.rb', line 8

def self.url
  if self == Maestrano::API::Resource
    raise NotImplementedError.new('Maestrano::API::Resource is an abstract class.  You should perform actions on its subclasses (Bill, Customer, etc.)')
  end
  if class_name.is_a?(Array)
    class_name.map { |w| CGI.escape(self.underscore(w)) }.join("/") + 's'
  else
    "#{CGI.escape(self.underscore(class_name))}s"
  end
end

Instance Method Details

#refreshObject



26
27
28
29
30
# File 'lib/maestrano/api/resource.rb', line 26

def refresh
  response, api_token = Maestrano::API::Operation::Base.request(:get, url, @api_token, @retrieve_options)
  refresh_from(response, api_token)
  self
end

#urlObject



19
20
21
22
23
24
# File 'lib/maestrano/api/resource.rb', line 19

def url
  unless id = self['id']
    raise Maestrano::API::Error::InvalidRequestError.new("Could not determine which URL to request: #{self.class} instance has invalid ID: #{id.inspect}", 'id')
  end
  "#{self.class.url}/#{CGI.escape(id)}"
end