Class: FreshBooks::Base
- Inherits:
-
Object
show all
- Defined in:
- lib/freshbooks/base.rb
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(attributes = {}) ⇒ Base
Returns a new instance of Base.
5
6
7
8
9
10
|
# File 'lib/freshbooks/base.rb', line 5
def initialize(attributes={})
@attributes = attributes.inject({}) { |result, pair|
result[pair.first] = self.class.normalize(*pair)
result
}
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
12
13
14
15
16
17
18
|
# File 'lib/freshbooks/base.rb', line 12
def method_missing(method, *args, &block)
if @attributes.has_key?(method.to_s)
@attributes[method.to_s]
else
super
end
end
|
Class Method Details
.belongs_to(model) ⇒ Object
77
78
79
80
81
82
83
|
# File 'lib/freshbooks/base.rb', line 77
def self.belongs_to(model)
class_eval <<-EOF
def #{model}
FreshBooks::#{model.to_s.classify}.get(#{model}_id)
end
EOF
end
|
.create(params = {}) ⇒ Object
31
32
33
|
# File 'lib/freshbooks/base.rb', line 31
def self.create(params={})
call_api('create', params)[id_field]
end
|
.delete(id) ⇒ Object
44
45
46
|
# File 'lib/freshbooks/base.rb', line 44
def self.delete(id)
call_api_with_id('delete', id)
end
|
.get(id) ⇒ Object
39
40
41
42
|
# File 'lib/freshbooks/base.rb', line 39
def self.get(id)
result = call_api_with_id('get', id).andand[prefix]
new(result) if result
end
|
.has_many(model) ⇒ Object
69
70
71
72
73
74
75
|
# File 'lib/freshbooks/base.rb', line 69
def self.has_many(model)
class_eval <<-EOF
def #{model}(params={})
FreshBooks::#{model.to_s.classify}.list(params.merge(self.class.id_field => id))
end
EOF
end
|
.list(params = {}) ⇒ Object
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
# File 'lib/freshbooks/base.rb', line 48
def self.list(params={})
params[:per_page] ||= 100
if params[:per_page] < 100
(call_api('list', params))
else
returning [] do |all|
params[:page] = 1
page = (call_api('list', params))
all.concat(page)
while page.size == 100
params[:page] += 1
page = (call_api('list', params))
all.concat(page)
end
end
end
end
|
.map(mappings = {}) ⇒ Object
24
25
26
27
|
# File 'lib/freshbooks/base.rb', line 24
def self.map(mappings={})
@mappings ||= {}
@mappings.merge!(mappings)
end
|
.update(id, params = {}) ⇒ Object
35
36
37
|
# File 'lib/freshbooks/base.rb', line 35
def self.update(id, params={})
call_api_with_id('update', id, params)
end
|
Instance Method Details
#id ⇒ Object
20
21
22
|
# File 'lib/freshbooks/base.rb', line 20
def id
@attributes[self.class.id_field]
end
|