fortnox-api 0.4.0 → 0.5.0

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.
Files changed (39) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +31 -29
  3. data/bin/console +8 -0
  4. data/fortnox-api.gemspec +1 -3
  5. data/lib/fortnox/api.rb +49 -14
  6. data/lib/fortnox/api/circular_queue.rb +33 -0
  7. data/lib/fortnox/api/mappers.rb +1 -1
  8. data/lib/fortnox/api/mappers/base.rb +3 -0
  9. data/lib/fortnox/api/models/article.rb +25 -25
  10. data/lib/fortnox/api/models/customer.rb +47 -47
  11. data/lib/fortnox/api/models/document_base.rb +4 -4
  12. data/lib/fortnox/api/repositories/article.rb +3 -4
  13. data/lib/fortnox/api/repositories/base.rb +77 -5
  14. data/lib/fortnox/api/repositories/base/savers.rb +2 -2
  15. data/lib/fortnox/api/repositories/customer.rb +3 -4
  16. data/lib/fortnox/api/repositories/invoice.rb +3 -4
  17. data/lib/fortnox/api/repositories/order.rb +3 -4
  18. data/lib/fortnox/api/repositories/project.rb +3 -3
  19. data/lib/fortnox/api/types/document_row.rb +3 -3
  20. data/lib/fortnox/api/types/model.rb +2 -6
  21. data/lib/fortnox/api/version.rb +1 -1
  22. data/spec/fortnox/api/mappers/base_spec.rb +6 -0
  23. data/spec/fortnox/api/repositories/article_spec.rb +4 -0
  24. data/spec/fortnox/api/repositories/base_spec.rb +348 -0
  25. data/spec/fortnox/api/repositories/customer_spec.rb +4 -0
  26. data/spec/fortnox/api/repositories/invoice_spec.rb +4 -0
  27. data/spec/fortnox/api/repositories/order_spec.rb +4 -0
  28. data/spec/fortnox/api/repositories/project_spec.rb +4 -0
  29. data/spec/fortnox/api/types/examples/document_row.rb +2 -2
  30. data/spec/fortnox/api/types/house_work_types_spec.rb +4 -0
  31. data/spec/fortnox/api_spec.rb +161 -31
  32. data/spec/spec_helper.rb +7 -2
  33. data/spec/support/helpers/configuration_helper.rb +10 -0
  34. metadata +10 -27
  35. data/lib/fortnox/api/base.rb +0 -47
  36. data/lib/fortnox/api/class_methods.rb +0 -30
  37. data/lib/fortnox/api/environment_validation.rb +0 -78
  38. data/spec/fortnox/api/base_spec.rb +0 -167
  39. data/spec/support/helpers/environment_helper.rb +0 -7
