turingstudio-freshbooks-rb 3.0.2 → 3.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt CHANGED
@@ -1,4 +1,10 @@
1
- == 0.0.1 2009-02-18
1
+ * 3.0.3
2
+ * Merged with http://github.com/bcurren/freshbooks.rb
3
+ * Create connection class
4
+ * Convert xml to ruby type: date, date_time, boolean
5
+ * Added links to objects
2
6
 
3
- * 1 major enhancement:
4
- * Initial release
7
+ * 3.0 major enhancement:
8
+ * Gemified
9
+ * Added specs
10
+ * Added methods to describe FreshBooks objects and actions
data/lib/freshbooks.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  #
2
2
  # Usage:
3
3
  #
4
- # FreshBooks::Base.setup('sample.freshbooks.com', 'mytoken')
4
+ # FreshBooks::Base.establish_connection!('sample.freshbooks.com', 'mytoken')
5
5
  #
6
6
  # clients = FreshBooks::Client.list
7
7
  # client = clients[0]
@@ -21,6 +21,7 @@ $:.unshift(File.dirname(__FILE__))
21
21
 
22
22
  require 'net/https'
23
23
  require 'rexml/document'
24
+ require 'logger'
24
25
  require 'activesupport'
25
26
 
26
27
  require 'freshbooks/version'
@@ -43,3 +44,4 @@ require 'freshbooks/staff'
43
44
  require 'freshbooks/task'
44
45
  require 'freshbooks/time_entry'
45
46
  require 'freshbooks/xml_serializer'
47
+ require 'freshbooks/xml_serializer/serializers'
@@ -1,11 +1,11 @@
1
1
  module FreshBooks
2
- class Base
3
- class InternalError < Exception; end;
4
- class AuthenticationError < Exception; end;
5
- class UnknownSystemError < Exception; end;
6
- class InvalidParameterError < Exception; end;
7
- class InvalidAccountUrlError < Exception; end;
8
-
2
+ class InternalError < Exception; end;
3
+ class AuthenticationError < Exception; end;
4
+ class UnknownSystemError < Exception; end;
5
+ class InvalidParameterError < Exception; end;
6
+ class InvalidAccountUrlError < Exception; end;
7
+
8
+ class Base
9
9
  def initialize(data = {})
10
10
  @changed_fields = []
11
11
  self.class.attributes.each do |name, options|
@@ -14,9 +14,8 @@ module FreshBooks
14
14
  data.each do |name, value|
15
15
  send("#{name}=", value) if self.class.attributes[name]
16
16
  end
17
- #@changed_fields = [] # reset changed fields
18
17
  end
19
-
18
+
20
19
  def to_xml(elem_name = nil)
21
20
  root = REXML::Element.new(node_name)
22
21
  self.class.attributes.each do |name, options|
@@ -32,7 +31,7 @@ module FreshBooks
32
31
  end
33
32
  root
34
33
  end
35
-
34
+
36
35
  class << self
37
36
  attr_reader :connection
38
37
 
@@ -51,7 +50,7 @@ module FreshBooks
51
50
  end
52
51
  object
53
52
  end
54
-
53
+
55
54
  def node_name
56
55
  self.to_s.split('::').last.underscore
57
56
  end
@@ -129,13 +128,13 @@ module FreshBooks
129
128
  #
130
129
  def define_class_method(name, &block)
131
130
  self.class.send(:define_method, name, &block)
132
- end
133
-
131
+ end
132
+
134
133
  def actions(*args)
135
134
  args.each do |action|
136
135
  method = action.to_s
137
136
  api_method = method.camelize(:lower)
138
-
137
+
139
138
  case method
140
139
  when 'list'
141
140
  define_class_method(method) do |*args|
@@ -171,27 +170,38 @@ module FreshBooks
171
170
  end
172
171
 
173
172
  def api_list_action(method, options = {})
