Class: FreshBooks::Base

Inherits:
Object
  • Object
show all
Includes:
Schema::Mixin
Defined in:
lib/freshbooks/base.rb

Constant Summary collapse

@@connection =
nil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Schema::Mixin

included

Constructor Details

#initialize(args = {}) ⇒ Base

Returns a new instance of Base.


9
10
11
# File 'lib/freshbooks/base.rb', line 9

def initialize(args = {})
  args.each_pair {|k, v| send("#{k}=", v)}
end

Instance Attribute Details

#error_msgObject (readonly)

Returns the value of attribute error_msg


7
8
9
# File 'lib/freshbooks/base.rb', line 7

def error_msg
  @error_msg
end

Class Method Details

.actions(*operations) ⇒ Object


103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/freshbooks/base.rb', line 103

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? # first param is optional and default to empty hash
        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_nameObject


72
73
74
75
76
77
78
79
80
81
# File 'lib/freshbooks/base.rb', line 72

def self.api_class_name
  klass = class_of_freshbooks_base_descendant(self)
  
  # Remove module, underscore between words, lowercase
  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


153
154
155
156
157
158
# File 'lib/freshbooks/base.rb', line 153

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


134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/freshbooks/base.rb', line 134

def self.api_list_action(action_name, options = {})
  # Create the proc for the list proxy to retrieve the next page
  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


83
84
85
86
87
88
89
90
91
# File 'lib/freshbooks/base.rb', line 83

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

.connectionObject


14
15
16
# File 'lib/freshbooks/base.rb', line 14

def self.connection
  @@connection
end

.define_class_method(symbol, &block) ⇒ Object


97
98
99
100
101
# File 'lib/freshbooks/base.rb', line 97

def self.define_class_method(symbol, &block)
  metaclass.instance_eval do
    define_method symbol, &block
  end
end

.establish_connection(account_url, auth_token, request_headers = {}) ⇒ Object


18
19
20
# File 'lib/freshbooks/base.rb', line 18

def self.establish_connection(, auth_token, request_headers = {})
  @@connection = Connection.new(, auth_token, request_headers)
end

.metaclassObject


93
94
95
# File 'lib/freshbooks/base.rb', line 93

def self.metaclass
  class << self; self; end
end

.new_from_xml(xml_root) ⇒ Object


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/freshbooks/base.rb', line 22

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?
    next if XmlSerializer.deprecated? node
    
    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
  # raise ParseError.new(e, xml_root.to_s)
end

Instance Method Details

#api_action(action_name) ⇒ Object


160
161
162
163
164
165
# File 'lib/freshbooks/base.rb', line 160

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


167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/freshbooks/base.rb', line 167

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)
  if response.success?
    self.primary_key_value = response.elements[1].text.to_i
    @error_msg = nil
    true
  else
    @error_msg = response.error_msg
    false
  end
end

#api_update_action(action_name) ⇒ Object


181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/freshbooks/base.rb', line 181

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)
  if response.success?
    @error_msg = nil
    true
  else
    @error_msg = response.error_msg
    false
  end
end

#primary_keyObject


60
61
62
# File 'lib/freshbooks/base.rb', line 60

def primary_key
  "#{self.class.api_class_name}_id"
end

#primary_key_valueObject


64
65
66
# File 'lib/freshbooks/base.rb', line 64

def primary_key_value
  send(primary_key)
end

#primary_key_value=(value) ⇒ Object


68
69
70
# File 'lib/freshbooks/base.rb', line 68

def primary_key_value=(value)
  send("#{primary_key}=", value)
end

#to_xml(elem_name = nil) ⇒ Object


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/freshbooks/base.rb', line 43

def to_xml(elem_name = nil)
  # The root element is the class name underscored
  elem_name ||= self.class.to_s.split('::').last.underscore
  root = REXML::Element.new(elem_name)
  
  # Add each member to the root elem
  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