@@ -1,30 +0,0 @@
1
- require 'fortnox/api/environment_validation'
2
-
3
- module Fortnox
4
- module API
5
- module ClassMethods
6
-
7
- include Fortnox::API::EnvironmentValidation
8
-
9
- def get_access_token
10
- repository = self.new
11
-
12
- repository.headers = {
13
- 'Content-Type' => 'application/json',
14
- 'Accept' => 'application/json',
15
- 'Authorization-Code' => get_authorization_code,
16
- 'Client-Secret' => get_client_secret,
17
- }
18
-
19
- response = repository.get('/')
20
-
21
- response[ 'Authorisation' ][ 'AccessToken' ]
22
- end
23
-
24
- def set_headers( headers = {} )
25
- self.headers.merge!( headers )
26
- end
27
-
28
- end
29
- end
30
- end
@@ -1,78 +0,0 @@
1
- require 'forwardable'
2
-
3
- module Fortnox
4
- module API
5
- module EnvironmentValidation
6
-
7
- class MissingEnvironmentVariable < ArgumentError
8
- end
9
-
10
- class CircularQueue
11
- extend Forwardable
12
-
13
- def initialize *items
14
- @queue = [ *items ]
15
- @@next_index = random_start_index
16
- end
17
-
18
- # support some general Array methods that fit Queues well
19
- def_delegators :@queue, :new, :[], :size
20
-
21
- def next
22
- value = @queue[ @@next_index ]
23
- if @@next_index == size - 1
24
- @@next_index = 0
25
- else
26
- @@next_index += 1
27
- end
28
- return value
29
- end
30
-
31
- private
32
-
33
- def random_start_index
34
- Random.rand(@queue.size)
35
- end
36
- end
37
-
38
- private
39
-
40
- def get_base_url
41
- base_url = ENV['FORTNOX_API_BASE_URL']
42
- fail MissingEnvironmentVariable, 'You have to provide a base url.' unless base_url
43
- base_url
44
- end
45
-
46
- def get_client_secret
47
- client_secret = ENV['FORTNOX_API_CLIENT_SECRET']
48
- fail MissingEnvironmentVariable, 'You have to provide your client secret.' unless client_secret
49
- client_secret
50
- end
51
-
52
- def get_access_token
53
- @access_tokens ||= load_access_tokens
54
- @access_tokens.next
55
- end
56
-
57
- def get_authorization_code
58
- authorization_code = ENV['FORTNOX_API_AUTHORIZATION_CODE']
59
- fail MissingEnvironmentVariable, 'You have to provide your authorization code.' unless authorization_code
60
- authorization_code
61
- end
62
-
63
- def load_access_tokens
64
- check_access_tokens!
65
- access_token_string = ENV['FORTNOX_API_ACCESS_TOKEN']
66
- access_tokens = access_token_string.split(",").map(&:strip)
67
- CircularQueue.new( *access_tokens )
68
- end
69
-
70
- def check_access_tokens!
71
- unless ENV['FORTNOX_API_ACCESS_TOKEN']
72
- fail MissingEnvironmentVariable, 'You have to provide your access token.'
73
- end
74
- end
75
-
76
- end
77
- end
78
- end
@@ -1,167 +0,0 @@
1
- require 'spec_helper'
2
- require 'fortnox/api'
3
-
4
- describe Fortnox::API::Base do
5
- include Helpers::Environment
6
-
7
- describe 'creation' do
8
- before do
9
- stub_environment('FORTNOX_API_BASE_URL' => nil,
10
- 'FORTNOX_API_CLIENT_SECRET' => nil,
11
- 'FORTNOX_API_ACCESS_TOKEN' => nil)
12
- end
13
-
14
- subject{ ->{ described_class.new() } }
15
-
16
- context 'without FORTNOX_API_BASE_URL' do
17
- before do
18
- stub_environment('FORTNOX_API_BASE_URL' => nil)
19
- end
20
-
21
- it{ is_expected.to raise_error( ArgumentError, /base url/ ) }
22
- end
23
-
24
- context 'without FORTNOX_API_CLIENT_SECRET' do
25
- before do
26
- stub_environment('FORTNOX_API_BASE_URL' => 'xxx')
27
- end
28
-
29
- it{ is_expected.to raise_error( ArgumentError, /client secret/ ) }
30
- end
31
-
32
- context 'without FORTNOX_API_ACCESS_TOKEN' do
33
- before do
34
- stub_environment('FORTNOX_API_BASE_URL' => 'xxx',
35
- 'FORTNOX_API_CLIENT_SECRET' => 'xxx')
36
- end
37
-
38
- it{ is_expected.to raise_error( ArgumentError, /access token/ ) }
39
- end
40
-
41
- end
42
-
43
- context 'making a request including the proper headers' do
44
- before do
45
- stub_environment(
46
- 'FORTNOX_API_BASE_URL' => 'http://api.fortnox.se/3',
47
- 'FORTNOX_API_CLIENT_SECRET' => 'P5K5vE3Kun',
48
- 'FORTNOX_API_ACCESS_TOKEN' => '3f08d038-f380-4893-94a0-a08f6e60e67a'
49
- )
50
-
51
- stub_request(
52
- :get,
53
- 'http://api.fortnox.se/3/test',
54
- ).with(
55
- headers: {
56
- 'Access-Token' => '3f08d038-f380-4893-94a0-a08f6e60e67a',
57
- 'Client-Secret' => 'P5K5vE3Kun',
58
- 'Content-Type' => 'application/json',
59
- 'Accept' => 'application/json',
60
- }
61
- ).to_return(
62
- status: 200
63
- )
64
- end
65
-
66
- subject{ described_class.new.get( '/test', { body: '' }) }
67
-
68
- it{ is_expected.to be_nil }
69
- end
70
-
71
- describe 'making requests with multiple access tokens' do
72
-
73
- before do
74
- stub_environment(
75
- 'FORTNOX_API_BASE_URL' => 'http://api.fortnox.se/3',
76
- 'FORTNOX_API_CLIENT_SECRET' => 'P5K5vE3Kun',
77
- 'FORTNOX_API_ACCESS_TOKEN' => '3f08d038-f380-4893-94a0-a08f6e60e67a,aaee8217-0bbd-2e16-441f-668931d582cd'
78
- )
79
-
80
- stub_request(
81
- :get,
82
- 'http://api.fortnox.se/3/test',
83
- ).with(
84
- headers: {
85
- 'Access-Token' => '3f08d038-f380-4893-94a0-a08f6e60e67a',
86
- 'Client-Secret' => 'P5K5vE3Kun',
87
- 'Content-Type' => 'application/json',
88
- 'Accept' => 'application/json',
89
- }
90
- ).to_return(
91
- status: 200,
92
- body: '1'
93
- )
94
-
95
- stub_request(
96
- :get,
97
- 'http://api.fortnox.se/3/test',
98
- ).with(
99
- headers: {
100
- 'Access-Token' => 'aaee8217-0bbd-2e16-441f-668931d582cd',
101
- 'Client-Secret' => 'P5K5vE3Kun',
102
- 'Content-Type' => 'application/json',
103
- 'Accept' => 'application/json',
104
- }
105
- ).to_return(
106
- status: 200,
107
- body: '2'
108
- )
109
- end
110
-
111
- context 'with subsequent requests on same object' do
112
- let!(:response1){ api.get( '/test', body: '' ) }
113
- let!(:response2){ api.get( '/test', body: '' ) }
114
- let!(:response3){ api.get( '/test', body: '' ) }
115
-
116
- let(:api){ described_class.new }
117
-
118
- # rubocop:disable RSpec/MultipleExpectations
119
- # All these checks must be run in same it-statement because
120
- # of the random starting index.
121
- it 'works' do
122
- expect(response1).not_to eq( response2 )
123
- expect(response1).to eq( response3 )
124
- expect(response3).not_to eq( response2 )
125
- end
126
- # rubocop:enable RSpec/MultipleExpectations
127
- end
128
- end
129
-
130
- context 'raising error from remote server' do
131
-
132
- before do
133
- stub_environment(
134
- 'FORTNOX_API_BASE_URL' => 'http://api.fortnox.se/3',
135
- 'FORTNOX_API_CLIENT_SECRET' => 'P5K5vE3Kun',
136
- 'FORTNOX_API_ACCESS_TOKEN' => '3f08d038-f380-4893-94a0-a08f6e60e67a'
137
- )
138
-
139
- stub_request(
140
- :post,
141
- 'http://api.fortnox.se/3/test',
142
- ).to_return(
143
- status: 500,
144
- body: { 'ErrorInformation' => { 'error' => 1, 'message' => 'Räkenskapsår finns inte upplagt. För att kunna skapa en faktura krävs det att det finns ett räkenskapsår' } }.to_json,
145
- headers: { 'Content-Type' => 'application/json' },
146
- )
147
- end
148
-
149
- subject{ ->{ described_class.new.post( '/test', { body: '' }) } }
150
-
151
- it{ is_expected.to raise_error( Fortnox::API::RemoteServerError ) }
152
- it{ is_expected.to raise_error( 'Räkenskapsår finns inte upplagt. För att kunna skapa en faktura krävs det att det finns ett räkenskapsår' ) }
153
-
154
- context 'with debugging enabled' do
155
-
156
- around do |example|
157
- Fortnox::API.debugging = true
158
- example.run
159
- Fortnox::API.debugging = false
160
- end
161
-
162
- it{ is_expected.to raise_error( /\<HTTParty\:\:Request\:0x/ ) }
163
-
164
- end
165
- end
166
-
167
- end
@@ -1,7 +0,0 @@
1
- module Helpers
2
- module Environment
3
- def stub_environment( hash )
4
- stub_const( 'ENV', ENV.to_hash.merge( hash ))
5
- end
6
- end
7
- end