moneybird 0.9.2 → 0.9.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (59) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/lib/moneybird.rb +9 -17
  4. data/lib/moneybird/client.rb +39 -34
  5. data/lib/moneybird/http_error/authorization_required.rb +6 -0
  6. data/lib/moneybird/http_error/bad_request.rb +6 -0
  7. data/lib/moneybird/http_error/forbidden.rb +6 -0
  8. data/lib/moneybird/http_error/internal_server_error.rb +6 -0
  9. data/lib/moneybird/http_error/method_not_allowed.rb +6 -0
  10. data/lib/moneybird/http_error/not_accepted.rb +6 -0
  11. data/lib/moneybird/http_error/not_found.rb +6 -0
  12. data/lib/moneybird/http_error/payment_required.rb +6 -0
  13. data/lib/moneybird/http_error/too_many_requests.rb +6 -0
  14. data/lib/moneybird/http_error/unprocessable_entity.rb +6 -0
  15. data/lib/moneybird/middleware/error_handling.rb +25 -0
  16. data/lib/moneybird/middleware/pagination.rb +29 -0
  17. data/lib/moneybird/middleware/pagination/links.rb +17 -0
  18. data/lib/moneybird/resource.rb +0 -1
  19. data/lib/moneybird/resource/administration.rb +20 -0
  20. data/lib/moneybird/resource/contact.rb +30 -29
  21. data/lib/moneybird/resource/documents/general_document.rb +25 -0
  22. data/lib/moneybird/resource/documents/general_journal_document.rb +26 -0
  23. data/lib/moneybird/resource/documents/purchase_invoice.rb +49 -0
  24. data/lib/moneybird/resource/documents/receipt.rb +35 -0
  25. data/lib/moneybird/resource/documents/typeless_document.rb +26 -0
  26. data/lib/moneybird/resource/estimate.rb +24 -23
  27. data/lib/moneybird/resource/financial_mutation.rb +14 -12
  28. data/lib/moneybird/resource/purchase_invoice.rb +1 -0
  29. data/lib/moneybird/resource/sales_invoice.rb +1 -0
  30. data/lib/moneybird/resource/synchronization.rb +13 -0
  31. data/lib/moneybird/service/administration.rb +6 -4
  32. data/lib/moneybird/service/contact.rb +7 -8
  33. data/lib/moneybird/service/document_style.rb +2 -0
  34. data/lib/moneybird/service/documents/general_document.rb +21 -0
  35. data/lib/moneybird/service/documents/general_journal_document.rb +21 -0
  36. data/lib/moneybird/service/documents/purchase_invoice.rb +21 -0
  37. data/lib/moneybird/service/documents/receipt.rb +21 -0
  38. data/lib/moneybird/service/documents/typeless_document.rb +21 -0
  39. data/lib/moneybird/service/estimate.rb +3 -0
  40. data/lib/moneybird/service/financial_account.rb +2 -0
  41. data/lib/moneybird/service/financial_mutation.rb +3 -0
  42. data/lib/moneybird/service/identity.rb +2 -0
  43. data/lib/moneybird/service/ledger_account.rb +2 -0
  44. data/lib/moneybird/service/product.rb +2 -0
  45. data/lib/moneybird/service/purchase_invoice.rb +2 -0
  46. data/lib/moneybird/service/recurring_sales_invoice.rb +3 -0
  47. data/lib/moneybird/service/sales_invoice.rb +3 -0
  48. data/lib/moneybird/service/tax_rate.rb +2 -0
  49. data/lib/moneybird/service/webhook.rb +2 -0
  50. data/lib/moneybird/service/workflow.rb +2 -0
  51. data/lib/moneybird/traits/delete.rb +0 -6
  52. data/lib/moneybird/traits/find.rb +1 -7
  53. data/lib/moneybird/traits/find_all.rb +33 -7
  54. data/lib/moneybird/traits/save.rb +10 -27
  55. data/lib/moneybird/traits/service.rb +1 -3
  56. data/lib/moneybird/traits/synchronization.rb +11 -0
  57. data/lib/moneybird/version.rb +1 -1
  58. data/moneybird.gemspec +3 -1
  59. metadata +69 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: edf49e48a06d2bbc1a852bc5d9c6869e8154acd9
4
- data.tar.gz: dd22cf1c4420519f963c7f88b487cdf54e548f08
3
+ metadata.gz: 496ef32fbfca566374e3a5a674d66e3ce57b25b9
4
+ data.tar.gz: 30c414e205265c5b7e163b43bcaad3dc873b8067
5
5
  SHA512:
