jortt 4.1.0 → 6.1.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 (44) hide show
  1. checksums.yaml +5 -5
  2. data/.github/workflows/rspec.yml +27 -0
  3. data/.ruby-version +1 -1
  4. data/README.md +75 -133
  5. data/Rakefile +0 -1
  6. data/jortt.gemspec +7 -7
  7. data/lib/jortt/client/customers.rb +71 -42
  8. data/lib/jortt/client/error.rb +33 -0
  9. data/lib/jortt/client/invoices.rb +55 -31
  10. data/lib/jortt/client/ledger_accounts.rb +27 -0
  11. data/lib/jortt/client/version.rb +1 -2
  12. data/lib/jortt/client.rb +65 -16
  13. data/lib/jortt.rb +0 -1
  14. data/spec/fixtures/vcr_cassettes/Jortt/_client/1_1_1.yml +64 -0
  15. data/spec/fixtures/vcr_cassettes/Jortt_Client/configured/_customers/1_1_1_1.yml +64 -0
  16. data/spec/fixtures/vcr_cassettes/Jortt_Client/configured/_invoices/1_1_2_1.yml +64 -0
  17. data/spec/fixtures/vcr_cassettes/Jortt_Client/configured/_ledger_accounts/1_1_3_1.yml +64 -0
  18. data/spec/fixtures/vcr_cassettes/Jortt_Client_Customers/_create/faulty_payload/shows_a_nice_error.yml +413 -0
  19. data/spec/fixtures/vcr_cassettes/Jortt_Client_Customers/_create/valid_payload/creates_the_customer.yml +505 -0
  20. data/spec/fixtures/vcr_cassettes/Jortt_Client_Customers/_delete/deletes_the_customer.yml +505 -0
  21. data/spec/fixtures/vcr_cassettes/Jortt_Client_Customers/_direct_debit_mandate/sends_direct_debit_mandate_to_the_customer_or_responds_with_an_error_when_not_possible.yml +470 -0
  22. data/spec/fixtures/vcr_cassettes/Jortt_Client_Customers/_index/query/returns_the_queried_customers.yml +464 -0
  23. data/spec/fixtures/vcr_cassettes/Jortt_Client_Customers/_index/without_params/returns_customers.yml +415 -0
  24. data/spec/fixtures/vcr_cassettes/Jortt_Client_Customers/_show/returns_the_customer.yml +464 -0
  25. data/spec/fixtures/vcr_cassettes/Jortt_Client_Customers/_update/updates_the_customer.yml +603 -0
  26. data/spec/fixtures/vcr_cassettes/Jortt_Client_Customers/_vat_percentages/returns_the_vat_percentages.yml +330 -0
  27. data/spec/fixtures/vcr_cassettes/Jortt_Client_Invoices/_create/creates_the_invoice.yml +170 -0
  28. data/spec/fixtures/vcr_cassettes/Jortt_Client_Invoices/_credit/credits_the_invoice.yml +374 -0
  29. data/spec/fixtures/vcr_cassettes/Jortt_Client_Invoices/_download/returns_the_invoice_download_link.yml +168 -0
  30. data/spec/fixtures/vcr_cassettes/Jortt_Client_Invoices/_index/invoice_status/returns_those_invoices.yml +776 -0
  31. data/spec/fixtures/vcr_cassettes/Jortt_Client_Invoices/_index/query/returns_the_queried_invoices.yml +315 -0
  32. data/spec/fixtures/vcr_cassettes/Jortt_Client_Invoices/_show/returns_the_invoice.yml +421 -0
  33. data/spec/fixtures/vcr_cassettes/Jortt_Client_LedgerAccounts/_index/returns_invoices.yml +118 -0
  34. data/spec/jortt/client/customers_spec.rb +109 -20
  35. data/spec/jortt/client/invoices_spec.rb +118 -20
  36. data/spec/jortt/client/ledger_accounts_spec.rb +19 -0
  37. data/spec/jortt/client_spec.rb +5 -27
  38. data/spec/jortt_spec.rb +2 -3
  39. data/spec/spec_helper.rb +18 -4
  40. metadata +85 -41
  41. data/.rubocop.yml +0 -26
  42. data/.travis.yml +0 -7
  43. data/lib/jortt/client/invoice.rb +0 -50
  44. data/spec/jortt/client/invoice_spec.rb +0 -23
data/lib/jortt/client.rb CHANGED
@@ -1,23 +1,26 @@
1
- # encoding: UTF-8
1
+ require 'oauth2'
2
+
3
+ require 'jortt/client/error'
2
4
  require 'jortt/client/customers'
3
5
  require 'jortt/client/invoices'
4
- require 'jortt/client/invoice'
6
+ require 'jortt/client/ledger_accounts'
5
7
 
6
8
  module Jortt
