mirror42-freshbooks.rb 3.0.25

Sign up to get free protection for your applications and to get access to all the features.
Files changed (47) hide show
  1. checksums.yaml +15 -0
  2. data/History.txt +8 -0
  3. data/LICENSE +10 -0
  4. data/Manifest.txt +45 -0
  5. data/README +58 -0
  6. data/Rakefile +27 -0
  7. data/lib/freshbooks.rb +95 -0
  8. data/lib/freshbooks/base.rb +176 -0
  9. data/lib/freshbooks/category.rb +11 -0
  10. data/lib/freshbooks/client.rb +23 -0
  11. data/lib/freshbooks/connection.rb +162 -0
  12. data/lib/freshbooks/estimate.rb +15 -0
  13. data/lib/freshbooks/expense.rb +12 -0
  14. data/lib/freshbooks/invoice.rb +22 -0
  15. data/lib/freshbooks/item.rb +11 -0
  16. data/lib/freshbooks/line.rb +11 -0
  17. data/lib/freshbooks/links.rb +7 -0
  18. data/lib/freshbooks/list_proxy.rb +80 -0
  19. data/lib/freshbooks/payment.rb +13 -0
  20. data/lib/freshbooks/project.rb +12 -0
  21. data/lib/freshbooks/recurring.rb +16 -0
  22. data/lib/freshbooks/response.rb +25 -0
  23. data/lib/freshbooks/schema/definition.rb +20 -0
  24. data/lib/freshbooks/schema/mixin.rb +40 -0
  25. data/lib/freshbooks/staff.rb +13 -0
  26. data/lib/freshbooks/task.rb +12 -0
  27. data/lib/freshbooks/time_entry.rb +12 -0
  28. data/lib/freshbooks/xml_serializer.rb +17 -0
  29. data/lib/freshbooks/xml_serializer/serializers.rb +116 -0
  30. data/script/console +10 -0
  31. data/script/destroy +14 -0
  32. data/script/generate +14 -0
  33. data/test/fixtures/freshbooks_credentials.sample.yml +3 -0
  34. data/test/fixtures/invoice_create_response.xml +4 -0
  35. data/test/fixtures/invoice_get_response.xml +54 -0
  36. data/test/fixtures/invoice_list_response.xml +109 -0
  37. data/test/fixtures/success_response.xml +2 -0
  38. data/test/mock_connection.rb +13 -0
  39. data/test/schema/test_definition.rb +36 -0
  40. data/test/schema/test_mixin.rb +39 -0
  41. data/test/test_base.rb +151 -0
  42. data/test/test_connection.rb +145 -0
  43. data/test/test_helper.rb +48 -0
  44. data/test/test_invoice.rb +125 -0
  45. data/test/test_list_proxy.rb +60 -0
  46. data/test/test_page.rb +50 -0
  47. metadata +157 -0
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ ODA5OGFhN2IzOTA3YjU0NDMwNDQ4Nzc1MzA3YTRiODI1NTg1MzZjMQ==
5
+ data.tar.gz: !binary |-
6
+ M2RlMjM1MjI4NzVkZDM2ZGQzZDFiMDE1NGJjYmZkZmM2N2JmNTQwMQ==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ ZGU3MzA3MmQ3N2U4MjIyMDMzYzU2Mzc1M2Y3YzZhOTYxNzBjMDIzZGNhNTI4
10
+ OGIxY2Q2MmY4NGViOTBjZjRjOTlmMGFhYWFiNDYzMmYxZmU0NDU0YWYzOWMy
11
+ MzBmMTg5N2RiZWI0MDJkODBkM2ZlNmE3OWIzMzdhNGI2OTM0ZTA=
12
+ data.tar.gz: !binary |-
13
+ YTA5MTM1NjZlNTMyY2UxNDhjZmQyMTQ4NzEyZWZmNTMzZWM2ZWM4NjM1ZDlh
14
+ YjE3M2JhNjg0MGJkN2YwNjNjNGE2ZDkyMTEyZDZhNDY4YzBiODhiMmFhOTE4
15
+ ZDYwNWFmODE0YzZhOWQ4YzcyNzkzYmRjZWU2ODFlODExMzA1YmY=
@@ -0,0 +1,8 @@
1
+ == 0.0.1 2009-02-25
2
+
3
+ * 3.0 major enhancement:
4
+ * Create connection class
5
+ * Create gem
6
+ * Convert xml to ruby type: date, date_time, boolean
7
+ * Added links to objects
8
+ * Create DSL to describe FreshBooks actions
data/LICENSE ADDED
@@ -0,0 +1,10 @@
1
+ = License
2
+
3
+ Copyright (c) 2007 Ben Vinegar
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
10
+
@@ -0,0 +1,45 @@
1
+ History.txt
2
+ LICENSE
3
+ Manifest.txt
4
+ README
5
+ Rakefile
6
+ lib/freshbooks.rb
7
+ lib/freshbooks/base.rb
8
+ lib/freshbooks/category.rb
9
+ lib/freshbooks/client.rb
10
+ lib/freshbooks/connection.rb
11
+ lib/freshbooks/estimate.rb
12
+ lib/freshbooks/expense.rb
13
+ lib/freshbooks/invoice.rb
14
+ lib/freshbooks/item.rb
15
+ lib/freshbooks/line.rb
16
+ lib/freshbooks/links.rb
17
+ lib/freshbooks/list_proxy.rb
18
+ lib/freshbooks/payment.rb
19
+ lib/freshbooks/project.rb
20
+ lib/freshbooks/recurring.rb
21
+ lib/freshbooks/response.rb
22
+ lib/freshbooks/schema/definition.rb
23
+ lib/freshbooks/schema/mixin.rb
24
+ lib/freshbooks/staff.rb
25
+ lib/freshbooks/task.rb
26
+ lib/freshbooks/time_entry.rb
27
+ lib/freshbooks/xml_serializer.rb
28
+ lib/freshbooks/xml_serializer/serializers.rb
29
+ script/console
30
+ script/destroy
31
+ script/generate
32
+ test/fixtures/freshbooks_credentials.sample.yml
33
+ test/fixtures/invoice_create_response.xml
34
+ test/fixtures/invoice_get_response.xml
35
+ test/fixtures/invoice_list_response.xml
36
+ test/fixtures/success_response.xml
37
+ test/mock_connection.rb
38
+ test/schema/test_definition.rb
39
+ test/schema/test_mixin.rb
40
+ test/test_base.rb
41
+ test/test_connection.rb
42
+ test/test_helper.rb
43
+ test/test_invoice.rb
44
+ test/test_list_proxy.rb
45
+ test/test_page.rb
data/README ADDED
@@ -0,0 +1,58 @@
1
+ = Todo
2
+
3
+ Please send me a message if you are interested in contributing. Here is a quick overview of some things I'd like to add to the gem.
4
+
5
+ Merge in forked versions of this gem (any idea what forks are worth while?)
6
+ Provide a way to retrieve the error message with actions beside list are called.
7
+ Make ListProxy optional on list actions. Some people have valid reason to deal with pagination.
8
+ Add oAuth
9
+ Add webhooks
10
+ More tests including integration tests
11
+ Write some documentation and examples
12
+ Launch an official 3.0 (maybe call it 4.0) version of the gem
13
+ Post the official version to rubygems
14
+ Ensure rubygems aren't required for this gem.
15
+
16
+ = About
17
+
18
+ FreshBooks.rb is a Ruby interface to the FreshBooks API. It exposes easy-to-use classes and methods for interacting with your FreshBooks account.
19
+
20
+ NOTE: These examples are out of date and need to be updated. I will be writing documentation for all the updates soon and will be pushing the changes to rubyforge in the near future.
21
+
22
+ = Examples
23
+
24
+ Initialization:
25
+
26
+ FreshBooks::Base.establish_connection('sample.freshbooks.com', 'mytoken')
27
+
28
+ Updating a client name:
29
+
30
+ clients = FreshBooks::Client.list
31
+ client = clients[0]
32
+ client.first_name = 'Suzy'
33
+ client.update
34
+
35
+ Updating an invoice:
36
+
37
+ invoice = FreshBooks::Invoice.get(4)
38
+ invoice.lines[0].quantity += 1
39
+ invoice.update
40
+
41
+ Creating a new item
42
+
43
+ item = FreshBooks::Item.new
44
+ item.name = 'A sample item'
45
+ item.create
46
+
47
+ = License
48
+
49
+ This work is distributed under the MIT License. Use/modify the code however you like.
50
+
51
+ = Download
52
+
53
+ sudo gem install bcurren-freshbooks.rb
54
+
55
+ = Credits
56
+
57
+ FreshBooks.rb is written and maintained by Ben Curren at Outright.com. Ben Vinegar was the original developer and we have taken over maintenance of the gem from now on.
58
+
@@ -0,0 +1,27 @@
1
+ %w[rubygems rake rake/clean fileutils newgem rubigen hoe].each { |f| require f }
2
+ require File.dirname(__FILE__) + '/lib/freshbooks'
3
+
4
+ # Generate all the Rake tasks
5
+ # Run 'rake -T' to see list of generated tasks (from gem root directory)
6
+ $hoe = Hoe.spec('freshbooks.rb') do |p|
7
+ p.developer('Ben Curren', 'ben@outright.com')
8
+ p.summary = ''
9
+ p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n")
10
+ p.rubyforge_name = p.name # TODO this is default value
11
+ p.extra_deps = [ ['activesupport', '>= 3.0.0'] ]
12
+ p.extra_dev_deps = [
13
+ ['newgem', ">= #{::Newgem::VERSION}"],
14
+ ['mocha', ">= 0.9.4"]
15
+ ]
16
+
17
+ p.clean_globs |= %w[**/.DS_Store tmp *.log]
18
+ path = (p.rubyforge_name == p.name) ? p.rubyforge_name : "\#{p.rubyforge_name}/\#{p.name}"
19
+ p.remote_rdoc_dir = File.join(path.gsub(/^#{p.rubyforge_name}\/?/,''), 'rdoc')
20
+ p.rsync_args = '-av --delete --ignore-errors'
21
+ end
22
+
23
+ require 'newgem/tasks' # load /tasks/*.rake
24
+ Dir['tasks/**/*.rake'].each { |t| load t }
25
+
26
+ # TODO - want other tests/tasks run by default? Add them to the list
27
+ # task :default => [:spec, :features]
@@ -0,0 +1,95 @@
1
+ $:.unshift(File.dirname(__FILE__)) unless
2
+ $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
3
+
4
+ begin
5
+ require 'active_support'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ gem 'activesupport'
9
+ require 'active_support'
10
+ end
11
+ require 'active_support/all'
12
+
13
+ require 'freshbooks/base'
14
+ require 'freshbooks/category'
15
+ require 'freshbooks/client'
16
+ require 'freshbooks/connection'
17
+ require 'freshbooks/estimate'
18
+ require 'freshbooks/expense'
19
+ require 'freshbooks/invoice'
20
+ require 'freshbooks/item'
21
+ require 'freshbooks/line'
22
+ require 'freshbooks/links'
23
+ require 'freshbooks/list_proxy'
24
+ require 'freshbooks/payment'
25
+ require 'freshbooks/project'
26
+ require 'freshbooks/recurring'
27
+ require 'freshbooks/response'
28
+ require 'freshbooks/staff'
29
+ require 'freshbooks/task'
30
+ require 'freshbooks/time_entry'
31
+
32
+ require 'net/https'
33
+ require 'rexml/document'
34
+ require 'logger'
35
+
36
+ #------------------------------------------------------------------------------
37
+ # FreshBooks.rb - Ruby interface to the FreshBooks API
38
+ #
39
+ # Copyright (c) 2007-2008 Ben Vinegar (http://www.benlog.org)
40
+ #
41
+ # This work is distributed under an MIT License:
42
+ # http://www.opensource.org/licenses/mit-license.php
43
+ #
44
+ #------------------------------------------------------------------------------
45
+ # Usage:
46
+ #
47
+ # FreshBooks.setup('sample.freshbooks.com', 'mytoken')
48
+ #
49
+ # clients = FreshBooks::Client.list
50
+ # client = clients[0]
51
+ # client.first_name = 'Suzy'
52
+ # client.update
53
+ #
54
+ # invoice = FreshBooks::Invoice.get(4)
55
+ # invoice.lines[0].quantity += 1
56
+ # invoice.update
57
+ #
58
+ # item = FreshBooks::Item.new
59
+ # item.name = 'A sample item'
60
+ # item.create
61
+ #
62
+ #==============================================================================
63
+ module FreshBooks
64
+ VERSION = '3.0.24' # Gem version
65
+ API_VERSION = '2.1' # FreshBooks API version
66
+ SERVICE_URL = "/api/#{API_VERSION}/xml-in"
67
+
68
+ class Error < StandardError; end;
69
+ class InternalError < Error; end;
70
+ class AuthenticationError < Error; end;
71
+ class UnknownSystemError < Error; end;
72
+ class InvalidParameterError < Error; end;
73
+ class ApiAccessNotEnabledError < Error; end;
74
+ class InvalidAccountUrlError < Error; end;
75
+ class AccountDeactivatedError < Error; end;
76
+
77
+ class ParseError < StandardError
78
+ attr_accessor :original_error, :xml
79
+
80
+ def initialize(original_error, xml, msg = nil)
81
+ @original_error = original_error
82
+ @xml = xml
83
+ super(msg)
84
+ end
85
+
86
+ def to_s
87
+ message = super
88
+
89
+ "Original Error: #{original_error.to_s}\n" +
90
+ "XML: #{xml.to_s}\n" +
91
+ "Message: #{message}\n"
92
+ end
93
+ end
94
+ end
95
+
@@ -0,0 +1,176 @@
1
+ require 'freshbooks/schema/mixin'
2
+ require 'freshbooks/xml_serializer'
3
+
4
+ module FreshBooks
5
+ class Base
6
+ include FreshBooks::Schema::Mixin
7
+
8
+ def initialize(args = {})
9
+ args.each_pair {|k, v| send("#{k}=", v)}
10
+ end
11
+
12
+ @@connection = nil
13
+ def self.connection
14
+ @@connection
15
+ end
16
+
17
+ def self.establish_connection(account_url, auth_token, request_headers = {})
18
+ @@connection = Connection.new(account_url, auth_token, request_headers)
19
+ end
20
+
21
+ def self.new_from_xml(xml_root)
22
+ object = self.new()
23
+
24
+ self.schema_definition.members.each do |member_name, member_options|
25
+ node = xml_root.elements[member_name.dup]
26
+ next if node.nil?
27
+
28
+ value = FreshBooks::XmlSerializer.to_value(node, member_options[:type])
29
+ object.send("#{member_name}=", value)
30
+ end
31
+
32
+ return object
33
+
34
+ rescue => e
35
+ error = ParseError.new(e, xml_root.to_s)
36
+ error.set_backtrace(e.backtrace)
37
+ raise error
38
+ # raise ParseError.new(e, xml_root.to_s)
39
+ end
40
+
41
+ def to_xml(elem_name = nil)
42
+ # The root element is the class name underscored
43
+ elem_name ||= self.class.to_s.split('::').last.underscore
44
+ root = REXML::Element.new(elem_name)
45
+
46
+ # Add each member to the root elem
47
+ self.schema_definition.members.each do |member_name, member_options|
48
+ value = self.send(member_name)
49
+ next if member_options[:read_only] || value.nil?
50
+
51
+ element = FreshBooks::XmlSerializer.to_node(member_name, value, member_options[:type])
52
+ root.add_element(element) if element != nil
53
+ end
54
+
55
+ root.to_s
56
+ end
57
+
58
+ def primary_key
59
+ "#{self.class.api_class_name}_id"
60
+ end
61
+
62
+ def primary_key_value
63
+ send(primary_key)
64
+ end
65
+
66
+ def primary_key_value=(value)
67
+ send("#{primary_key}=", value)
68
+ end
69
+
70
+ def self.api_class_name
71
+ klass = class_of_freshbooks_base_descendant(self)
72
+
73
+ # Remove module, underscore between words, lowercase
74
+ klass.name.
75
+ gsub(/^.*::/, "").
76
+ gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
77
+ gsub(/([a-z\d])([A-Z])/,'\1_\2').
78
+ downcase
79
+ end
80
+
81
+ def self.class_of_freshbooks_base_descendant(klass)
82
+ if klass.superclass == Base
83
+ klass
84
+ elsif klass.superclass.nil?
85
+ raise "#{name} doesn't belong in a hierarchy descending from ActiveRecord"
86
+ else
87
+ self.class_of_freshbooks_base_descendant(klass.superclass)
88
+ end
89
+ end
90
+
91
+ def self.define_class_method(symbol, &block)
92
+ self.class.send(:define_method, symbol, &block)
93
+ end
94
+
95
+
96
+ def self.actions(*operations)
97
+ operations.each do |operation|
98
+ method_name = operation.to_s
99
+ api_action_name = method_name.camelize(:lower)
100
+
101
+ case method_name
102
+ when "list"
103
+ define_class_method(method_name) do |*args|
104
+ args << {} if args.empty? # first param is optional and default to empty hash
105
+ api_list_action(api_action_name, *args)
106
+ end
107
+ when "get"
108
+ define_class_method(method_name) do |object_id|
109
+ api_get_action(api_action_name, object_id)
110
+ end
111
+ when "create"
112
+ define_method(method_name) do
113
+ api_create_action(api_action_name)
114
+ end
115
+ when "update"
116
+ define_method(method_name) do
117
+ api_update_action(api_action_name)
118
+ end
119
+ else
120
+ define_method(method_name) do
121
+ api_action(api_action_name)
122
+ end
123
+ end
124
+ end
125
+ end
126
+
127
+ def self.api_list_action(action_name, options = {})
128
+ # Create the proc for the list proxy to retrieve the next page
129
+ list_page_proc = proc do |page|
130
+ options["page"] = page
131
+ response = FreshBooks::Base.connection.call_api("#{api_class_name}.#{action_name}", options)
132
+
133
+ raise FreshBooks::InternalError.new(response.error_msg) unless response.success?
134
+
135
+ root = response.elements[1]
136
+ array = root.elements.map { |item| self.new_from_xml(item) }
137
+
138
+ current_page = Page.new(root.attributes['page'], root.attributes['per_page'], root.attributes['total'], array.size)
139
+
140
+ [array, current_page]
141
+ end
142
+
143
+ ListProxy.new(list_page_proc)
144
+ end
145
+
146
+ def self.api_get_action(action_name, object_id)
147
+ response = FreshBooks::Base.connection.call_api(
148
+ "#{api_class_name}.#{action_name}",
149
+ "#{api_class_name}_id" => object_id)
150
+ response.success? ? self.new_from_xml(response.elements[1]) : nil
151
+ end
152
+
153
+ def api_action(action_name)
154
+ response = FreshBooks::Base.connection.call_api(
155
+ "#{self.class.api_class_name}.#{action_name}",
156
+ "#{self.class.api_class_name}_id" => primary_key_value)
157
+ response.success?
158
+ end
159
+
160
+ def api_create_action(action_name)
161
+ response = FreshBooks::Base.connection.call_api(
162
+ "#{self.class.api_class_name}.#{action_name}",
163
+ self.class.api_class_name => self)
164
+ self.primary_key_value = response.elements[1].text.to_i if response.success?
165
+ response.success?
166
+ end
167
+
168
+ def api_update_action(action_name)
169
+ response = FreshBooks::Base.connection.call_api(
170
+ "#{self.class.api_class_name}.#{action_name}",
171
+ self.class.api_class_name => self)
172
+ response.success?
173
+ end
174
+
175
+ end
176
+ end
@@ -0,0 +1,11 @@
1
+ module FreshBooks
2
+ class Category < FreshBooks::Base
3
+ define_schema do |s|
4
+ s.fixnum :category_id
5
+ s.float :tax1, :tax2
6
+ s.string :name
7
+ end
8
+
9
+ actions :list, :get, :create, :update, :delete
10
+ end
11
+ end