Class: Pingpp::APIResource

Inherits:
PingppObject show all
Includes:
Pingpp::APIOperations::Request
Defined in:
lib/pingpp/api_resource.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Pingpp::APIOperations::Request

included

Methods inherited from PingppObject

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

Constructor Details

This class inherits a constructor from Pingpp::PingppObject

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Pingpp::PingppObject

Class Method Details

.class_nameObject



5
6
7
# File 'lib/pingpp/api_resource.rb', line 5

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

.object_nameObject



9
10
11
# File 'lib/pingpp/api_resource.rb', line 9

def self.object_name
  class_name.downcase
end

.resource_url(opts = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/pingpp/api_resource.rb', line 17

def self.resource_url(opts={})
  if self == APIResource
    raise NotImplementedError.new('APIResource is an abstract class.  You should perform actions on its subclasses (Charge, etc.)')
  end

  if opts.has_key?(:parents)
    if opts[:parents].is_a?(Array)
      "/v1/#{opts[:parents].join('/')}/#{CGI.escape(uri_object_name.downcase)}s"
    else
      raise ArgumentError.new("opts[:parents] should be an Array")
    end
  else
    "/v1/#{CGI.escape(uri_object_name.downcase)}s"
  end
end

.retrieve(id, opts = {}) ⇒ Object



45
46
47
48
49
50
# File 'lib/pingpp/api_resource.rb', line 45

def self.retrieve(id, opts={})
  opts = Util.normalize_opts(opts)
  instance = self.new(id, opts)
  instance.refresh
  instance
end

.uri_object_nameObject



13
14
15
# File 'lib/pingpp/api_resource.rb', line 13

def self.uri_object_name
  object_name
end

Instance Method Details

#refreshObject



40
41
42
43
# File 'lib/pingpp/api_resource.rb', line 40

def refresh
  response, opts = request(:get, resource_url(@opts), @retrieve_params)
  initialize_from(response, opts)
end

#resource_url(opts = {}) ⇒ Object



33
34
35
36
37
38
# File 'lib/pingpp/api_resource.rb', line 33

def resource_url(opts={})
  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.resource_url(opts)}/#{CGI.escape(id)}"
end