Class: GifterHub::ApiResource

Inherits:
Object
  • Object
show all
Includes:
Operations::Get
Defined in:
lib/gifter_hub/api_resource.rb

Direct Known Subclasses

GiftCard

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Operations::Get

included

Constructor Details

#initialize(id = nil, opts = {}) ⇒ ApiResource

Returns a new instance of ApiResource.



6
7
8
9
10
11
# File 'lib/gifter_hub/api_resource.rb', line 6

def initialize(id=nil, opts={})
  @id = id
  @opts = opts
  @values = {}
  @attributes = {}
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



4
5
6
# File 'lib/gifter_hub/api_resource.rb', line 4

def attributes
  @attributes
end

#idObject

Returns the value of attribute id.



4
5
6
# File 'lib/gifter_hub/api_resource.rb', line 4

def id
  @id
end

#optsObject

Returns the value of attribute opts.



4
5
6
# File 'lib/gifter_hub/api_resource.rb', line 4

def opts
  @opts
end

Class Method Details

.class_nameObject



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

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

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



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

def self.get(id, opts={})
  instance = self.new(id, opts)
  instance.refresh
  instance
end

.resource_urlObject



17
18
19
20
21
22
# File 'lib/gifter_hub/api_resource.rb', line 17

def self.resource_url
  if self == ApiResource
    raise NotImplementedError
  end
  "/api/#{CGI.escape(class_name.tableize)}"
end

Instance Method Details

#inspectObject



30
31
32
# File 'lib/gifter_hub/api_resource.rb', line 30

def inspect
  JSON.pretty_generate(attributes)
end

#refreshObject



38
39
40
41
# File 'lib/gifter_hub/api_resource.rb', line 38

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

#resource_urlObject



34
35
36
# File 'lib/gifter_hub/api_resource.rb', line 34

def resource_url
  "#{self.class.resource_url}/#{CGI.escape(id)}"
end