fortnox-api 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitignore +3 -17
- data/.ruby-version +1 -0
- data/.travis.yml +4 -0
- data/Gemfile +1 -1
- data/Gemfile.lock +127 -0
- data/Guardfile +5 -0
- data/LICENSE.txt +1 -1
- data/README.md +131 -7
- data/Rakefile +5 -1
- data/fortnox-api.gemspec +13 -3
- data/lib/fortnox/api.rb +11 -1
- data/lib/fortnox/api/base.rb +56 -0
- data/lib/fortnox/api/class_methods.rb +62 -0
- data/lib/fortnox/api/customer.rb +15 -0
- data/lib/fortnox/api/customer/entity.rb +179 -0
- data/lib/fortnox/api/customer/repository.rb +28 -0
- data/lib/fortnox/api/customer/validator.rb +26 -0
- data/lib/fortnox/api/entity.rb +45 -0
- data/lib/fortnox/api/repository/base.rb +27 -0
- data/lib/fortnox/api/repository/json_convertion.rb +55 -0
- data/lib/fortnox/api/repository/loaders.rb +49 -0
- data/lib/fortnox/api/repository/savers.rb +15 -0
- data/lib/fortnox/api/validator.rb +32 -0
- data/lib/fortnox/api/version.rb +1 -1
- data/spec/fortnox/api/base_spec.rb +93 -0
- data/spec/fortnox/api/customer/repository_spec.rb +0 -0
- data/spec/fortnox/api/customer/validator_spec.rb +32 -0
- data/spec/fortnox/api/entity_spec.rb +62 -0
- data/spec/spec_helper.rb +19 -0
- metadata +163 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6bca3361bd95f700ce751571429bc8463554b7d7
|
4
|
+
data.tar.gz: be033a769fa8d6bf4ca66ec4891b7d244826706c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: eb4e66f6bf2cf68ac7c2221f44a5e2e3b1e2fe82e6506ff5d409d24f37b347dca2c757dc53cbc861d673cb38cb14c213d383805e007062c4feacde911d16ce90
|
7
|
+
data.tar.gz: 53978bd089c4023f1cbaccd21b643663bb014217f98c4dfa850df792b85a4dd12e893f33f2371662b52c228a0ae8e05fb9b3588bdfd4858e60511f72bc331b60
|
data/.gitignore
CHANGED
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.1.2
|
data/.travis.yml
ADDED
data/Gemfile
CHANGED
data/Gemfile.lock
ADDED
@@ -0,0 +1,127 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
fortnox-api (0.0.2)
|
5
|
+
dotenv (~> 2.0)
|
6
|
+
httparty (~> 0.13)
|
7
|
+
vanguard (~> 0.0)
|
8
|
+
virtus (~> 1.0)
|
9
|
+
|
10
|
+
GEM
|
11
|
+
remote: https://rubygems.org/
|
12
|
+
specs:
|
13
|
+
abstract_type (0.0.7)
|
14
|
+
adamantium (0.2.0)
|
15
|
+
ice_nine (~> 0.11.0)
|
16
|
+
memoizable (~> 0.4.0)
|
17
|
+
addressable (2.3.8)
|
18
|
+
anima (0.2.0)
|
19
|
+
abstract_type (~> 0.0.7)
|
20
|
+
adamantium (~> 0.1)
|
21
|
+
equalizer (~> 0.0.8)
|
22
|
+
axiom-types (0.1.1)
|
23
|
+
descendants_tracker (~> 0.0.4)
|
24
|
+
ice_nine (~> 0.11.0)
|
25
|
+
thread_safe (~> 0.3, >= 0.3.1)
|
26
|
+
celluloid (0.16.0)
|
27
|
+
timers (~> 4.0.0)
|
28
|
+
coderay (1.1.0)
|
29
|
+
coercible (1.0.0)
|
30
|
+
descendants_tracker (~> 0.0.1)
|
31
|
+
crack (0.4.2)
|
32
|
+
safe_yaml (~> 1.0.0)
|
33
|
+
descendants_tracker (0.0.4)
|
34
|
+
thread_safe (~> 0.3, >= 0.3.1)
|
35
|
+
diff-lcs (1.2.5)
|
36
|
+
dotenv (2.0.1)
|
37
|
+
equalizer (0.0.11)
|
38
|
+
ffi (1.9.8)
|
39
|
+
formatador (0.2.5)
|
40
|
+
guard (2.12.5)
|
41
|
+
formatador (>= 0.2.4)
|
42
|
+
listen (~> 2.7)
|
43
|
+
lumberjack (~> 1.0)
|
44
|
+
nenv (~> 0.1)
|
45
|
+
notiffany (~> 0.0)
|
46
|
+
pry (>= 0.9.12)
|
47
|
+
shellany (~> 0.0)
|
48
|
+
thor (>= 0.18.1)
|
49
|
+
guard-compat (1.2.1)
|
50
|
+
guard-rspec (4.5.0)
|
51
|
+
guard (~> 2.1)
|
52
|
+
guard-compat (~> 1.1)
|
53
|
+
rspec (>= 2.99.0, < 4.0)
|
54
|
+
hitimes (1.2.2)
|
55
|
+
httparty (0.13.3)
|
56
|
+
json (~> 1.8)
|
57
|
+
multi_xml (>= 0.5.2)
|
58
|
+
ice_nine (0.11.1)
|
59
|
+
json (1.8.2)
|
60
|
+
listen (2.10.0)
|
61
|
+
celluloid (~> 0.16.0)
|
62
|
+
rb-fsevent (>= 0.9.3)
|
63
|
+
rb-inotify (>= 0.9)
|
64
|
+
lumberjack (1.0.9)
|
65
|
+
memoizable (0.4.2)
|
66
|
+
thread_safe (~> 0.3, >= 0.3.1)
|
67
|
+
method_source (0.8.2)
|
68
|
+
multi_xml (0.5.5)
|
69
|
+
nenv (0.2.0)
|
70
|
+
notiffany (0.0.6)
|
71
|
+
nenv (~> 0.1)
|
72
|
+
shellany (~> 0.0)
|
73
|
+
pry (0.10.1)
|
74
|
+
coderay (~> 1.1.0)
|
75
|
+
method_source (~> 0.8.1)
|
76
|
+
slop (~> 3.4)
|
77
|
+
rake (10.4.2)
|
78
|
+
rb-fsevent (0.9.4)
|
79
|
+
rb-inotify (0.9.5)
|
80
|
+
ffi (>= 0.5.0)
|
81
|
+
rspec (3.2.0)
|
82
|
+
rspec-core (~> 3.2.0)
|
83
|
+
rspec-expectations (~> 3.2.0)
|
84
|
+
rspec-mocks (~> 3.2.0)
|
85
|
+
rspec-core (3.2.3)
|
86
|
+
rspec-support (~> 3.2.0)
|
87
|
+
rspec-expectations (3.2.1)
|
88
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
89
|
+
rspec-support (~> 3.2.0)
|
90
|
+
rspec-mocks (3.2.1)
|
91
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
92
|
+
rspec-support (~> 3.2.0)
|
93
|
+
rspec-support (3.2.2)
|
94
|
+
safe_yaml (1.0.4)
|
95
|
+
shellany (0.0.1)
|
96
|
+
slop (3.6.0)
|
97
|
+
thor (0.19.1)
|
98
|
+
thread_safe (0.3.5)
|
99
|
+
timers (4.0.1)
|
100
|
+
hitimes
|
101
|
+
vanguard (0.0.5)
|
102
|
+
abstract_type (~> 0.0.6)
|
103
|
+
adamantium (~> 0.2.0)
|
104
|
+
anima (~> 0.2.0)
|
105
|
+
descendants_tracker (~> 0.0.1)
|
106
|
+
equalizer (~> 0.0.9)
|
107
|
+
virtus (1.0.5)
|
108
|
+
axiom-types (~> 0.1)
|
109
|
+
coercible (~> 1.0)
|
110
|
+
descendants_tracker (~> 0.0, >= 0.0.3)
|
111
|
+
equalizer (~> 0.0, >= 0.0.9)
|
112
|
+
webmock (1.21.0)
|
113
|
+
addressable (>= 2.3.6)
|
114
|
+
crack (>= 0.3.2)
|
115
|
+
|
116
|
+
PLATFORMS
|
117
|
+
ruby
|
118
|
+
|
119
|
+
DEPENDENCIES
|
120
|
+
bundler (~> 1.9)
|
121
|
+
fortnox-api!
|
122
|
+
guard (~> 2.12)
|
123
|
+
guard-rspec (~> 4.5)
|
124
|
+
pry
|
125
|
+
rake (~> 10.4)
|
126
|
+
rspec (~> 3.2)
|
127
|
+
webmock (~> 1.21)
|
data/Guardfile
ADDED
data/LICENSE.txt
CHANGED
data/README.md
CHANGED
@@ -1,28 +1,152 @@
|
|
1
|
-
# Fortnox::
|
1
|
+
# Fortnox::API
|
2
2
|
|
3
|
-
|
3
|
+
Wrapper gem for Fortnox AB's version 3 API.
|
4
|
+
|
5
|
+
[](http://badge.fury.io/rb/fortnox-api)
|
6
|
+
[](https://travis-ci.org/my-codeworks/fortnox-api)
|
7
|
+
[](https://codeclimate.com/github/my-codeworks/fortnox-api)
|
8
|
+
[](https://gemnasium.com/my-codeworks/fortnox-api)
|
9
|
+
|
10
|
+
## Features
|
11
|
+
|
12
|
+
Quick overview of the current status of this gem vs the API it integrates
|
13
|
+
against:
|
14
|
+
|
15
|
+
API resource | Status
|
16
|
+
------------------------------------------ | --------
|
17
|
+
Exchange AuthorizationCode for AccessToken | :white_check_mark:
|
18
|
+
Entity base class with immutability | :white_check_mark:
|
19
|
+
Validator base class based on Vanguard | :white_check_mark:
|
20
|
+
Customer::Validator | :white_check_mark:
|
21
|
+
Customers | :hourglass:
|
22
|
+
Repository READ from API functionality | :hourglass:
|
23
|
+
|
24
|
+
## Requirements
|
25
|
+
|
26
|
+
This gem is build for Ruby 2.0 or higher, it's tested agains Ruby 2.0.0 and
|
27
|
+
2.1.2. Since it uses the keywords argument feature it won't work on Ruby 1.9.
|
28
|
+
|
29
|
+
If you want or need Ruby 1.9 compatability please contact us or better yet,
|
30
|
+
submit a pull request. Instructions are to be found below :)
|
4
31
|
|
5
32
|
## Installation
|
6
33
|
|
7
34
|
Add this line to your application's Gemfile:
|
8
35
|
|
9
|
-
|
36
|
+
```ruby
|
37
|
+
gem 'fortnox-api'
|
38
|
+
```
|
10
39
|
|
11
40
|
And then execute:
|
12
41
|
|
13
|
-
|
42
|
+
```shell
|
43
|
+
$ bundle
|
44
|
+
```
|
14
45
|
|
15
46
|
Or install it yourself as:
|
16
47
|
|
17
|
-
|
48
|
+
```shell
|
49
|
+
$ gem install fortnox-api
|
50
|
+
```
|
18
51
|
|
19
52
|
## Usage
|
20
53
|
|
21
|
-
|
54
|
+
### Getting an AccessToken
|
55
|
+
|
56
|
+
Unless you have an AccessToken already you will need to exchange your
|
57
|
+
AuthorizationCode for one to make calls. In a ruby shell do the following
|
58
|
+
|
59
|
+
```ruby
|
60
|
+
require 'fortnox-api' # Load the gem, it needs to be installed first
|
61
|
+
Fortnox::API.get_access_token(
|
62
|
+
base_url: 'https://api.fortnox.se/3',
|
63
|
+
client_secret: 'P5K5wE3Kun',
|
64
|
+
authorization_code: 'ea3862b1-189c-464b-8e25-1b9702365fa1',
|
65
|
+
)
|
66
|
+
```
|
67
|
+
|
68
|
+
This will output a new token like `3f08d038-f380-4893-94a0a0-8f6e60e67a` that is
|
69
|
+
your access token, **save it!** You will need to save it since it will be used
|
70
|
+
every time you instansiate a Fortnox::API object.
|
71
|
+
|
72
|
+
### Using environment variables
|
73
|
+
|
74
|
+
You can save your settings in environment variables that will be used by the gem
|
75
|
+
when you instantiate it. The following variables are recognized:
|
76
|
+
|
77
|
+
```
|
78
|
+
FORTNOX_API_BASE_URL
|
79
|
+
FORTNOX_API_CLIENT_SECRET
|
80
|
+
FORTNOX_API_ACCESS_TOKEN
|
81
|
+
FORTNOX_API_AUTHORIZATION_CODE
|
82
|
+
```
|
83
|
+
|
84
|
+
Their values should match their name. Note that the authorization code is only
|
85
|
+
ever used once and only when you call the `get_access_token` method described
|
86
|
+
above.
|
87
|
+
|
88
|
+
### Collections
|
89
|
+
|
90
|
+
To get something to work with you instantiate a new Fortnox::API object
|
91
|
+
|
92
|
+
```ruby
|
93
|
+
fortnox = Fortnox::API.new(
|
94
|
+
base_url: 'https://api.fortnox.se/3',
|
95
|
+
client_secret: 'P5K5wE3Kun',
|
96
|
+
access_token: '3f08d038-f380-4893-94a0a0-8f6e60e67a',
|
97
|
+
)
|
98
|
+
```
|
99
|
+
|
100
|
+
On this you can then call things like
|
101
|
+
|
102
|
+
```ruby
|
103
|
+
# Get a list of all the customers (slow, don't use unless you have to)
|
104
|
+
fortnox.customer.all #=> [<Fortnox::API::Customer:0x007fdf21033ee8>, <Fortnox::API::Customer:0x007fdf22994310>, ... ]
|
105
|
+
|
106
|
+
# Get single customer by id
|
107
|
+
fortnox.customer.find( 5 ) #=> <Fortnox::API::Customer:0x007fdf21100b00>
|
108
|
+
|
109
|
+
# Get single customer by customer number
|
110
|
+
fortnox.customer.find_by( customer_number: 5 ) #=> <Fortnox::API::Customer:0x007fdf22949298>
|
111
|
+
|
112
|
+
# Get all customers matching some criteria
|
113
|
+
fortnox.customer.find_all_by( name: 'test' ) #=> [<Fortnox::API::Customer:0x007fdf22949298>, ... ]
|
114
|
+
```
|
115
|
+
|
116
|
+
### Entities
|
117
|
+
|
118
|
+
All the above methods return instances or arrays of instances of some resource
|
119
|
+
class such as customer, invoice, item, voucher and so on.
|
120
|
+
|
121
|
+
Instances are immutable and any update returns a new instance with the
|
122
|
+
appropriate attributes changed.
|
123
|
+
|
124
|
+
> :warning: Ruby implements assignment statements so that `LHS = RHS`
|
125
|
+
> always returns `RHS`. This is regardless of what an assignment method is
|
126
|
+
> implemented to return.
|
127
|
+
>
|
128
|
+
> This means that assignments like `customer.name = 'Test'` will return
|
129
|
+
> `'Test'` and not a new instance of `Customer`, which is what you want in this
|
130
|
+
> case. So assignment methods to update objects is not possible. Use the
|
131
|
+
> `.update()` method instead.
|
132
|
+
|
133
|
+
Each entity class has it's own methods and properties but they also all respond
|
134
|
+
to a few common methods, such as for persistence.
|
135
|
+
|
136
|
+
```ruby
|
137
|
+
customer #=> <Fortnox::API::Customer:0x007fdf228db310>
|
138
|
+
customer.name #=> "Nelly Bloom"
|
139
|
+
customer.update( name: "Ned Stark" ) #=> <Fortnox::API::Customer:0x0193a456ff0307>
|
140
|
+
customer.name #=> "Nelly Bloom"
|
141
|
+
updated_customer = customer.update( name: "Ned Stark" )
|
142
|
+
updated_customer.name #=> "Nelly Bloom"
|
143
|
+
updated_customer.save #=> true on success or false on failure
|
144
|
+
updated_customer.save! #=> true on success or raises Fortnox::API::RequestFailed exception on failure
|
145
|
+
```
|
22
146
|
|
23
147
|
## Contributing
|
24
148
|
|
25
|
-
1. Fork it ( http://github.com
|
149
|
+
1. Fork it ( http://github.com/my-codeworks/fortnox-api/fork )
|
26
150
|
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
151
|
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
152
|
4. Push to the branch (`git push origin my-new-feature`)
|
data/Rakefile
CHANGED
data/fortnox-api.gemspec
CHANGED
@@ -9,7 +9,7 @@ Gem::Specification.new do |spec|
|
|
9
9
|
spec.authors = ["Jonas Schubert Erlandsson"]
|
10
10
|
spec.email = ["jonas.schubert.erlandsson@my-codeworks.com"]
|
11
11
|
spec.summary = "Gem to use Fortnox REST API in Ruby."
|
12
|
-
spec.description = "This gem uses
|
12
|
+
spec.description = "This gem uses the HTTParty library to abstract away the REST calls. It gives you access to a number of objects that behave a lot like ActiveRecord instances, giving you access to methods like `all`, `find`, `find_by_...` and so on. And each individual instance can be easaly persistable to Fortnox again using the `save` method."
|
13
13
|
spec.homepage = "http://github.com/my-codeworks/fortnox-api"
|
14
14
|
spec.license = "MIT"
|
15
15
|
|
@@ -18,6 +18,16 @@ Gem::Specification.new do |spec|
|
|
18
18
|
spec.test_files = spec.files.grep(%r{^(spec)/})
|
19
19
|
spec.require_paths = ["lib"]
|
20
20
|
|
21
|
-
spec.
|
22
|
-
spec.
|
21
|
+
spec.add_dependency "httparty", "~> 0.13"
|
22
|
+
spec.add_dependency "dotenv", "~> 2.0"
|
23
|
+
spec.add_dependency "virtus", "~> 1.0"
|
24
|
+
spec.add_dependency "vanguard", "~> 0.0"
|
25
|
+
|
26
|
+
spec.add_development_dependency "bundler", "~> 1.9"
|
27
|
+
spec.add_development_dependency "rake", "~> 10.4"
|
28
|
+
spec.add_development_dependency "rspec", "~> 3.2"
|
29
|
+
spec.add_development_dependency "guard", "~> 2.12"
|
30
|
+
spec.add_development_dependency "guard-rspec", "~> 4.5"
|
31
|
+
spec.add_development_dependency "webmock", "~> 1.21"
|
32
|
+
spec.add_development_dependency "pry"
|
23
33
|
end
|
data/lib/fortnox/api.rb
CHANGED
@@ -1,7 +1,17 @@
|
|
1
|
+
require "set"
|
2
|
+
require "dotenv"
|
1
3
|
require "fortnox/api/version"
|
4
|
+
require "fortnox/api/customer"
|
5
|
+
|
6
|
+
Dotenv.load unless ENV['RUBY_ENV'] == 'test'
|
2
7
|
|
3
8
|
module Fortnox
|
4
9
|
module API
|
5
|
-
|
10
|
+
|
11
|
+
class << self
|
12
|
+
extend Forwardable
|
13
|
+
delegate [ :new, :get_access_token ] => Fortnox::API::Base
|
14
|
+
end
|
15
|
+
|
6
16
|
end
|
7
17
|
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
require "fortnox/api/class_methods"
|
2
|
+
require "httparty"
|
3
|
+
|
4
|
+
module Fortnox
|
5
|
+
module API
|
6
|
+
class Base
|
7
|
+
|
8
|
+
include HTTParty
|
9
|
+
extend Forwardable
|
10
|
+
extend Fortnox::API::ClassMethods
|
11
|
+
|
12
|
+
def_delegators self, :set_header, :set_headers, :remove_header,
|
13
|
+
:remove_headers, :validate_base_url, :validate_client_secret,
|
14
|
+
:validate_access_token, :validate_authorization_code
|
15
|
+
|
16
|
+
def initialize( base_url: nil, client_secret: nil, access_token: nil )
|
17
|
+
base_url = validate_base_url( base_url )
|
18
|
+
client_secret = validate_client_secret( client_secret )
|
19
|
+
access_token = validate_access_token( access_token )
|
20
|
+
|
21
|
+
self.class.base_uri( base_url )
|
22
|
+
|
23
|
+
set_headers(
|
24
|
+
'Accept' => 'application/json',
|
25
|
+
'Client-Secret' => client_secret,
|
26
|
+
'Access-Token' => access_token,
|
27
|
+
)
|
28
|
+
end
|
29
|
+
|
30
|
+
def get( *args )
|
31
|
+
resp = self.class.get( *args )
|
32
|
+
# Insert error handling here
|
33
|
+
resp.parsed_response
|
34
|
+
end
|
35
|
+
|
36
|
+
def put( *args )
|
37
|
+
resp = self.class.get( *args )
|
38
|
+
# Insert error handling here
|
39
|
+
resp.parsed_response
|
40
|
+
end
|
41
|
+
|
42
|
+
def post( *args )
|
43
|
+
resp = self.class.get( *args )
|
44
|
+
# Insert error handling here
|
45
|
+
resp.parsed_response
|
46
|
+
end
|
47
|
+
|
48
|
+
def delete( *args )
|
49
|
+
resp = self.class.get( *args )
|
50
|
+
# Insert error handling here
|
51
|
+
resp.parsed_response
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|