6
- metadata.gz: 2c1da271dd2696efd9c22b6d5d8fe12d3ee2c839e4bfda2dcd237fe76203cb1537f26a23e92fad991f0b24786a93c553afdee391676d1ca8b174426987e66a2d
7
- data.tar.gz: 9a081ff415eb15757eab40906fdc1e7c3fe17e6f79a81ebc58e35b43778cfbf144da82a16ea8f0069166884c3ef20dd88fdb7d44b45b020ddf9e181df257e15b
6
+ metadata.gz: 7f7b3a30290815ec5696f8138baeb5990ce09d2d7f1697cf4c229c2396641894cd90d4dc401a31ca8595330dd3f5c9f034df9969596e926e66db3175b2c55765
7
+ data.tar.gz: 28eb63c622f6daab61bfc2f99bbc10465ad838ac5e6ecbaa26a8b21e284ed6de19a1af3977b732522ec8a435f2d95461e07e946f5b24ab5808b311ebcd0e60a9
data/.gitignore CHANGED
@@ -7,3 +7,4 @@
7
7
  /pkg/
8
8
  /spec/reports/
9
9
  /tmp/
10
+ /.idea/
@@ -10,26 +10,18 @@ require 'json'
10
10
  require 'net/http'
11
11
 
12
12
  module Moneybird
13
- module Service; end;
14
- module Traits; end;
13
+ module HttpError; end
14
+ module Middleware; end
15
+ module Service; end
16
+ module Traits; end
15
17
  end
16
18
 
17
- require "moneybird/traits/administration_service"
18
- require "moneybird/traits/find"
19
- require "moneybird/traits/find_all"
20
- require "moneybird/traits/service"
21
- require "moneybird/traits/save"
22
- require "moneybird/traits/delete"
23
- require "moneybird/client"
24
- require "moneybird/resource"
25
- require "moneybird/webhook"
19
+ require 'moneybird/resource'
26
20
 
27
- Dir[File.join(File.dirname(__FILE__), 'moneybird/service', '**/*.rb')].each do |service|
28
- require service
21
+ Dir[File.join(File.dirname(__FILE__), 'moneybird', 'traits', '*.rb')].each do |file|
22
+ require file
29
23
  end
30
24
 
31
- Dir[File.join(File.dirname(__FILE__), 'moneybird/resource', '**/*.rb')].each do |resource|
32
- require resource
25
+ Dir[File.join(File.dirname(__FILE__), 'moneybird', '**', '**.rb')].each do |file|
26
+ require file
33
27
  end
34
-
35
- require "moneybird/version"
@@ -1,6 +1,8 @@
1
+ require 'faraday_middleware'
2
+
1
3
  module Moneybird
2
4
  class Client
3
- attr_reader :bearer_token, :_last_response
5
+ attr_reader :bearer_token
4
6
  attr_accessor :errors
5
7
  attr_writer :http, :faraday_adapter
6
8
 
@@ -9,7 +11,7 @@ module Moneybird
9
11
  end
10
12
 
11
13
  def base_url
12
- "https://moneybird.com/api/#{version}/"
14
+ "https://moneybird.com/"
13
15
  end
14
16
 
15
17
  def version
@@ -21,50 +23,53 @@ module Moneybird
21
23
  end
22
24
 
23
25
  def http
24
- @http ||= begin
25
- uri = uri_for_path(base_url)
26
- http = Faraday.new(:url => uri) do |faraday|
27
- faraday.request :url_encoded
28
- faraday.adapter faraday_adapter
29
- end
30
- http
26
+ @http ||= Faraday.new(url: base_url) do |faraday|
27
+ faraday.headers = faraday_headers
28
+ faraday.request :url_encoded
29
+ faraday.response :json
30
+ faraday.use Moneybird::Middleware::ErrorHandling
31
+ faraday.use Moneybird::Middleware::Pagination
32
+ # faraday.response :logger do |logger|
33
+ # logger.filter(/(Bearer)\s(\S+)/, '\1[REMOVED]')
34
+ # end
35
+ faraday.adapter faraday_adapter
31
36
  end
32
37
  end
33
38
 
34
- def headers
35
- {
36
- 'Accept' => 'application/json',
37
- 'Content-Type' => 'application/json',
38
- 'Authorization' => "Bearer #{bearer_token}"
39
- }
40
- end
41
-
42
- def uri_for_path(path)
43
- URI.parse(File.join(base_url, path))
39
+ %i[get patch post delete].each do |call|
40
+ define_method call do |path, options = {}|
41
+ http.public_send(call, "/api/#{version}/#{path}", options).body
42
+ end
44
43
  end
