Class: Billogram::Resource

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

Constant Summary collapse

DEFAULT_SEARCH_OPTIONS =
{ page: 1, page_size: 50 }

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Resource

Returns a new instance of Resource.



57
58
59
60
61
62
63
# File 'lib/billogram/resource.rb', line 57

def initialize(attributes = {})
  Hash(attributes).each do |key, value|
    public_send("#{key}=", value) if respond_to?(key)
  end

  RelationBuilder.new(self, attributes).call
end

Class Method Details

.build_objects(data) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/billogram/resource.rb', line 43

def build_objects(data)
  case data
  when Hash then new(data)
  when Array then data.map{|item| build_objects(item) }
  else data
  end
end

.create(attributes) ⇒ Object



26
27
28
# File 'lib/billogram/resource.rb', line 26

def create(attributes)
  perform_request(:post, "#{endpoint}", attributes)
end

.delete(id) ⇒ Object



34
35
36
# File 'lib/billogram/resource.rb', line 34

def delete(id)
  perform_request(:delete, "#{endpoint}/#{id}")
end

.endpoint(value = nil) ⇒ Object



12
13
14
15
# File 'lib/billogram/resource.rb', line 12

def endpoint(value = nil)
  @endpoint = value if value
  @endpoint || name.demodulize.underscore
end

.fetch(id = nil) ⇒ Object



22
23
24
# File 'lib/billogram/resource.rb', line 22

def fetch(id = nil)
  perform_request(:get, "#{endpoint}/#{id}")
end

.perform_request(type, url, params = {}) ⇒ Object



51
52
53
54
# File 'lib/billogram/resource.rb', line 51

def perform_request(type, url, params = {})
  response = Request.new(type, url, params).execute
  build_objects(response)
end

.relation(name, type, class_override: nil) ⇒ Object



38
39
40
41
# File 'lib/billogram/resource.rb', line 38

def relation(name, type, class_override: nil)
  relations << Relation.new(name, type, class_override: class_override)
  attr_accessor name
end

.relationsObject



8
9
10
# File 'lib/billogram/resource.rb', line 8

def relations
  @relations ||= []
end

.search(options = {}) ⇒ Object



17
18
19
20
# File 'lib/billogram/resource.rb', line 17

def search(options = {})
  query = DEFAULT_SEARCH_OPTIONS.merge(options)
  perform_request(:get, "#{endpoint}", query)
end

.update(id, attributes) ⇒ Object



30
31
32
# File 'lib/billogram/resource.rb', line 30

def update(id, attributes)
  perform_request(:put, "#{endpoint}/#{id}", attributes)
end

Instance Method Details

#deleteObject



69
70
71
# File 'lib/billogram/resource.rb', line 69

def delete
  self.class.delete(id)
end

#endpointObject



73
74
75
# File 'lib/billogram/resource.rb', line 73

def endpoint
  self.class.endpoint
end

#to_json(*args) ⇒ Object



77
78
79
80
81
82
# File 'lib/billogram/resource.rb', line 77

def to_json(*args)
  instance_variables
    .map{|var| ["#{var}"[1..-1], instance_variable_get(var)]}
    .to_h
    .to_json(*args)
end

#update(attributes) ⇒ Object



65
66
67
# File 'lib/billogram/resource.rb', line 65

def update(attributes)
  self.class.update(id, attributes)
end