Class: FreshBooks::Base
Direct Known Subclasses
Category, Client, Estimate, Expense, Invoice, Item, Line, Links, Payment, Project, Recurring, Staff, Task, TimeEntry
Constant Summary
collapse
- @@connection =
nil
Class Method Summary
collapse
Instance Method Summary
collapse
included
Constructor Details
#initialize(args = {}) ⇒ Base
Returns a new instance of Base.
8
9
10
|
# File 'lib/freshbooks/base.rb', line 8
def initialize(args = {})
args.each_pair {|k, v| send("#{k}=", v)}
end
|
Class Method Details
.actions(*operations) ⇒ Object
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
|
# File 'lib/freshbooks/base.rb', line 96
def self.actions(*operations)
operations.each do |operation|
method_name = operation.to_s
api_action_name = method_name.camelize(:lower)
case method_name
when "list"
define_class_method(method_name) do |*args|
args << {} if args.empty?
api_list_action(api_action_name, *args)
end
when "get"
define_class_method(method_name) do |object_id|
api_get_action(api_action_name, object_id)
end
when "create"
define_method(method_name) do
api_create_action(api_action_name)
end
when "update"
define_method(method_name) do
api_update_action(api_action_name)
end
else
define_method(method_name) do
api_action(api_action_name)
end
end
end
end
|
.api_class_name ⇒ Object
70
71
72
73
74
75
76
77
78
79
|
# File 'lib/freshbooks/base.rb', line 70
def self.api_class_name
klass = class_of_freshbooks_base_descendant(self)
klass.name.
gsub(/^.*::/, "").
gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
gsub(/([a-z\d])([A-Z])/,'\1_\2').
downcase
end
|
.api_get_action(action_name, object_id) ⇒ Object
146
147
148
149
150
151
|
# File 'lib/freshbooks/base.rb', line 146
def self.api_get_action(action_name, object_id)
response = FreshBooks::Base.connection.call_api(
"#{api_class_name}.#{action_name}",
"#{api_class_name}_id" => object_id)
response.success? ? self.new_from_xml(response.elements[1]) : nil
end
|
.api_list_action(action_name, options = {}) ⇒ Object
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
|
# File 'lib/freshbooks/base.rb', line 127
def self.api_list_action(action_name, options = {})
list_page_proc = proc do |page|
options["page"] = page
response = FreshBooks::Base.connection.call_api("#{api_class_name}.#{action_name}", options)
raise FreshBooks::InternalError.new(response.error_msg) unless response.success?
root = response.elements[1]
array = root.elements.map { |item| self.new_from_xml(item) }
current_page = Page.new(root.attributes['page'], root.attributes['per_page'], root.attributes['total'], array.size)
[array, current_page]
end
ListProxy.new(list_page_proc)
end
|
.class_of_freshbooks_base_descendant(klass) ⇒ Object
81
82
83
84
85
86
87
88
89
|
# File 'lib/freshbooks/base.rb', line 81
def self.class_of_freshbooks_base_descendant(klass)
if klass.superclass == Base
klass
elsif klass.superclass.nil?
raise "#{name} doesn't belong in a hierarchy descending from ActiveRecord"
else
self.class_of_freshbooks_base_descendant(klass.superclass)
end
end
|
.connection ⇒ Object
13
14
15
|
# File 'lib/freshbooks/base.rb', line 13
def self.connection
@@connection
end
|
.define_class_method(symbol, &block) ⇒ Object
91
92
93
|
# File 'lib/freshbooks/base.rb', line 91
def self.define_class_method(symbol, &block)
self.class.send(:define_method, symbol, &block)
end
|
.establish_connection(account_url, auth_token, request_headers = {}) ⇒ Object
17
18
19
|
# File 'lib/freshbooks/base.rb', line 17
def self.establish_connection(account_url, auth_token, = {})
@@connection = Connection.new(account_url, auth_token, )
end
|
.new_from_xml(xml_root) ⇒ Object
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
# File 'lib/freshbooks/base.rb', line 21
def self.new_from_xml(xml_root)
object = self.new()
self.schema_definition.members.each do |member_name, member_options|
node = xml_root.elements[member_name.dup]
next if node.nil?
value = FreshBooks::XmlSerializer.to_value(node, member_options[:type])
object.send("#{member_name}=", value)
end
return object
rescue => e
error = ParseError.new(e, xml_root.to_s)
error.set_backtrace(e.backtrace)
raise error
end
|
Instance Method Details
#api_action(action_name) ⇒ Object
153
154
155
156
157
158
|
# File 'lib/freshbooks/base.rb', line 153
def api_action(action_name)
response = FreshBooks::Base.connection.call_api(
"#{self.class.api_class_name}.#{action_name}",
"#{self.class.api_class_name}_id" => primary_key_value)
response.success?
end
|
#api_create_action(action_name) ⇒ Object
160
161
162
163
164
165
166
|
# File 'lib/freshbooks/base.rb', line 160
def api_create_action(action_name)
response = FreshBooks::Base.connection.call_api(
"#{self.class.api_class_name}.#{action_name}",
self.class.api_class_name => self)
self.primary_key_value = response.elements[1].text.to_i if response.success?
response.success?
end
|
#api_update_action(action_name) ⇒ Object
168
169
170
171
172
173
|
# File 'lib/freshbooks/base.rb', line 168
def api_update_action(action_name)
response = FreshBooks::Base.connection.call_api(
"#{self.class.api_class_name}.#{action_name}",
self.class.api_class_name => self)
response.success?
end
|
#primary_key ⇒ Object
58
59
60
|
# File 'lib/freshbooks/base.rb', line 58
def primary_key
"#{self.class.api_class_name}_id"
end
|
#primary_key_value ⇒ Object
62
63
64
|
# File 'lib/freshbooks/base.rb', line 62
def primary_key_value
send(primary_key)
end
|
#primary_key_value=(value) ⇒ Object
66
67
68
|
# File 'lib/freshbooks/base.rb', line 66
def primary_key_value=(value)
send("#{primary_key}=", value)
end
|
#to_xml(elem_name = nil) ⇒ Object
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
# File 'lib/freshbooks/base.rb', line 41
def to_xml(elem_name = nil)
elem_name ||= self.class.to_s.split('::').last.underscore
root = REXML::Element.new(elem_name)
self.schema_definition.members.each do |member_name, member_options|
value = self.send(member_name)
next if member_options[:read_only] || value.nil?
element = FreshBooks::XmlSerializer.to_node(member_name, value, member_options[:type])
root.add_element(element) if element != nil
end
root.to_s
end
|