Class: Billogram::Resource

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

Constant Summary collapse

DEFAULT_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.


65
66
67
68
69
70
71
# File 'lib/billogram/resource.rb', line 65

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("#{endpoint}", :post, attributes)
end

.delete(id) ⇒ Object


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

def delete(id)
  Billogram.client.put("#{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) ⇒ Object


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

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

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


51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/billogram/resource.rb', line 51

def perform_request(url, type, params = {})
  case type
  when :post, :put
    query = { body: params.to_json }
  when :get
    query = { query: params }
  else nil
  end

  response = Billogram.client.send(type, url, query)
  build_objects(response) unless type == :delete
end

.relation(relation_name, relation_type = :one) ⇒ Object


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

def relation(relation_name, relation_type = :one)
  relations[relation_type] << relation_name
  attr_accessor relation_name
end

.relationsObject


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

def relations
  @relations ||= { one: Set.new, many: Set.new }
end

.search(options = {}) ⇒ Object


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

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

.update(id, attributes) ⇒ Object


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

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

Instance Method Details

#deleteObject


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

def delete
  self.class.delete(id)
end

#endpointObject


85
86
87
# File 'lib/billogram/resource.rb', line 85

def endpoint
  self.class.endpoint
end

#perform_request(*args) ⇒ Object


81
82
83
# File 'lib/billogram/resource.rb', line 81

def perform_request(*args)
  self.class.perform_request(*args)
end

#to_json(*args) ⇒ Object


89
90
91
92
93
94
# File 'lib/billogram/resource.rb', line 89

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

#update(attributes) ⇒ Object


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

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