Class: FreshBooks::Base

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

Constant Summary collapse

@@connection =
nil

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Schema::Mixin

included

Class Method Details

.actions(*operations) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/freshbooks/base.rb', line 89

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



63
64
65
66
67
68
69
70
71
72
# File 'lib/freshbooks/base.rb', line 63

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



139
140
141
142
143
144
# File 'lib/freshbooks/base.rb', line 139

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



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/freshbooks/base.rb', line 120

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



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

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



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

def self.connection
  @@connection
end

.define_class_method(symbol, &block) ⇒ Object



84
85
86
# File 'lib/freshbooks/base.rb', line 84

def self.define_class_method(symbol, &block)
  self.class.send(:define_method, symbol, &block)
end

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



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

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

.new_from_xml(xml_root) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/freshbooks/base.rb', line 17

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]
    next if node.nil?
    
    value = FreshBooks::XmlSerializer.to_value(node, member_options[:type])
    object.send("#{member_name}=", value)
  end
  
  return object
  
rescue => e
  raise ParseError.new(e, xml_root.to_s)
end

Instance Method Details

#api_action(action_name) ⇒ Object



146
147
148
149
150
151
# File 'lib/freshbooks/base.rb', line 146

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



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

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



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

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_keyObject



51
52
53
# File 'lib/freshbooks/base.rb', line 51

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

#primary_key_valueObject



55
56
57
# File 'lib/freshbooks/base.rb', line 55

def primary_key_value
  send(primary_key)
end

#primary_key_value=(value) ⇒ Object



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

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

#to_xml(elem_name = nil) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/freshbooks/base.rb', line 34

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