45
44
 
46
- def get(path, headers={})
47
- uri = uri_for_path(path)
48
- @_last_response = http.get(uri.request_uri, nil, self.headers.merge(headers))
45
+ def get_all_pages(path, options = {})
46
+ get_each_page(path, options).inject([]) do |array, objects|
47
+ array += objects
48
+ end
49
49
  end
50
50
 
51
- def patch(path, body=nil, headers={})
52
- uri = uri_for_path(path)
53
- @_last_response = http.patch(uri.request_uri, body, self.headers.merge(headers))
51
+ def get_each_page(path, options = {})
52
+ return enum_for(:get_each_page, path, options) unless block_given?
53
+ path = "/api/#{version}/#{path}"
54
+ while path
55
+ response = http.get(path, options)
56
+ yield response.body
57
+ path = (response[:pagination_links].next if response[:pagination_links])
58
+ end
54
59
  end
55
60
 
56
- def post(path, body=nil, headers={})
57
- uri = uri_for_path(path)
58
- @_last_response = http.post(uri.request_uri, body, self.headers.merge(headers))
61
+ def administrations
62
+ Moneybird::Service::Administration.new(self).all
59
63
  end
60
64
 
61
- def delete(path, headers={})
62
- uri = uri_for_path(path)
63
- @_last_response = http.delete(uri.request_uri, nil, self.headers.merge(headers))
64
- end
65
+ private
65
66
 
66
- def administrations
67
- Moneybird::Service::Administration.new(self).all
67
+ def faraday_headers
68
+ {
69
+ 'Accept' => 'application/json',
70
+ 'Content-Type' => 'application/json',
71
+ 'Authorization' => "Bearer #{bearer_token}"
72
+ }
68
73
  end
69
74
  end
70
75
  end
@@ -0,0 +1,6 @@
1
+ module Moneybird
2
+ module HttpError
3
+ class AuthorizationRequired < StandardError
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ module Moneybird
2
+ module HttpError
3
+ class BadRequest < StandardError
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ module Moneybird
2
+ module HttpError
3
+ class Forbidden < StandardError
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ module Moneybird
2
+ module HttpError
3
+ class InternalServerError < StandardError
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ module Moneybird
2
+ module HttpError
3
+ class MethodNotAllowed < StandardError
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ module Moneybird
2
+ module HttpError
3
+ class NotAccepted < StandardError
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ module Moneybird
2
+ module HttpError
3
+ class NotFound < StandardError
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ module Moneybird
2
+ module HttpError
3
+ class PaymentRequired < StandardError
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ module Moneybird
2
+ module HttpError
3
+ class TooManyRequests < StandardError
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ module Moneybird
2
+ module HttpError
3
+ class UnprocessableEntity < StandardError
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Moneybird
4
+ module Middleware
5
+ class ErrorHandling < Faraday::Response::Middleware
6
+ ERROR_MAP = {
7
+ 400 => Moneybird::HttpError::BadRequest,
8
+ 401 => Moneybird::HttpError::AuthorizationRequired,
9
+ 402 => Moneybird::HttpError::PaymentRequired,
10
+ 403 => Moneybird::HttpError::Forbidden,
11
+ 404 => Moneybird::HttpError::NotFound,
12
+ 405 => Moneybird::HttpError::MethodNotAllowed,
13
+ 406 => Moneybird::HttpError::NotAccepted,
14
+ 422 => Moneybird::HttpError::UnprocessableEntity,
15
+ 429 => Moneybird::HttpError::TooManyRequests,
16
+ 500 => Moneybird::HttpError::InternalServerError
17
+ }.freeze
18
+
19
+ def on_complete(response)
20
+ key = response[:status].to_i
21
+ raise ERROR_MAP[key], response if ERROR_MAP.key? key
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,29 @@
1
+ require 'link_header'
2
+
3
+ module Moneybird
4
+ module Middleware
5
+ class Pagination < Faraday::Response::Middleware
6
+ def on_complete(response)
7
+ return unless response[:response_headers]
8
+ link_header = response[:response_headers][:link]
9
+ return unless link_header
10
+ response[:response_headers][:pagination_links] = find_links(link_header)
11
+ end
12
+
13
+ private
14
+
15
+ def find_links(link_header)
16
+ Links.new.tap do |links|
17
+ %w[prev next first last current].each do |page|
18
+ links.public_send("#{page}=", find_link(link_header, page))
19
+ end
20
+ end
21
+ end
22
+
23
+ def find_link(header, rel)
24
+ link = ::LinkHeader.parse(header).links.find { |l| l['rel'] == rel }
25
+ link.to_a.first if link
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,17 @@
1
+ require 'link_header'
2
+
3
+ module Moneybird
4
+ module Middleware
5
+ class Pagination < Faraday::Response::Middleware
6
+ class Links
7
+ attr_accessor :first, :prev, :current, :next, :last
8
+ alias previous prev
9
+ alias prevous= prev=
10
+
11
+ def last_page?
12
+ !current.nil? && !last.nil? && current == last
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -1,6 +1,5 @@
1
1
  module Moneybird
2
2
  module Resource
3
- attr_reader :attributes
4
3
  attr_accessor :errors, :client
5
4
 
6
5
  def initialize(attributes)
@@ -63,5 +63,25 @@ module Moneybird::Resource
63
63
  def financial_mutations
64
64
  Moneybird::Service::FinancialMutation.new(@client, id)
65
65
  end
66
+
67
+ def general_documents
68
+ Moneybird::Service::Document::GeneralDocument.new(@client, id)
69
+ end
70
+
71
+ def general_journal_documents
72
+ Moneybird::Service::Document::GeneralJournalDocument.new(@client, id)
73
+ end
74
+
75
+ def receipts
76
+ Moneybird::Service::Document::Receipt.new(@client, id)
77
+ end
78
+
79
+ def purchase_invoice_documents
80
+ Moneybird::Service::Document::PurchaseInvoice.new(@client, id)
81
+ end
82
+
83
+ def typeless_documents
84
+ Moneybird::Service::Document::TypelessDocument.new(@client, id)
85
+ end
66
86
  end
67
87
  end
@@ -4,50 +4,51 @@ module Moneybird::Resource
4
4
  extend Moneybird::Resource::ClassMethods
5
5
 
6
6
  has_attributes %i(
7
- id
8
- administration_id
9
- company_name
10
- firstname
11
- lastname
12
- attention
13
7
  address1
14
8
  address2
15
- zipcode
9
+ administration_id
10
+ attention
11
+ bank_account
12
+ chamber_of_commerce
16
13
  city
14
+ company_name
17
15
  country
16
+ created_at
17
+ credit_card_number
18
+ credit_card_reference
19
+ credit_card_type
20
+ custom_fields
21
+ customer_id
22
+ delivery_method
18
23
  email
24
+ email_ubl
25
+ estimate_workflow_id
26
+ events
27
+ firstname
28
+ id
29
+ invoice_workflow_id
30
+ lastname
31
+ notes
19
32
  phone
20
- send_method
21
- delivery_method
22
- customer_id
23
- tax_number
24
- chamber_of_commerce
25
- bank_account
26
- send_invoices_to_attention
27
- send_invoices_to_email
33
+ sales_invoices_url
28
34
  send_estimates_to_attention
29
35
  send_estimates_to_email
36
+ send_invoices_to_attention
37
+ send_invoices_to_email
38
+ send_method
30
39
  sepa_active
40
+ sepa_bic
31
41
  sepa_iban
32
42
  sepa_iban_account_name
33
- sepa_bic
34
- sepa_mandate_id
35
43
  sepa_mandate_date
44
+ sepa_mandate_id
36
45
  sepa_sequence_type
37
- credit_card_number
38
- credit_card_reference
39
- credit_card_type
40
- tax_number_validated_at
46
+ tax_number
41
47
  tax_number_valid
42
- created_at
48
+ tax_number_validated_at
43
49
  updated_at
44
- notes
45
- custom_fields
46
- events
47
- email_ubl
48
- sales_invoices_url
49
- invoice_workflow_id
50
- estimate_workflow_id
50
+ version
51
+ zipcode
51
52
  )
52
53
 
53
54
  def notes=(notes)
@@ -0,0 +1,25 @@
1
+ module Moneybird::Resource::Documents
2
+ class GeneralDocument
3
+ include Moneybird::Resource
4
+ extend Moneybird::Resource::ClassMethods
5
+
6
+ has_attributes %i(
7
+ id
8
+ administration_id
9
+ contact
10
+ contact_id
11
+ reference
12
+ date
13
+ due_date
14
+ entry_number
15
+ state
16
+ exchange_rate
17
+ created_at
18
+ updated_at
19
+ version
20
+ notes
21
+ attachments
22
+ events
23
+ )
24
+ end
25
+ end