174
- resp = Base.connection.call_api("#{node_name}.#{method}", options)
175
- resp.success? ? ListProxy.new_from_xml(self, resp) : nil
176
- end
173
+ list_page_proc = proc do |page|
174
+ options["page"] = page
175
+ response = FreshBooks::Base.connection.call_api("#{node_name}.#{method}", options)
176
+ raise FreshBooks::InternalError.new("Response was not successful. This should never happen.") unless response.success?
177
+
178
+ root = response.elements[1]
179
+ array = root.elements.map { |item| self.new_from_xml(item) }
180
+ current_page = Page.new(root.attributes['page'], root.attributes['per_page'], root.attributes['total'])
177
181
 
182
+ [array, current_page]
183
+ end
184
+
185
+ ListProxy.new(list_page_proc)
186
+ end
187
+
178
188
  def api_get_action(method, id)
179
189
  resp = Base.connection.call_api("#{node_name}.#{method}", "#{node_name}_id" => id)
180
190
  resp.success? ? self.new_from_xml(resp.elements[1]) : nil
181
191
  end
182
-
192
+
183
193
  def api_delete_action(method, id)
184
194
  resp = Base.connection.call_api("#{node_name}.#{method}", "#{node_name}_id" => id)
185
195
  resp.success?
186
196
  end
187
-
197
+
188
198
  def api_action(method, id)
189
199
  resp = Base.connection.call_api("#{node_name}.#{method}", "#{node_name}_id" => id)
190
200
  resp.success?
191
201
  end
192
202
  end
193
203
  end
194
-
204
+
195
205
  protected
196
206
 
197
207
  def node_name
@@ -203,10 +213,10 @@ module FreshBooks
203
213
  self.send("#{node_name}_id=", resp.elements[1].text.to_i) if resp.success?
204
214
  resp.success?
205
215
  end
206
-
216
+
207
217
  def api_update_action(method)
208
218
  resp = Base.connection.call_api("#{node_name}.#{method}", node_name => self)
209
219
  resp.success?
210
- end
220
+ end
211
221
  end
212
222
  end
@@ -21,4 +21,4 @@ module FreshBooks
21
21
  has_many :expenses
22
22
  actions :list, :get
23
23
  end
24
- end
24
+ end
@@ -1,5 +1,5 @@
1
1
  module FreshBooks
2
- VERSION = '3.0.2' # Gem version
2
+ VERSION = '3.0.3' # Gem version
3
3
  API_VERSION = '2.1' # FreshBooks API version
4
4
 
5
5
  SERVICE_URL = "/api/#{API_VERSION}/xml-in"
@@ -292,13 +292,13 @@ describe FreshBooks::Base do
292
292
  end
293
293
 
294
294
  it "should call api method" do
295
- @connection.should_receive(:call_api).with('test.list', :name => 'value').and_return(@resp)
295
+ @connection.should_receive(:call_api).with('test.list', 'page' => 1, :name => 'value').and_return(@resp)
296
296
  Test.list(:name => 'value')
297
297
  end
298
298
 
299
299
  it "should return nil if request failed" do
300
300
  @resp.stub!(:success?).and_return(false)
301
- Test.list.should be_nil
301
+ lambda { Test.list }.should raise_error
302
302
  end
303
303
 
304
304
  it "should create a list proxy if request succeeded" do
@@ -6,7 +6,7 @@ describe FreshBooks::Staff do
6
6
  end
7
7
 
8
8
  it "should list all staffs" do
9
- FreshBooks::Staff.list.size.should > 0
9
+ FreshBooks::Staff.list.should be_a(FreshBooks::ListProxy)
10
10
  end
11
11
 
12
12
  it "should find staff by id" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: turingstudio-freshbooks-rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.2
4
+ version: 3.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - The Turing Studio, Inc.
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-03-04 00:00:00 -08:00
12
+ date: 2009-03-05 00:00:00 -08:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency