ruby-freshbooks 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +20 -0
- data/README.md +117 -0
- data/VERSION +1 -0
- data/lib/freshbooks.rb +95 -0
- data/ruby-freshbooks.gemspec +35 -0
- data/spec/freshbooks_spec.rb +56 -0
- metadata +109 -0
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2010 Justin Giancola
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,117 @@
|
|
1
|
+
# ruby-freshbooks: a simple FreshBooks API wrapper
|
2
|
+
|
3
|
+
This is a Ruby wrapper for the [FreshBooks](http://www.freshbooks.com) API. This style of API wrapper is typically called "reflective". However, because most web APIs (and in particular, FreshBooks' API) do not provide enough metadata to actually be reflective, I feel a more appropriate term is "isomorphic". i.e. the wrapper translates native (in this case Ruby) data structures to and from some representation (in this case XML) that the API understands.
|
4
|
+
|
5
|
+
For example,
|
6
|
+
|
7
|
+
conn = FreshBooks::Connection.new('youraccount.freshbooks.com', 'yourfreshbooksapitoken')
|
8
|
+
conn.client.get :client_id => 2
|
9
|
+
|
10
|
+
generates the XML:
|
11
|
+
|
12
|
+
<?xml version="1.0" encoding="utf-8"?>
|
13
|
+
<request method="client.get">
|
14
|
+
<client_id>2</client_id>
|
15
|
+
</request>
|
16
|
+
|
17
|
+
purely based on the request arguments. This library doesn't actually know anything about the FreshBooks API. Instead it relies on users to read the [FreshBooks API Documentation](http://developers.freshbooks.com/api) and build Ruby data structures (i.e. nested Hash and Array combos) that have isomorphic structure to intended API XML requests. The transformation is quite simple; once you understand how the mapping works any FreshBooks API request can easily be written as a Ruby data structure.
|
18
|
+
|
19
|
+
## Detailed Ruby Data Structure to XML Isomorphism Example
|
20
|
+
|
21
|
+
The following call will generate and POST the invoice create XML shown in the [FreshBooks API Documentation](http://developers.freshbooks.com/api/view/invoices/):
|
22
|
+
|
23
|
+
conn = FreshBooks::Connection.new('youraccount.freshbooks.com', 'yourfreshbooksapitoken')
|
24
|
+
conn.invoice.create(:invoice => {
|
25
|
+
:client_id => 13,
|
26
|
+
:number => 'FB00004',
|
27
|
+
:status => 'draft',
|
28
|
+
:date => '2007-06-23',
|
29
|
+
:po_number => 2314,
|
30
|
+
:discount => 10,
|
31
|
+
:notes => 'Due upon receipt.',
|
32
|
+
:currency_code => 'CAD',
|
33
|
+
:terms => 'Payment due in 30 days.',
|
34
|
+
:return_uri => 'http://example.com/account',
|
35
|
+
|
36
|
+
:first_name => 'John',
|
37
|
+
:last_name => 'Smith',
|
38
|
+
:organization => 'ABC Corp',
|
39
|
+
:p_street1 => nil,
|
40
|
+
:p_street2 => nil,
|
41
|
+
:p_city => nil,
|
42
|
+
:p_state => nil,
|
43
|
+
:p_country => nil,
|
44
|
+
:p_code => nil,
|
45
|
+
:vat_name => nil,
|
46
|
+
:vat_number => nil,
|
47
|
+
|
48
|
+
:lines => [{ :line => {
|
49
|
+
:name => 'Yard Work',
|
50
|
+
:description => 'Mowed the lawn.',
|
51
|
+
:unit_cost => 10,
|
52
|
+
:quantity => 4,
|
53
|
+
:tax1_name => 'GST',
|
54
|
+
:tax2_name => 'PST',
|
55
|
+
:tax1_percent => 5,
|
56
|
+
:tax2_percent => 8,
|
57
|
+
}}]})
|
58
|
+
|
59
|
+
|
60
|
+
## Examples
|
61
|
+
|
62
|
+
You can call any `#{namespace}.#{method_name}` method chain against a `FreshBooks::Connection` instance and it will POST a request to the corresponding FreshBooks API method. i.e.
|
63
|
+
|
64
|
+
conn = FreshBooks::Connection.new('youraccount.freshbooks.com', 'yourfreshbooksapitoken')
|
65
|
+
conn.client.get :client_id => 37
|
66
|
+
conn.invoice.list :client_id => 37, :page => 2, :per_page => 10
|
67
|
+
|
68
|
+
## Goals
|
69
|
+
|
70
|
+
* easy to use. you can get started using this wrapper in 5 minutes without having to read any documentation
|
71
|
+
* flexible enough to support minor changes to the FreshBooks API without requiring a new release. users can simply refer to the official [FreshBooks API Documentation](http://developers.freshbooks.com/api) to see what they can and can't do. you need not depend on this library's maintainer to map/remove new attributes, methods or namespaces as the FreshBooks API changes
|
72
|
+
|
73
|
+
## Non-goals
|
74
|
+
|
75
|
+
* seamless integration with FreshBooks API via an object interface. i.e.
|
76
|
+
|
77
|
+
<pre><code>clients = FreshBooks::Client.list
|
78
|
+
client = clients.first
|
79
|
+
client.first_name = 'Swenson'
|
80
|
+
client.update
|
81
|
+
</code></pre>
|
82
|
+
|
83
|
+
if you want this sort of thing, please use [freshbooks.rb](http://github.com/bcurren/freshbooks.rb) instead
|
84
|
+
|
85
|
+
## Why Shouldn't I use freshbooks.rb Instead?
|
86
|
+
|
87
|
+
Maybe you should. It depends on what you want to do. I've used freshbooks.rb before but there were a few things that didn't work for me:
|
88
|
+
|
89
|
+
* global connection. I've had the need to connect to multiple FreshBooks accounts within the same program to do things like sync or migrate data. you can't do this with freshbooks.rb because the global connection is owned by `FreshBooks::Base` which is the superclass of `Client`, `Invoice`, etc.
|
90
|
+
* requiring a library update every time the FreshBooks API changes. although this doesn't happen very often, it's a little annoying to have to manually patch freshbooks.rb when it does.
|
91
|
+
* having to convert everything to and from the business objects the library provides. because the freshbooks.rb API is nice and abstract, it's easy to play around with invoices, clients, etc. as business objects. however, this is less convenient for mass import/export type programs because your data has to be pushed through that object interface instead of just transformed into YAML, CSV, etc. it's also less than desirable when your integration makes use of an alternate model class (i.e. an `ActiveRecord` subclass that you're using to save your FreshBooks data into a database).
|
92
|
+
* data transparency. if you're just exploring the FreshBooks API, you might not know all of the attributes that some data types exposes. if you are getting back nicely packaged objects, you'll need to read through the documentation (or source code of freshbooks.rb if you're sure some property ought to be there but isn't and you suspect it's missing from the mapping) to see what you have access to.
|
93
|
+
|
94
|
+
## Installation
|
95
|
+
|
96
|
+
gem install ruby-freshbooks
|
97
|
+
|
98
|
+
|
99
|
+
## Links
|
100
|
+
|
101
|
+
* [FreshBooks API Documentation](http://developers.freshbooks.com/api)
|
102
|
+
* [Source Code](http://github.com/elucid/ruby-freshbooks)
|
103
|
+
|
104
|
+
## If You Want to Contribute:
|
105
|
+
|
106
|
+
1. fork http://github.com/elucid/ruby-freshbooks
|
107
|
+
2. make your changes along with specs (don't touch VERSION)
|
108
|
+
3. send me a pull request
|
109
|
+
|
110
|
+
## TODO
|
111
|
+
|
112
|
+
* more examples
|
113
|
+
* more documentation
|
114
|
+
|
115
|
+
## Copyright
|
116
|
+
|
117
|
+
Copyright (c) 2010 Justin Giancola. See LICENSE for details.
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.1
|
data/lib/freshbooks.rb
ADDED
@@ -0,0 +1,95 @@
|
|
1
|
+
require 'httparty'
|
2
|
+
require 'builder'
|
3
|
+
|
4
|
+
module FreshBooks
|
5
|
+
API_VERSION = '2.1'
|
6
|
+
|
7
|
+
# provides a Hash-like response object with structure
|
8
|
+
# isomorphic to actual xml response, slightly tidied.
|
9
|
+
class Response < Hash
|
10
|
+
attr_reader :status
|
11
|
+
|
12
|
+
def initialize(data)
|
13
|
+
super nil
|
14
|
+
response = data["response"]
|
15
|
+
response.delete "xmlns"
|
16
|
+
@status = response.delete "status"
|
17
|
+
update response
|
18
|
+
end
|
19
|
+
|
20
|
+
def success?
|
21
|
+
status == "ok"
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
# FreshBooks API connection. instances are FreshBooks account
|
26
|
+
# specific so you can, e.g. setup two connections and copy/
|
27
|
+
# sync data between them
|
28
|
+
class Connection
|
29
|
+
include HTTParty
|
30
|
+
|
31
|
+
def initialize(domain, token)
|
32
|
+
@domain = domain
|
33
|
+
@auth = { :username => token, :password => 'X' }
|
34
|
+
end
|
35
|
+
|
36
|
+
def api_url # :nodoc:
|
37
|
+
"https://#{@domain}/api/#{API_VERSION}/xml-in"
|
38
|
+
end
|
39
|
+
|
40
|
+
# HTTParty (sort of) assumes global connections to services
|
41
|
+
# but we can easily avoid that by making an instance method
|
42
|
+
# that knows account-specific details that calls its
|
43
|
+
# coresponding class method.
|
44
|
+
# note: we only need to provide a #post method because the
|
45
|
+
# FreshBooks API is POST only
|
46
|
+
def post(method, params={}) # :nodoc:
|
47
|
+
Response.new self.class.post(api_url,
|
48
|
+
:basic_auth => @auth,
|
49
|
+
:body => self.class.xml_body(method, params))
|
50
|
+
end
|
51
|
+
|
52
|
+
# takes nested Hash/Array combos and generates isomorphic
|
53
|
+
# XML bodies to be POSTed to FreshBooks API
|
54
|
+
def self.xml_body(method, params)
|
55
|
+
xml = Builder::XmlMarkup.new
|
56
|
+
xml.tag!("request", :method => method) do
|
57
|
+
build_xml(params, xml)
|
58
|
+
end
|
59
|
+
xml.target!
|
60
|
+
end
|
61
|
+
|
62
|
+
# helper method to xml_body
|
63
|
+
def self.build_xml(obj, target='')
|
64
|
+
xml = Builder::XmlMarkup.new(:target => target)
|
65
|
+
# ZOMG! haven't you ever heard of polymorphism?!?
|
66
|
+
# of course. I'm simply electing not to pollute the
|
67
|
+
# method space of two of the most common Ruby classes.
|
68
|
+
# besides, what are the chances this library will ever
|
69
|
+
# be used in a context where some other library hasn't
|
70
|
+
# already defined #to_xml on Hash...
|
71
|
+
case obj
|
72
|
+
when Hash : obj.each { |k,v| xml.tag!(k) { build_xml(v, xml) } }
|
73
|
+
when Array : obj.each { |e| build_xml(e ,xml) }
|
74
|
+
else xml.text! obj.to_s
|
75
|
+
end
|
76
|
+
xml.target!
|
77
|
+
end
|
78
|
+
|
79
|
+
# infer API methods based on 2-deep method chains sent to
|
80
|
+
# connections. this allows us to provide a simple interface
|
81
|
+
# without actually knowing anything about the supported API
|
82
|
+
# methods (and hence trusting users to read the official
|
83
|
+
# FreshBooks API documentation)
|
84
|
+
def method_missing(sym, *args) # :nodoc:
|
85
|
+
NamespaceProxy.new self, sym
|
86
|
+
end
|
87
|
+
|
88
|
+
# nothing to see here...
|
89
|
+
class NamespaceProxy < Struct.new(:conn, :namespace) # :nodoc:
|
90
|
+
def method_missing(sym, *args)
|
91
|
+
conn.post "#{namespace}.#{sym}", *args
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = %q{ruby-freshbooks}
|
3
|
+
s.version = File.read(File.join(File.dirname(__FILE__),'VERSION')).chomp
|
4
|
+
|
5
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
|
6
|
+
s.authors = ["Justin Giancola"]
|
7
|
+
s.date = %q{2010-04-25}
|
8
|
+
s.description = %q{simple FreshBooks API wrapper}
|
9
|
+
s.email = %q{elucid@gmail.com}
|
10
|
+
s.files = ["README.md", "LICENSE", "VERSION", "ruby-freshbooks.gemspec", "lib/freshbooks.rb", "spec/freshbooks_spec.rb"]
|
11
|
+
s.has_rdoc = false
|
12
|
+
s.homepage = %q{http://github.com/elucid/ruby-freshbooks}
|
13
|
+
s.require_paths = ["lib"]
|
14
|
+
s.rubygems_version = %q{1.3.4}
|
15
|
+
s.summary = s.description
|
16
|
+
|
17
|
+
if s.respond_to? :specification_version then
|
18
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
19
|
+
s.specification_version = 3
|
20
|
+
|
21
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0')
|
22
|
+
s.add_runtime_dependency(%q<httparty>, [">= 0.5.0"])
|
23
|
+
s.add_runtime_dependency(%q<builder>, [">= 2.1.2"])
|
24
|
+
s.add_development_dependency(%q<rspec>, [">= 1.3.0"])
|
25
|
+
else
|
26
|
+
s.add_dependency(%q<httparty>, [">= 0.5.0"])
|
27
|
+
s.add_dependency(%q<builder>, [">= 2.1.2"])
|
28
|
+
s.add_dependency(%q<rspec>, [">= 1.3.0"])
|
29
|
+
end
|
30
|
+
else
|
31
|
+
s.add_dependency(%q<httparty>, [">= 0.5.0"])
|
32
|
+
s.add_dependency(%q<builder>, [">= 2.1.2"])
|
33
|
+
s.add_dependency(%q<rspec>, [">= 1.3.0"])
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'freshbooks'
|
2
|
+
|
3
|
+
def build_xml(data)
|
4
|
+
FreshBooks::Connection.build_xml data
|
5
|
+
end
|
6
|
+
|
7
|
+
describe "XML generation:" do
|
8
|
+
describe "simple hash" do
|
9
|
+
it "should serialize correctly" do
|
10
|
+
data = {"foo" => "bar"}
|
11
|
+
build_xml(data).should == "<foo>bar</foo>"
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
describe "simple hash with Fixnum value" do
|
16
|
+
it "should serialize correctly, coercing to string" do
|
17
|
+
data = {"foo" => 1}
|
18
|
+
build_xml(data).should == "<foo>1</foo>"
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
describe "simple hash value containing entity" do
|
23
|
+
it "should serialize correctly, escaping entity" do
|
24
|
+
data = {"foo" => "b&r"}
|
25
|
+
build_xml(data).should == "<foo>b&r</foo>"
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
describe "nested hash" do
|
30
|
+
it "should serialize correctly" do
|
31
|
+
data = {"foo" => {"bar" => "baz"}}
|
32
|
+
build_xml(data).should == "<foo><bar>baz</bar></foo>"
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
describe "deeply nested hash" do
|
37
|
+
it "should serialize correctly" do
|
38
|
+
data = {"foo" => {"bar" => {"baz" => "bat"}}}
|
39
|
+
build_xml(data).should == "<foo><bar><baz>bat</baz></bar></foo>"
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
describe "array" do
|
44
|
+
it "should serialize correctly" do
|
45
|
+
data = [{"bar" => "baz"}, {"bar" => "baz"}]
|
46
|
+
build_xml(data).should == "<bar>baz</bar><bar>baz</bar>"
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
describe "hash with array" do
|
51
|
+
it "should serialize correctly" do
|
52
|
+
data = {"foo" => [{"bar" => "baz"}, {"bar" => "baz"}]}
|
53
|
+
build_xml(data).should == "<foo><bar>baz</bar><bar>baz</bar></foo>"
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
metadata
ADDED
@@ -0,0 +1,109 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ruby-freshbooks
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
- 1
|
9
|
+
version: 0.1.1
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Justin Giancola
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-04-25 00:00:00 -04:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: httparty
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
segments:
|
28
|
+
- 0
|
29
|
+
- 5
|
30
|
+
- 0
|
31
|
+
version: 0.5.0
|
32
|
+
type: :runtime
|
33
|
+
version_requirements: *id001
|
34
|
+
- !ruby/object:Gem::Dependency
|
35
|
+
name: builder
|
36
|
+
prerelease: false
|
37
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
segments:
|
42
|
+
- 2
|
43
|
+
- 1
|
44
|
+
- 2
|
45
|
+
version: 2.1.2
|
46
|
+
type: :runtime
|
47
|
+
version_requirements: *id002
|
48
|
+
- !ruby/object:Gem::Dependency
|
49
|
+
name: rspec
|
50
|
+
prerelease: false
|
51
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
segments:
|
56
|
+
- 1
|
57
|
+
- 3
|
58
|
+
- 0
|
59
|
+
version: 1.3.0
|
60
|
+
type: :development
|
61
|
+
version_requirements: *id003
|
62
|
+
description: simple FreshBooks API wrapper
|
63
|
+
email: elucid@gmail.com
|
64
|
+
executables: []
|
65
|
+
|
66
|
+
extensions: []
|
67
|
+
|
68
|
+
extra_rdoc_files: []
|
69
|
+
|
70
|
+
files:
|
71
|
+
- README.md
|
72
|
+
- LICENSE
|
73
|
+
- VERSION
|
74
|
+
- ruby-freshbooks.gemspec
|
75
|
+
- lib/freshbooks.rb
|
76
|
+
- spec/freshbooks_spec.rb
|
77
|
+
has_rdoc: true
|
78
|
+
homepage: http://github.com/elucid/ruby-freshbooks
|
79
|
+
licenses: []
|
80
|
+
|
81
|
+
post_install_message:
|
82
|
+
rdoc_options: []
|
83
|
+
|
84
|
+
require_paths:
|
85
|
+
- lib
|
86
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
88
|
+
- - ">="
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
segments:
|
91
|
+
- 0
|
92
|
+
version: "0"
|
93
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
94
|
+
requirements:
|
95
|
+
- - ">="
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
segments:
|
98
|
+
- 1
|
99
|
+
- 2
|
100
|
+
version: "1.2"
|
101
|
+
requirements: []
|
102
|
+
|
103
|
+
rubyforge_project:
|
104
|
+
rubygems_version: 1.3.6
|
105
|
+
signing_key:
|
106
|
+
specification_version: 3
|
107
|
+
summary: simple FreshBooks API wrapper
|
108
|
+
test_files: []
|
109
|
+
|