freshbooks.rb 3.0.13

Sign up to get free protection for your applications and to get access to all the features.
Files changed (46) hide show
  1. data/History.txt +8 -0
  2. data/LICENSE +10 -0
  3. data/Manifest.txt +45 -0
  4. data/README +44 -0
  5. data/Rakefile +27 -0
  6. data/lib/freshbooks.rb +94 -0
  7. data/lib/freshbooks/base.rb +169 -0
  8. data/lib/freshbooks/category.rb +11 -0
  9. data/lib/freshbooks/client.rb +22 -0
  10. data/lib/freshbooks/connection.rb +153 -0
  11. data/lib/freshbooks/estimate.rb +15 -0
  12. data/lib/freshbooks/expense.rb +12 -0
  13. data/lib/freshbooks/invoice.rb +18 -0
  14. data/lib/freshbooks/item.rb +11 -0
  15. data/lib/freshbooks/line.rb +10 -0
  16. data/lib/freshbooks/links.rb +7 -0
  17. data/lib/freshbooks/list_proxy.rb +80 -0
  18. data/lib/freshbooks/payment.rb +13 -0
  19. data/lib/freshbooks/project.rb +12 -0
  20. data/lib/freshbooks/recurring.rb +15 -0
  21. data/lib/freshbooks/response.rb +25 -0
  22. data/lib/freshbooks/schema/definition.rb +20 -0
  23. data/lib/freshbooks/schema/mixin.rb +40 -0
  24. data/lib/freshbooks/staff.rb +13 -0
  25. data/lib/freshbooks/task.rb +12 -0
  26. data/lib/freshbooks/time_entry.rb +12 -0
  27. data/lib/freshbooks/xml_serializer.rb +17 -0
  28. data/lib/freshbooks/xml_serializer/serializers.rb +109 -0
  29. data/script/console +10 -0
  30. data/script/destroy +14 -0
  31. data/script/generate +14 -0
  32. data/test/fixtures/freshbooks_credentials.sample.yml +3 -0
  33. data/test/fixtures/invoice_create_response.xml +4 -0
  34. data/test/fixtures/invoice_get_response.xml +54 -0
  35. data/test/fixtures/invoice_list_response.xml +109 -0
  36. data/test/fixtures/success_response.xml +2 -0
  37. data/test/mock_connection.rb +13 -0
  38. data/test/schema/test_definition.rb +36 -0
  39. data/test/schema/test_mixin.rb +39 -0
  40. data/test/test_base.rb +97 -0
  41. data/test/test_connection.rb +145 -0
  42. data/test/test_helper.rb +48 -0
  43. data/test/test_invoice.rb +125 -0
  44. data/test/test_list_proxy.rb +60 -0
  45. data/test/test_page.rb +50 -0
  46. metadata +148 -0
