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

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



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
119
120
121
122
# File 'lib/freshbooks/base.rb', line 93

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



67
68
69
70
71
72
73
74
75
76
# File 'lib/freshbooks/base.rb', line 67

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



143
144
145
146
147
148
# File 'lib/freshbooks/base.rb', line 143

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



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/freshbooks/base.rb', line 124

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



78
79
80
81
82
83
84
85
86
# File 'lib/freshbooks/base.rb', line 78

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



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

def self.connection
  @@connection
end

.define_class_method(symbol, &block) ⇒ Object



88
89
90
# File 'lib/freshbooks/base.rb', line 88

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(, auth_token, request_headers = {})
  @@connection = Connection.new(, auth_token, request_headers)
end

.new_from_xml(xml_root) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# 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]
    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



150
151
152
153
154
155
# File 'lib/freshbooks/base.rb', line 150

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



157
158
159
160
161
162
163
# File 'lib/freshbooks/base.rb', line 157

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



165
166
167
168
169
170
# File 'lib/freshbooks/base.rb', line 165

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



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

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

#primary_key_valueObject



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

def primary_key_value
  send(primary_key)
end

#primary_key_value=(value) ⇒ Object



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

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

#to_xml(elem_name = nil) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/freshbooks/base.rb', line 38

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