Class: SweetTooth::APIResource

Inherits:
SweetToothObject show all
Defined in:
lib/sweettooth/api_resource.rb

Instance Attribute Summary

Attributes inherited from SweetToothObject

#api_key

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from SweetToothObject

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

Constructor Details

This class inherits a constructor from SweetTooth::SweetToothObject

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class SweetTooth::SweetToothObject

Class Method Details

.class_nameObject



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

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

.class_name_pluralObject

Override for irregular plurals, eg. ‘Activities’



8
9
10
# File 'lib/sweettooth/api_resource.rb', line 8

def self.class_name_plural
  "#{self.class_name}s"
end

.retrieve(id, api_key = nil) ⇒ Object



32
33
34
35
36
# File 'lib/sweettooth/api_resource.rb', line 32

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

.urlObject



12
13
14
15
16
17
# File 'lib/sweettooth/api_resource.rb', line 12

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_plural.downcase)}"
end

Instance Method Details

#refreshObject



26
27
28
29
30
# File 'lib/sweettooth/api_resource.rb', line 26

def refresh
  response, api_key = SweetTooth.request(:get, url, @api_key, @retrieve_options)
  refresh_from(response, api_key)
  self
end

#urlObject



19
20
21
22
23
24
# File 'lib/sweettooth/api_resource.rb', line 19

def url
  unless id = self['id']
    raise 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