@@ -0,0 +1,48 @@
1
+ require File.dirname(__FILE__) + '/../lib/freshbooks'
2
+
3
+ require 'stringio'
4
+ require 'test/unit'
5
+ require File.dirname(__FILE__) + '/mock_connection'
6
+
7
+ begin
8
+ require 'mocha'
9
+ rescue LoadError
10
+ require 'rubygems'
11
+ gem 'mocha'
12
+ require 'mocha'
13
+ end
14
+
15
+ class Test::Unit::TestCase
16
+
17
+ @@fixtures = {}
18
+ def self.fixtures list
19
+ [list].flatten.each do |fixture|
20
+ self.class_eval do
21
+ # add a method name for this fixture type
22
+ define_method(fixture) do |item|
23
+ # load and cache the YAML
24
+ @@fixtures[fixture] ||= YAML::load_file(self.fixture_dir + "/#{fixture.to_s}.yml")
25
+ @@fixtures[fixture][item.to_s]
26
+ end
27
+ end
28
+ end
29
+ end
30
+
31
+
32
+ def mock_connection(file_name)
33
+ mock_connection = MockConnection.new(fixture_xml_content(file_name))
34
+ FreshBooks::Base.stubs(:connection).with().returns(mock_connection)
35
+ mock_connection
36
+ end
37
+
38
+ def fixture_xml_content(file_name)
39
+ # Quick way to remove white space and newlines from xml. Makes it easier to compare in tests
40
+ open(File.join(fixture_dir, "#{file_name}.xml"), "r").readlines.inject("") do |contents, line|
41
+ contents + line.strip
42
+ end
43
+ end
44
+
45
+ def fixture_dir
46
+ File.join(File.dirname(__FILE__), "fixtures")
47
+ end
48
+ end
@@ -0,0 +1,125 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ class TestInvoice < Test::Unit::TestCase
4
+ def test_list
5
+ mock_call_api("invoice.list", { "page" => 1 }, "invoice_list_response")
6
+
7
+ invoices = FreshBooks::Invoice.list
8
+ assert_equal 3, invoices.size
9
+ assert_invoice invoices[0], 0
10
+ assert_invoice invoices[1], 1
11
+ end
12
+
13
+ def test_get
14
+ invoice_id = 2
15
+ mock_call_api("invoice.get", { "invoice_id" => invoice_id }, "invoice_get_response")
16
+
17
+ invoice = FreshBooks::Invoice.get(invoice_id)
18
+ assert_invoice invoice, 0, true
19
+ end
20
+
21
+ def test_create
22
+ invoice = FreshBooks::Invoice.new
23
+ assert_nil invoice.invoice_id
24
+
25
+ mock_call_api("invoice.create", { "invoice" => invoice }, "invoice_create_response")
26
+ assert invoice.create
27
+ assert_equal 1, invoice.invoice_id
28
+ end
29
+
30
+ def test_update
31
+ invoice = FreshBooks::Invoice.new
32
+ invoice.invoice_id = 1
33
+
34
+ mock_call_api("invoice.update", { "invoice" => invoice }, "success_response")
35
+ assert invoice.update
36
+ end
37
+
38
+ def test_delete
39
+ invoice = FreshBooks::Invoice.new
40
+ invoice.invoice_id = 2
41
+ mock_call_api("invoice.delete", { "invoice_id" => invoice.invoice_id }, "success_response")
42
+
43
+ assert invoice.delete
44
+ end
45
+
46
+ def test_send_by_email
47
+ invoice = FreshBooks::Invoice.new
48
+ invoice.invoice_id = 2
49
+ mock_call_api("invoice.sendByEmail", { "invoice_id" => invoice.invoice_id }, "success_response")
50
+
51
+ assert invoice.send_by_email
52
+ end
53
+
54
+ def test_send_by_snail_mail
55
+ invoice = FreshBooks::Invoice.new
56
+ invoice.invoice_id = 2
57
+ mock_call_api("invoice.sendBySnailMail", { "invoice_id" => invoice.invoice_id }, "success_response")
58
+
59
+ assert invoice.send_by_snail_mail
60
+ end
61
+
62
+ private
63
+
64
+ def mock_call_api(method, options, response_fixture)
65
+ FreshBooks::Base.connection.
66
+ expects(:call_api).
67
+ with(method, options).
68
+ returns(FreshBooks::Response.new(fixture_xml_content(response_fixture)))
69
+ end
70
+
71
+ def assert_invoice(invoice, number, expanded_form = false)
72
+ number = number + 1
73
+
74
+ assert_equal number, invoice.invoice_id
75
+ assert_equal "number#{number}", invoice.number
76
+ assert_equal number, invoice.client_id
77
+ assert_equal number, invoice.recurring_id
78
+ assert_equal "draft", invoice.status
79
+ assert_equal (number * 100), invoice.amount
80
+ assert_equal (number * 100) / 2, invoice.amount_outstanding
81
+ assert_equal Date.parse("2009-02-0#{number}"), invoice.date
82
+
83
+ assert_equal number, invoice.po_number
84
+ assert_equal number.to_f, invoice.discount
85
+ assert_equal "notes#{number}", invoice.notes
86
+ assert_equal "terms#{number}", invoice.terms
87
+ assert_equal "first_name#{number}", invoice.first_name
88
+ assert_equal "last_name#{number}", invoice.last_name
89
+ assert_equal "p_street1#{number}", invoice.p_street1
90
+ assert_equal "p_street2#{number}", invoice.p_street2
91
+ assert_equal "p_city#{number}", invoice.p_city
92
+ assert_equal "p_state#{number}", invoice.p_state
93
+ assert_equal "p_country#{number}", invoice.p_country
94
+ assert_equal "p_code#{number}", invoice.p_code
95
+
96
+ assert_equal "return_uri#{number}", invoice.return_uri
97
+ assert_equal DateTime.parse("2009-08-#{number} 0#{number}:00:00 -04:00"), invoice.updated
98
+
99
+
100
+
101
+ assert_equal "client_view#{number}", invoice.links.client_view
102
+ assert_equal "view#{number}", invoice.links.view
103
+ assert_equal "edit#{number}", invoice.links.edit
104
+
105
+ lines = invoice.lines
106
+ assert_equal 1, lines.size
107
+ lines.each_with_index do |line, index|
108
+ assert_line(line, index)
109
+ end
110
+ end
111
+
112
+ def assert_line(line, number)
113
+ number = number + 1
114
+
115
+ assert_equal number.to_f, line.amount
116
+ assert_equal "name#{number}", line.name
117
+ assert_equal "description#{number}", line.description
118
+ assert_equal number.to_f, line.unit_cost
119
+ assert_equal number, line.quantity
120
+ assert_equal "tax1_name#{number}", line.tax1_name
121
+ assert_equal "tax2_name#{number}", line.tax2_name
122
+ assert_equal number.to_f, line.tax1_percent
123
+ assert_equal number.to_f, line.tax2_percent
124
+ end
125
+ end
@@ -0,0 +1,60 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ class TestListProxy < Test::Unit::TestCase
4
+ def test_size__should_be_total_size_of_results
5
+ list_proxy = FreshBooks::ListProxy.new(create_proc(1, 2, 3))
6
+ assert_equal 3, list_proxy.size
7
+ end
8
+
9
+ def test_reference_element
10
+ list_proxy = FreshBooks::ListProxy.new(create_proc(1, 2, 3))
11
+ assert_equal 0, list_proxy[0]
12
+ assert_equal 1, list_proxy[1]
13
+ assert_equal 2, list_proxy[2]
14
+ end
15
+
16
+ def test_each
17
+ list_proxy = FreshBooks::ListProxy.new(create_proc(1, 2, 3))
18
+ count = 0
19
+ list_proxy.each_with_index do |item, i|
20
+ assert_equal i, item, "Incorrect item on iteration #{i}"
21
+ count += 1
22
+ end
23
+ assert_equal count, list_proxy.size
24
+ end
25
+
26
+ def test_each__no_pages_and_items
27
+ list_proxy = FreshBooks::ListProxy.new(create_proc(0, 0, 0))
28
+ count = 0
29
+ list_proxy.each_with_index do |item, i|
30
+ count += 1
31
+ end
32
+ assert_equal 0, list_proxy.size
33
+ assert_equal 0, count
34
+ end
35
+
36
+ def test_each__response_doesnt_have_pagination_but_returns_items
37
+ list_proxy = FreshBooks::ListProxy.new(create_proc(0, 0, 0, 3))
38
+ count = 0
39
+ list_proxy.each_with_index do |item, i|
40
+ count += 1
41
+ end
42
+ assert_equal 3, list_proxy.size
43
+ assert_equal 3, count
44
+ end
45
+
46
+ private
47
+
48
+ def create_proc(page, per_page, total, total_in_array = total)
49
+ get_page_proc = proc do |page|
50
+ start_num = (page - 1) * per_page
51
+ end_num = start_num + per_page
52
+ if end_num >= total_in_array || end_num == 0
53
+ end_num = total_in_array
54
+ end
55
+
56
+ array = Range.new(start_num, end_num, true).to_a
57
+ [array, FreshBooks::Page.new(page, per_page, total, total_in_array)]
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,50 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ class TestPage < Test::Unit::TestCase
4
+ def test_pages
5
+ page = FreshBooks::Page.new(1, 2, 1)
6
+ assert_equal 1, page.pages
7
+
8
+ page = FreshBooks::Page.new(1, 2, 2)
9
+ assert_equal 1, page.pages
10
+
11
+ page = FreshBooks::Page.new(1, 2, 3)
12
+ assert_equal 2, page.pages
13
+
14
+ page = FreshBooks::Page.new(1, 2, 4)
15
+ assert_equal 2, page.pages
16
+ end
17
+
18
+ def test_next_page?
19
+ list_proxy = FreshBooks::Page.new(1, 2, 1)
20
+ assert !list_proxy.next_page?
21
+
22
+ list_proxy = FreshBooks::Page.new(1, 2, 3)
23
+ assert list_proxy.next_page?
24
+
25
+ list_proxy = FreshBooks::Page.new(2, 2, 3)
26
+ assert !list_proxy.next_page?
27
+ end
28
+
29
+ def test_page_number
30
+ list_proxy = FreshBooks::Page.new(1, 2, 1)
31
+ assert_equal 1, list_proxy.page_number(0)
32
+
33
+ list_proxy = FreshBooks::Page.new(1, 2, 3)
34
+ assert_equal 1, list_proxy.page_number(1)
35
+
36
+ list_proxy = FreshBooks::Page.new(1, 2, 3)
37
+ assert_equal 2, list_proxy.page_number(2)
38
+ end
39
+
40
+ def test_position_number
41
+ list_proxy = FreshBooks::Page.new(1, 2, 1)
42
+ assert_equal 0, list_proxy.position_number(0)
43
+
44
+ list_proxy = FreshBooks::Page.new(1, 2, 3)
45
+ assert_equal 1, list_proxy.position_number(1)
46
+
47
+ list_proxy = FreshBooks::Page.new(2, 2, 3)
48
+ assert_equal 0, list_proxy.position_number(2)
49
+ end
50
+ end
metadata ADDED
@@ -0,0 +1,148 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: freshbooks.rb
3
+ version: !ruby/object:Gem::Version
4
+ version: 3.0.13
5
+ platform: ruby
6
+ authors:
7
+ - Ben Curren
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-10-15 00:00:00 -05:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: activesupport
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: newgem
27
+ type: :development
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 1.5.2
34
+ version:
35
+ - !ruby/object:Gem::Dependency
36
+ name: mocha
37
+ type: :development
38
+ version_requirement:
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: 0.9.4
44
+ version:
45
+ - !ruby/object:Gem::Dependency
46
+ name: hoe
47
+ type: :development
48
+ version_requirement:
49
+ version_requirements: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: 2.3.3
54
+ version:
55
+ description: ""
56
+ email:
57
+ - ben@outright.com
58
+ executables: []
59
+
60
+ extensions: []
61
+
62
+ extra_rdoc_files:
63
+ - History.txt
64
+ - Manifest.txt
65
+ files:
66
+ - History.txt
67
+ - LICENSE
68
+ - Manifest.txt
69
+ - README
70
+ - Rakefile
71
+ - lib/freshbooks.rb
72
+ - lib/freshbooks/base.rb
73
+ - lib/freshbooks/category.rb
74
+ - lib/freshbooks/client.rb
75
+ - lib/freshbooks/connection.rb
76
+ - lib/freshbooks/estimate.rb
77
+ - lib/freshbooks/expense.rb
78
+ - lib/freshbooks/invoice.rb
79
+ - lib/freshbooks/item.rb
80
+ - lib/freshbooks/line.rb
81
+ - lib/freshbooks/links.rb
82
+ - lib/freshbooks/list_proxy.rb
83
+ - lib/freshbooks/payment.rb
84
+ - lib/freshbooks/project.rb
85
+ - lib/freshbooks/recurring.rb
86
+ - lib/freshbooks/response.rb
87
+ - lib/freshbooks/schema/definition.rb
88
+ - lib/freshbooks/schema/mixin.rb
89
+ - lib/freshbooks/staff.rb
90
+ - lib/freshbooks/task.rb
91
+ - lib/freshbooks/time_entry.rb
92
+ - lib/freshbooks/xml_serializer.rb
93
+ - lib/freshbooks/xml_serializer/serializers.rb
94
+ - script/console
95
+ - script/destroy
96
+ - script/generate
97
+ - test/fixtures/freshbooks_credentials.sample.yml
98
+ - test/fixtures/invoice_create_response.xml
99
+ - test/fixtures/invoice_get_response.xml
100
+ - test/fixtures/invoice_list_response.xml
101
+ - test/fixtures/success_response.xml
102
+ - test/mock_connection.rb
103
+ - test/schema/test_definition.rb
104
+ - test/schema/test_mixin.rb
105
+ - test/test_base.rb
106
+ - test/test_connection.rb
107
+ - test/test_helper.rb
108
+ - test/test_invoice.rb
109
+ - test/test_list_proxy.rb
110
+ - test/test_page.rb
111
+ has_rdoc: true
112
+ homepage:
113
+ licenses: []
114
+
115
+ post_install_message:
116
+ rdoc_options:
117
+ - --main
118
+ - README.txt
119
+ require_paths:
120
+ - lib
121
+ required_ruby_version: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - ">="
124
+ - !ruby/object:Gem::Version
125
+ version: "0"
126
+ version:
127
+ required_rubygems_version: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: "0"
132
+ version:
133
+ requirements: []
134
+
135
+ rubyforge_project: freshbooks.rb
136
+ rubygems_version: 1.3.5
137
+ signing_key:
138
+ specification_version: 3
139
+ summary: ""
140
+ test_files:
141
+ - test/test_connection.rb
142
+ - test/test_list_proxy.rb
143
+ - test/test_page.rb
144
+ - test/test_invoice.rb
145
+ - test/schema/test_mixin.rb
146
+ - test/schema/test_definition.rb
147
+ - test/test_helper.rb
148
+ - test/test_base.rb