Class: Postmaster::APIResource

Inherits:
PostmasterObject show all
Defined in:
lib/postmaster/api_resource.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from PostmasterObject

#[], #[]=, construct_from, #delete, #each, #has_key?, #initialize, #inspect, #keys, #refresh_from, #to_hash, #to_json, #to_s, #values

Constructor Details

This class inherits a constructor from Postmaster::PostmasterObject

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Postmaster::PostmasterObject

Class Method Details

.class_nameObject



3
4
5
# File 'lib/postmaster/api_resource.rb', line 3

def self.class_name
  self.name.split('::')[-1]
end

.retrieve(id) ⇒ Object



30
31
32
33
34
# File 'lib/postmaster/api_resource.rb', line 30

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

.urlObject



7
8
9
10
11
12
# File 'lib/postmaster/api_resource.rb', line 7

def self.url()
  if self == APIResource
    raise NotImplementedError.new('APIResource is an abstract class.  You should perform actions on its subclasses (Charge, Customer, etc.)')
  end
  "/v1/#{CGI.escape(class_name.downcase)}s"
end

Instance Method Details

#refreshObject



24
25
26
27
28
# File 'lib/postmaster/api_resource.rb', line 24

def refresh
  response = Postmaster.request(:get, url)
  refresh_from(response)
  self
end

#url(action = nil) ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'lib/postmaster/api_resource.rb', line 14

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