7
9
  ##
8
10
  # This class is the main interface used to communicate with the Jortt API.
9
11
  # It is by the {Jortt} module to create configured instances.
10
12
  class Client
11
- BASE_URL = 'https://app.jortt.nl/api'
13
+ SITE = 'https://api.jortt.nl'
14
+ OAUTH_PROVIDER_URL = 'https://app.jortt.nl/oauth-provider/oauth'
12
15
 
13
- attr_accessor :base_url, :app_name, :api_key
16
+ attr_accessor :token
14
17
 
15
18
  # Initialize a Jortt client.
16
19
  #
17
20
  # @example
18
21
  # Jortt::Client.new(
19
- # app_name: <application-name, chosen in jortt.nl>
20
- # api_key: <api-key, as provided by jortt.nl>
22
+ # "my-client-id",
23
+ # "my-client-secret"
21
24
  # )
22
25
  #
23
26
  # @params [ Hash ] opts Options for the client,
@@ -26,10 +29,17 @@ module Jortt
26
29
  # @return [ Jortt::Client ]
27
30
  #
28
31
  # @since 1.0.0
29
- def initialize(opts)
30
- self.base_url = opts.fetch(:base_url, BASE_URL)
31
- self.app_name = opts.fetch(:app_name)
32
- self.api_key = opts.fetch(:api_key)
32
+ def initialize(id, secret, opts = {})
33
+ oauth_provider_url = opts[:oauth_provider_url] || OAUTH_PROVIDER_URL
34
+
35
+ client = OAuth2::Client.new(id, secret,
36
+ site: opts[:site] || SITE,
37
+ token_url: "#{oauth_provider_url}/token",
38
+ authorize_url: "#{oauth_provider_url}/authorize",
39
+ auth_scheme: :basic_auth
40
+ )
41
+
42
+ @token = client.client_credentials.get_token(scope: "invoices:read invoices:write customers:read customers:write")
33
43
  end
34
44
 
35
45
  # Access the customer resource to perform operations.
@@ -56,16 +66,55 @@ module Jortt
56
66
  @invoices ||= Jortt::Client::Invoices.new(self)
57
67
  end
58
68
 
59
- # Access a single invoice resource to perform operations.
69
+ # Access the ledger_accounts resource.
60
70
  #
61
71
  # @example
62
- # client.invoice('abc')
72
+ # client.ledger_accounts
63
73
  #
64
- # @params [ String ] invoice_id The id of an invoice.
74
+ # @return [ Jortt::Client::LedgerAccounts ] entry to the leger_accounts resource.
65
75
  #
66
- # @return [ Jortt::Client::Invoice ] entry to the invoice resource.
67
- def invoice(invoice_id)
68
- Jortt::Client::Invoice.new(self, invoice_id)
76
+ # @since 5.0.0
77
+ def ledger_accounts
78
+ Jortt::Client::LedgerAccounts.new(self)
79
+ end
80
+
81
+ def get(path, params = {})
82
+ handle_response { token.get(path, params: params) }
83
+ end
84
+
85
+ def post(path, params = {})
86
+ handle_response { token.post(path, params: params) }
87
+ end
88
+
89
+ def put(path, params = {})
90
+ handle_response { token.put(path, params: params) }
91
+ end
92
+
93
+ def delete(path)
94
+ handle_response { token.delete(path) }
95
+ end
96
+
97
+ def handle_response(&block)
98
+ response = yield
99
+ return true if response.status == 204
100
+ response.parsed.fetch('data')
101
+ rescue OAuth2::Error => e
102
+ raise Error.from_response(e.response)
103
+ end
104
+
105
+ def paginated(path, params = {})
106
+ page = 1
107
+
108
+ Enumerator.new do |yielder|
109
+ loop do
110
+ response = token.get(path, params: params.merge(page: page)).parsed
111
+ response['data'].each { |item| yielder << item }
112
+ break if response['_links']['next'].nil?
113
+ page += 1
114
+ end
115
+ end
116
+ rescue OAuth2::Error => e
117
+ raise Error.from_response(e.response)
69
118
  end
70
119
 
71
120
  end
data/lib/jortt.rb CHANGED
@@ -1,4 +1,3 @@
1
- # encoding: UTF-8
2
1
  require 'jortt/client'
3
2
  require 'jortt/client/version'
4
3
 
