jpablobr-freshbooks 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +5 -0
- data/History.txt +8 -0
- data/LICENSE +10 -0
- data/Manifest.txt +44 -0
- data/README +65 -0
- data/Rakefile +19 -0
- data/lib/freshbooks.rb +94 -0
- data/lib/freshbooks/base.rb +168 -0
- data/lib/freshbooks/category.rb +11 -0
- data/lib/freshbooks/client.rb +20 -0
- data/lib/freshbooks/connection.rb +112 -0
- data/lib/freshbooks/estimate.rb +23 -0
- data/lib/freshbooks/expense.rb +12 -0
- data/lib/freshbooks/invoice.rb +25 -0
- data/lib/freshbooks/item.rb +11 -0
- data/lib/freshbooks/line.rb +10 -0
- data/lib/freshbooks/links.rb +7 -0
- data/lib/freshbooks/list_proxy.rb +70 -0
- data/lib/freshbooks/payment.rb +12 -0
- data/lib/freshbooks/project.rb +12 -0
- data/lib/freshbooks/recurring.rb +15 -0
- data/lib/freshbooks/response.rb +27 -0
- data/lib/freshbooks/schema/definition.rb +20 -0
- data/lib/freshbooks/schema/mixin.rb +40 -0
- data/lib/freshbooks/staff.rb +13 -0
- data/lib/freshbooks/task.rb +12 -0
- data/lib/freshbooks/time_entry.rb +12 -0
- data/lib/freshbooks/xml_serializer.rb +17 -0
- data/lib/freshbooks/xml_serializer/serializers.rb +107 -0
- data/script/console +10 -0
- data/script/destroy +14 -0
- data/script/generate +14 -0
- data/test/fixtures/invoice_create_response.xml +4 -0
- data/test/fixtures/invoice_get_response.xml +51 -0
- data/test/fixtures/invoice_list_response.xml +27 -0
- data/test/fixtures/success_response.xml +2 -0
- data/test/mock_connection.rb +13 -0
- data/test/schema/test_definition.rb +36 -0
- data/test/schema/test_mixin.rb +39 -0
- data/test/test_base.rb +97 -0
- data/test/test_connection.rb +83 -0
- data/test/test_helper.rb +32 -0
- data/test/test_invoice.rb +122 -0
- data/test/test_list_proxy.rb +41 -0
- data/test/test_page.rb +50 -0
- metadata +115 -0
data/History.txt
ADDED
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
|
+
|
data/Manifest.txt
ADDED
@@ -0,0 +1,44 @@
|
|
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/invoice_create_response.xml
|
33
|
+
test/fixtures/invoice_get_response.xml
|
34
|
+
test/fixtures/invoice_list_response.xml
|
35
|
+
test/fixtures/success_response.xml
|
36
|
+
test/mock_connection.rb
|
37
|
+
test/schema/test_definition.rb
|
38
|
+
test/schema/test_mixin.rb
|
39
|
+
test/test_base.rb
|
40
|
+
test/test_connection.rb
|
41
|
+
test/test_helper.rb
|
42
|
+
test/test_invoice.rb
|
43
|
+
test/test_list_proxy.rb
|
44
|
+
test/test_page.rb
|
data/README
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
= About
|
2
|
+
|
3
|
+
FreshBooks.rb is a Ruby interface to the FreshBooks API. It exposes easy-to-use classes and methods for interacting with your FreshBooks account. Read more on ELC's blog:
|
4
|
+
http://elctech.com/tags/freshbooks
|
5
|
+
|
6
|
+
= Examples
|
7
|
+
|
8
|
+
Installation:
|
9
|
+
gem install elc-freshbooks.rb --source http://gems.github.com
|
10
|
+
|
11
|
+
Initialization:
|
12
|
+
|
13
|
+
FreshBooks::Base.establish_connection( 'sample.freshbooks.com', 'mytoken' )
|
14
|
+
|
15
|
+
Updating a client name:
|
16
|
+
|
17
|
+
clients = FreshBooks::Client.list
|
18
|
+
client = clients[0]
|
19
|
+
client.first_name = 'Suzy'
|
20
|
+
client.update
|
21
|
+
|
22
|
+
Updating an invoice:
|
23
|
+
|
24
|
+
invoice = FreshBooks::Invoice.get(4)
|
25
|
+
invoice.lines[0].quantity += 1
|
26
|
+
invoice.update
|
27
|
+
|
28
|
+
Creating a new item
|
29
|
+
|
30
|
+
item = FreshBooks::Item.new
|
31
|
+
item.name = 'A sample item'
|
32
|
+
item.create
|
33
|
+
|
34
|
+
Getting an invoice pdf:
|
35
|
+
|
36
|
+
invoice = FreshBooks::Invoice.get(4)
|
37
|
+
original_status = invoice.status
|
38
|
+
invoice.status = "sent"
|
39
|
+
raise "API Change or error" unless invoice.update
|
40
|
+
html_url = invoice.client_view
|
41
|
+
cookiejar = Tempfile.new("cookies")
|
42
|
+
pdf_file = Tempfile.new("pdf")
|
43
|
+
html_doc = `wget "#{html_url}" --no-check-certificate --cookies=on --keep-session-cookies --save-cookies='#{cookiejar.path}' -O -`
|
44
|
+
`wget 'https://#{freshbooks_api_host}/getPDF.php' --post-data='invoice_id[]=#{invoice.number}' --cookies=on --keep-session-cookies
|
45
|
+
--load-cookies='#{cookiejar.path}' --no-check-certificate -O '#{pdf_file.path}'"`
|
46
|
+
invoice.status = original_status
|
47
|
+
raise "API Change or error" unless invoice.update
|
48
|
+
pdf_file.path # Here's the PDF!
|
49
|
+
|
50
|
+
= License
|
51
|
+
|
52
|
+
This work is distributed under the MIT License. Use/modify the code however you like.
|
53
|
+
|
54
|
+
= Download
|
55
|
+
|
56
|
+
FreshBooks.rb is distributed as a gem via Rubyforge. The easiest way to install it is like so:
|
57
|
+
|
58
|
+
gem install freshbooks
|
59
|
+
|
60
|
+
Alternatively, you can download it from the Rubyforge project page.
|
61
|
+
|
62
|
+
= Credits
|
63
|
+
|
64
|
+
FreshBooks.rb is written and maintained by Ben Vinegar, with contributions from Flinn Meuller, Kenneth Kalmer, and others.
|
65
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gemspec|
|
7
|
+
gemspec.name = "jpablobr-freshbooks"
|
8
|
+
gemspec.summary = "jpablobr-freshBooks is a Ruby interface to the FreshBooks API."
|
9
|
+
gemspec.description = "jpablobr-freshBooks is a Ruby interface to the FreshBooks API. It exposes easy-to-use classes and methods for interacting with your FreshBooks account."
|
10
|
+
gemspec.email = "xjpablobrx@gmail.com"
|
11
|
+
gemspec.homepage = "http://github.com/jpablobr/freshbooks.rb"
|
12
|
+
gemspec.authors = ["Jose Pablo Barrantes"]
|
13
|
+
end
|
14
|
+
Jeweler::GemcutterTasks.new
|
15
|
+
rescue LoadError
|
16
|
+
puts "Jeweler not available. Install it with: sudo gem install jeweler -s http://gemcutter.org"
|
17
|
+
end
|
18
|
+
|
19
|
+
Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each { |ext| load ext }
|
data/lib/freshbooks.rb
ADDED
@@ -0,0 +1,94 @@
|
|
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
|
+
|
12
|
+
require 'freshbooks/base'
|
13
|
+
require 'freshbooks/category'
|
14
|
+
require 'freshbooks/client'
|
15
|
+
require 'freshbooks/connection'
|
16
|
+
require 'freshbooks/estimate'
|
17
|
+
require 'freshbooks/expense'
|
18
|
+
require 'freshbooks/invoice'
|
19
|
+
require 'freshbooks/item'
|
20
|
+
require 'freshbooks/line'
|
21
|
+
require 'freshbooks/links'
|
22
|
+
require 'freshbooks/list_proxy'
|
23
|
+
require 'freshbooks/payment'
|
24
|
+
require 'freshbooks/project'
|
25
|
+
require 'freshbooks/recurring'
|
26
|
+
require 'freshbooks/response'
|
27
|
+
require 'freshbooks/staff'
|
28
|
+
require 'freshbooks/task'
|
29
|
+
require 'freshbooks/time_entry'
|
30
|
+
|
31
|
+
require 'net/https'
|
32
|
+
require 'rexml/document'
|
33
|
+
require 'logger'
|
34
|
+
|
35
|
+
#------------------------------------------------------------------------------
|
36
|
+
# FreshBooks.rb - Ruby interface to the FreshBooks API
|
37
|
+
#
|
38
|
+
# Copyright (c) 2007-2008 Ben Vinegar (http://www.benlog.org)
|
39
|
+
#
|
40
|
+
# This work is distributed under an MIT License:
|
41
|
+
# http://www.opensource.org/licenses/mit-license.php
|
42
|
+
#
|
43
|
+
#------------------------------------------------------------------------------
|
44
|
+
# Usage:
|
45
|
+
#
|
46
|
+
# FreshBooks.setup('sample.freshbooks.com', 'mytoken')
|
47
|
+
#
|
48
|
+
# clients = FreshBooks::Client.list
|
49
|
+
# client = clients[0]
|
50
|
+
# client.first_name = 'Suzy'
|
51
|
+
# client.update
|
52
|
+
#
|
53
|
+
# invoice = FreshBooks::Invoice.get(4)
|
54
|
+
# invoice.lines[0].quantity += 1
|
55
|
+
# invoice.update
|
56
|
+
#
|
57
|
+
# item = FreshBooks::Item.new
|
58
|
+
# item.name = 'A sample item'
|
59
|
+
# item.create
|
60
|
+
#
|
61
|
+
#==============================================================================
|
62
|
+
module FreshBooks
|
63
|
+
VERSION = '3.0.6.1' # Gem version
|
64
|
+
API_VERSION = '2.1' # FreshBooks API version
|
65
|
+
SERVICE_URL = "/api/#{API_VERSION}/xml-in"
|
66
|
+
|
67
|
+
class Error < StandardError; end;
|
68
|
+
class InternalError < Error; end;
|
69
|
+
class AuthenticationError < Error; end;
|
70
|
+
class UnknownSystemError < Error; end;
|
71
|
+
class InvalidParameterError < Error; end;
|
72
|
+
class ApiAccessNotEnabledError < Error; end;
|
73
|
+
class InvalidAccountUrlError < Error; end;
|
74
|
+
class AccountDeactivatedError < Error; end;
|
75
|
+
|
76
|
+
class ParseError < StandardError
|
77
|
+
attr_accessor :original_error, :xml
|
78
|
+
|
79
|
+
def initialize(original_error, xml, msg = nil)
|
80
|
+
@original_error = original_error
|
81
|
+
@xml = xml
|
82
|
+
super(msg)
|
83
|
+
end
|
84
|
+
|
85
|
+
def to_s
|
86
|
+
message = super
|
87
|
+
|
88
|
+
"Original Error: #{original_error.to_s}\n" +
|
89
|
+
"XML: #{xml.to_s}\n" +
|
90
|
+
"Message: #{message}\n"
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
@@ -0,0 +1,168 @@
|
|
1
|
+
require 'freshbooks/schema/mixin'
|
2
|
+
require 'freshbooks/xml_serializer'
|
3
|
+
|
4
|
+
module FreshBooks
|
5
|
+
class Base
|
6
|
+
include FreshBooks::Schema::Mixin
|
7
|
+
|
8
|
+
@@connection = nil
|
9
|
+
def self.connection
|
10
|
+
@@connection
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.establish_connection(account_url, auth_token, request_headers = {})
|
14
|
+
@@connection = Connection.new(account_url, auth_token, request_headers)
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.new_from_xml(xml_root)
|
18
|
+
object = self.new
|
19
|
+
|
20
|
+
self.schema_definition.members.each do |member_name, member_options|
|
21
|
+
node = xml_root.elements[member_name]
|
22
|
+
next if node.nil?
|
23
|
+
|
24
|
+
value = FreshBooks::XmlSerializer.to_value(node, member_options[:type])
|
25
|
+
object.send("#{member_name}=", value)
|
26
|
+
end
|
27
|
+
|
28
|
+
return object
|
29
|
+
|
30
|
+
rescue => e
|
31
|
+
raise ParseError.new(e, xml_root.to_s)
|
32
|
+
end
|
33
|
+
|
34
|
+
def to_xml(elem_name = nil)
|
35
|
+
# The root element is the class name underscored
|
36
|
+
elem_name ||= self.class.to_s.split('::').last.underscore
|
37
|
+
root = REXML::Element.new(elem_name)
|
38
|
+
|
39
|
+
# Add each member to the root elem
|
40
|
+
self.schema_definition.members.each do |member_name, member_options|
|
41
|
+
value = self.send(member_name)
|
42
|
+
next if member_options[:read_only] || value.nil?
|
43
|
+
|
44
|
+
element = FreshBooks::XmlSerializer.to_node(member_name, value, member_options[:type])
|
45
|
+
root.add_element(element) if element != nil
|
46
|
+
end
|
47
|
+
|
48
|
+
root.to_s
|
49
|
+
end
|
50
|
+
|
51
|
+
def primary_key
|
52
|
+
"#{self.class.api_class_name}_id"
|
53
|
+
end
|
54
|
+
|
55
|
+
def primary_key_value
|
56
|
+
send(primary_key)
|
57
|
+
end
|
58
|
+
|
59
|
+
def primary_key_value=(value)
|
60
|
+
send("#{primary_key}=", value)
|
61
|
+
end
|
62
|
+
|
63
|
+
def self.api_class_name
|
64
|
+
klass = class_of_freshbooks_base_descendant(self)
|
65
|
+
|
66
|
+
# Remove module, underscore between words, lowercase
|
67
|
+
klass.name.
|
68
|
+
gsub(/^.*::/, "").
|
69
|
+
gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
|
70
|
+
gsub(/([a-z\d])([A-Z])/,'\1_\2').
|
71
|
+
downcase
|
72
|
+
end
|
73
|
+
|
74
|
+
def self.class_of_freshbooks_base_descendant(klass)
|
75
|
+
if klass.superclass == Base
|
76
|
+
klass
|
77
|
+
elsif klass.superclass.nil?
|
78
|
+
raise "#{name} doesn't belong in a hierarchy descending from ActiveRecord"
|
79
|
+
else
|
80
|
+
self.class_of_freshbooks_base_descendant(klass.superclass)
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
def self.define_class_method(symbol, &block)
|
85
|
+
self.class.send(:define_method, symbol, &block)
|
86
|
+
end
|
87
|
+
|
88
|
+
|
89
|
+
def self.actions(*operations)
|
90
|
+
operations.each do |operation|
|
91
|
+
method_name = operation.to_s
|
92
|
+
api_action_name = method_name.camelize(:lower)
|
93
|
+
|
94
|
+
case method_name
|
95
|
+
when "list"
|
96
|
+
define_class_method(method_name) do |*args|
|
97
|
+
args << HashWithIndifferentAccess.new if args.empty? # first param is optional and default to empty hash
|
98
|
+
api_list_action(api_action_name, *args)
|
99
|
+
end
|
100
|
+
when "get"
|
101
|
+
define_class_method(method_name) do |object_id|
|
102
|
+
api_get_action(api_action_name, object_id)
|
103
|
+
end
|
104
|
+
when "create"
|
105
|
+
define_method(method_name) do
|
106
|
+
api_create_action(api_action_name)
|
107
|
+
end
|
108
|
+
when "update"
|
109
|
+
define_method(method_name) do
|
110
|
+
api_update_action(api_action_name)
|
111
|
+
end
|
112
|
+
else
|
113
|
+
define_method(method_name) do
|
114
|
+
api_action(api_action_name)
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
def self.api_list_action(action_name, options = {})
|
121
|
+
options = HashWithIndifferentAccess.new(options) # NOTE: this is needed so that :page => 10 doesn't end up twice in the options hash (dvd, 10-08-2009)
|
122
|
+
# Create the proc for the list proxy to retrieve the next page
|
123
|
+
list_page_proc = proc do |page|
|
124
|
+
options["page"] = page
|
125
|
+
response = FreshBooks::Base.connection.call_api("#{api_class_name}.#{action_name}", options)
|
126
|
+
raise FreshBooks::InternalError.new("Response was not successful. Error: #{response.error_msg}") unless response.success?
|
127
|
+
|
128
|
+
root = response.elements[1]
|
129
|
+
array = root.elements.map { |item| self.new_from_xml(item) }
|
130
|
+
current_page = Page.new(root.attributes['page'], root.attributes['per_page'], root.attributes['total'])
|
131
|
+
|
132
|
+
[array, current_page]
|
133
|
+
end
|
134
|
+
|
135
|
+
ListProxy.new(list_page_proc)
|
136
|
+
end
|
137
|
+
|
138
|
+
def self.api_get_action(action_name, object_id)
|
139
|
+
response = FreshBooks::Base.connection.call_api(
|
140
|
+
"#{api_class_name}.#{action_name}",
|
141
|
+
"#{api_class_name}_id" => object_id)
|
142
|
+
response.success? ? self.new_from_xml(response.elements[1]) : nil
|
143
|
+
end
|
144
|
+
|
145
|
+
def api_action(action_name)
|
146
|
+
response = FreshBooks::Base.connection.call_api(
|
147
|
+
"#{self.class.api_class_name}.#{action_name}",
|
148
|
+
"#{self.class.api_class_name}_id" => primary_key_value)
|
149
|
+
response.success?
|
150
|
+
end
|
151
|
+
|
152
|
+
def api_create_action(action_name)
|
153
|
+
response = FreshBooks::Base.connection.call_api(
|
154
|
+
"#{self.class.api_class_name}.#{action_name}",
|
155
|
+
self.class.api_class_name => self)
|
156
|
+
self.primary_key_value = response.elements[1].text.to_i if response.success?
|
157
|
+
response.success?
|
158
|
+
end
|
159
|
+
|
160
|
+
def api_update_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
|
+
response.success?
|
165
|
+
end
|
166
|
+
|
167
|
+
end
|
168
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module FreshBooks
|
2
|
+
class Client < FreshBooks::Base
|
3
|
+
define_schema do |s|
|
4
|
+
s.string :first_name, :last_name, :organization, :email
|
5
|
+
s.string :username, :password, :work_phone, :home_phone
|
6
|
+
s.string :mobile, :fax, :notes, :p_street1, :p_street2, :p_city
|
7
|
+
s.string :p_state, :p_country, :p_code, :s_street1, :s_street2
|
8
|
+
s.string :s_city, :s_state, :s_country, :s_code
|
9
|
+
s.fixnum :client_id
|
10
|
+
s.object :links, :read_only => true
|
11
|
+
end
|
12
|
+
|
13
|
+
actions :list, :get, :create, :update, :delete
|
14
|
+
|
15
|
+
def invoices(options = {})
|
16
|
+
options.merge('client_id' => self.client_id)
|
17
|
+
Invoice::list(options)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|