@@ -0,0 +1,64 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: post
5
+ uri: https://app.jortt.nl/oauth-provider/oauth/token
6
+ body:
7
+ encoding: UTF-8
8
+ string: grant_type=client_credentials&scope=invoices%3Aread+invoices%3Awrite+customers%3Aread+customers%3Awrite
9
+ headers:
10
+ User-Agent:
11
+ - Faraday v1.0.1
12
+ Content-Type:
13
+ - application/x-www-form-urlencoded
14
+ Accept-Encoding:
15
+ - gzip;q=1.0,deflate;q=0.6,identity;q=0.3
16
+ Accept:
17
+ - "*/*"
18
+ response:
19
+ status:
20
+ code: 200
21
+ message: OK
22
+ headers:
23
+ Date:
24
+ - Thu, 24 Sep 2020 15:03:37 GMT
25
+ Content-Type:
26
+ - application/json; charset=utf-8
27
+ Transfer-Encoding:
28
+ - chunked
29
+ Connection:
30
+ - keep-alive
31
+ Status:
32
+ - 200 OK
33
+ Cache-Control:
34
+ - private, no-store
35
+ Referrer-Policy:
36
+ - strict-origin-when-cross-origin
37
+ X-Permitted-Cross-Domain-Policies:
38
+ - none
39
+ Pragma:
40
+ - no-cache
41
+ X-Xss-Protection:
42
+ - 1; mode=block
43
+ X-Request-Id:
44
+ - dfa7330e-2975-4637-8d0b-fee35ed40cac
45
+ X-Download-Options:
46
+ - noopen
47
+ Etag:
48
+ - W/"97e5c8d5cedcea8cd8407a1a0a6f21c1"
49
+ X-Frame-Options:
50
+ - SAMEORIGIN
51
+ X-Runtime:
52
+ - '0.319627'
53
+ X-Content-Type-Options:
54
+ - nosniff
55
+ X-Powered-By:
56
+ - Phusion Passenger
57
+ Server:
58
+ - nginx + Phusion Passenger
59
+ body:
60
+ encoding: UTF-8
61
+ string: '{"access_token":"OCU8keLfHRLh6MQ1GdpTxDF9fvnRHge-h8IuDo1fx5M","token_type":"Bearer","expires_in":7200,"scope":"invoices:read
62
+ invoices:write customers:read customers:write","created_at":1600959817}'
63
+ recorded_at: Thu, 24 Sep 2020 15:03:37 GMT
64
+ recorded_with: VCR 6.0.0
@@ -0,0 +1,64 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: post
5
+ uri: https://app.jortt.nl/oauth-provider/oauth/token
6
+ body:
7
+ encoding: UTF-8
8
+ string: grant_type=client_credentials&scope=invoices%3Aread+invoices%3Awrite+customers%3Aread+customers%3Awrite
9
+ headers:
10
+ User-Agent:
11
+ - Faraday v1.0.1
12
+ Content-Type:
13
+ - application/x-www-form-urlencoded
14
+ Accept-Encoding:
15
+ - gzip;q=1.0,deflate;q=0.6,identity;q=0.3
16
+ Accept:
17
+ - "*/*"
18
+ response:
19
+ status:
20
+ code: 200
21
+ message: OK
22
+ headers:
23
+ Date:
24
+ - Thu, 24 Sep 2020 15:03:35 GMT
25
+ Content-Type:
26
+ - application/json; charset=utf-8
27
+ Transfer-Encoding:
28
+ - chunked
29
+ Connection:
30
+ - keep-alive
31
+ Status:
32
+ - 200 OK
33
+ Cache-Control:
34
+ - private, no-store
35
+ Referrer-Policy:
36
+ - strict-origin-when-cross-origin
37
+ X-Permitted-Cross-Domain-Policies:
38
+ - none
39
+ Pragma:
40
+ - no-cache
41
+ X-Xss-Protection:
42
+ - 1; mode=block
43
+ X-Request-Id:
44
+ - 68b40955-bca3-4ef1-a33a-b688de73e738
45
+ X-Download-Options:
46
+ - noopen
47
+ Etag:
48
+ - W/"c977a25cd5737656d676c943a553cb29"
49
+ X-Frame-Options:
50
+ - SAMEORIGIN
51
+ X-Runtime:
52
+ - '0.319920'
53
+ X-Content-Type-Options:
54
+ - nosniff
55
+ X-Powered-By:
56
+ - Phusion Passenger
57
+ Server:
58
+ - nginx + Phusion Passenger
59
+ body:
60
+ encoding: UTF-8
61
+ string: '{"access_token":"Ck844kAGSr1df34pT_5YnvgKKZxa50ZzDsBHpwDTjk8","token_type":"Bearer","expires_in":7200,"scope":"invoices:read
62
+ invoices:write customers:read customers:write","created_at":1600959815}'
63
+ recorded_at: Thu, 24 Sep 2020 15:03:35 GMT
64
+ recorded_with: VCR 6.0.0
@@ -0,0 +1,64 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: post
5
+ uri: https://app.jortt.nl/oauth-provider/oauth/token
6
+ body:
7
+ encoding: UTF-8
8
+ string: grant_type=client_credentials&scope=invoices%3Aread+invoices%3Awrite+customers%3Aread+customers%3Awrite
9
+ headers:
10
+ User-Agent:
11
+ - Faraday v1.0.1
12
+ Content-Type:
13
+ - application/x-www-form-urlencoded
14
+ Accept-Encoding:
15
+ - gzip;q=1.0,deflate;q=0.6,identity;q=0.3
16
+ Accept:
17
+ - "*/*"
18
+ response:
19
+ status:
20
+ code: 200
21
+ message: OK
22
+ headers:
23
+ Date:
24
+ - Thu, 24 Sep 2020 15:03:36 GMT
25
+ Content-Type:
26
+ - application/json; charset=utf-8
27
+ Transfer-Encoding:
28
+ - chunked
29
+ Connection:
30
+ - keep-alive
31
+ Status:
32
+ - 200 OK
33
+ Cache-Control:
34
+ - private, no-store
35
+ Referrer-Policy:
36
+ - strict-origin-when-cross-origin
37
+ X-Permitted-Cross-Domain-Policies:
38
+ - none
39
+ Pragma:
40
+ - no-cache
41
+ X-Xss-Protection:
42
+ - 1; mode=block
43
+ X-Request-Id:
44
+ - 6bc1cab8-2244-4e71-9660-756eb0151c4e
45
+ X-Download-Options:
46
+ - noopen
47
+ Etag:
48
+ - W/"d0f9e4a84aa7b9af1d36a01aa38cf13a"
49
+ X-Frame-Options:
50
+ - SAMEORIGIN
51
+ X-Runtime:
52
+ - '0.321558'
53
+ X-Content-Type-Options:
54
+ - nosniff
55
+ X-Powered-By:
56
+ - Phusion Passenger
57
+ Server:
58
+ - nginx + Phusion Passenger
59
+ body:
60
+ encoding: UTF-8
61
+ string: '{"access_token":"Ap-1PU0Vghk01uEbz8mEP-Iaoc9eZygDcLwvEphpFqU","token_type":"Bearer","expires_in":7200,"scope":"invoices:read
62
+ invoices:write customers:read customers:write","created_at":1600959816}'
63
+ recorded_at: Thu, 24 Sep 2020 15:03:36 GMT
64
+ recorded_with: VCR 6.0.0
@@ -0,0 +1,64 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: post
5
+ uri: https://app.jortt.nl/oauth-provider/oauth/token
6
+ body:
7
+ encoding: UTF-8
8
+ string: grant_type=client_credentials&scope=invoices%3Aread+invoices%3Awrite+customers%3Aread+customers%3Awrite
9
+ headers:
10
+ User-Agent:
11
+ - Faraday v1.0.1
12
+ Content-Type:
13
+ - application/x-www-form-urlencoded
14
+ Accept-Encoding:
15
+ - gzip;q=1.0,deflate;q=0.6,identity;q=0.3
16
+ Accept:
17
+ - "*/*"
18
+ response:
19
+ status:
20
+ code: 200
21
+ message: OK
22
+ headers:
23
+ Date:
24
+ - Thu, 24 Sep 2020 15:03:36 GMT
25
+ Content-Type:
26
+ - application/json; charset=utf-8
27
+ Transfer-Encoding:
28
+ - chunked
29
+ Connection:
30
+ - keep-alive
31
+ Status:
32
+ - 200 OK
33
+ Cache-Control:
34
+ - private, no-store
35
+ Referrer-Policy:
36
+ - strict-origin-when-cross-origin
37
+ X-Permitted-Cross-Domain-Policies:
38
+ - none
39
+ Pragma:
40
+ - no-cache
41
+ X-Xss-Protection:
42
+ - 1; mode=block
43
+ X-Request-Id:
44
+ - 290ef5a5-d238-4a03-bfdb-5e811fbd9461
45
+ X-Download-Options:
46
+ - noopen
47
+ Etag:
48
+ - W/"e1784d286681a5ff4135f3ca0a76ee57"
49
+ X-Frame-Options:
50
+ - SAMEORIGIN
51
+ X-Runtime:
52
+ - '0.319839'
53
+ X-Content-Type-Options:
54
+ - nosniff
55
+ X-Powered-By:
56
+ - Phusion Passenger
57
+ Server:
58
+ - nginx + Phusion Passenger
59
+ body:
60
+ encoding: UTF-8
61
+ string: '{"access_token":"tTXpHoM9jl-_hp7rEr3_9SsUQWtvg0Pfw-re3HsbgZo","token_type":"Bearer","expires_in":7200,"scope":"invoices:read
62
+ invoices:write customers:read customers:write","created_at":1600959816}'
63
+ recorded_at: Thu, 24 Sep 2020 15:03:36 GMT
64
+ recorded_with: VCR 6.0.0