moneybird-client 0.12.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 (95) hide show
  1. checksums.yaml +7 -0
  2. data/.codeclimate.yml +26 -0
  3. data/.forgejo/workflows/release.yml +56 -0
  4. data/.forgejo/workflows/ruby.yml +37 -0
  5. data/.gitignore +10 -0
  6. data/.kick +17 -0
  7. data/.rspec +2 -0
  8. data/.rubocop.yml +1156 -0
  9. data/CODE_OF_CONDUCT.md +49 -0
  10. data/Gemfile +11 -0
  11. data/LICENSE.txt +21 -0
  12. data/README.md +110 -0
  13. data/Rakefile +11 -0
  14. data/bin/console +13 -0
  15. data/bin/setup +8 -0
  16. data/lib/moneybird/client.rb +80 -0
  17. data/lib/moneybird/http_error/authorization_required.rb +8 -0
  18. data/lib/moneybird/http_error/bad_request.rb +8 -0
  19. data/lib/moneybird/http_error/forbidden.rb +8 -0
  20. data/lib/moneybird/http_error/internal_server_error.rb +8 -0
  21. data/lib/moneybird/http_error/method_not_allowed.rb +8 -0
  22. data/lib/moneybird/http_error/not_accepted.rb +8 -0
  23. data/lib/moneybird/http_error/not_found.rb +8 -0
  24. data/lib/moneybird/http_error/payment_required.rb +8 -0
  25. data/lib/moneybird/http_error/too_many_requests.rb +8 -0
  26. data/lib/moneybird/http_error/unprocessable_entity.rb +8 -0
  27. data/lib/moneybird/middleware/error_handling.rb +29 -0
  28. data/lib/moneybird/middleware/pagination/links.rb +19 -0
  29. data/lib/moneybird/middleware/pagination.rb +31 -0
  30. data/lib/moneybird/oauth.rb +91 -0
  31. data/lib/moneybird/resource/administration.rb +101 -0
  32. data/lib/moneybird/resource/contact.rb +75 -0
  33. data/lib/moneybird/resource/custom_field.rb +18 -0
  34. data/lib/moneybird/resource/document_style.rb +35 -0
  35. data/lib/moneybird/resource/documents/general_document.rb +27 -0
  36. data/lib/moneybird/resource/documents/general_journal_document.rb +28 -0
  37. data/lib/moneybird/resource/documents/purchase_invoice.rb +57 -0
  38. data/lib/moneybird/resource/documents/receipt.rb +57 -0
  39. data/lib/moneybird/resource/documents/typeless_document.rb +28 -0
  40. data/lib/moneybird/resource/estimate.rb +64 -0
  41. data/lib/moneybird/resource/external_sales_invoice.rb +61 -0
  42. data/lib/moneybird/resource/financial_account.rb +23 -0
  43. data/lib/moneybird/resource/financial_mutation.rb +39 -0
  44. data/lib/moneybird/resource/generic/event.rb +19 -0
  45. data/lib/moneybird/resource/generic/note.rb +25 -0
  46. data/lib/moneybird/resource/identity.rb +31 -0
  47. data/lib/moneybird/resource/invoice/details.rb +29 -0
  48. data/lib/moneybird/resource/invoice/payment.rb +26 -0
  49. data/lib/moneybird/resource/ledger_account.rb +19 -0
  50. data/lib/moneybird/resource/product.rb +21 -0
  51. data/lib/moneybird/resource/recurring_sales_invoice.rb +62 -0
  52. data/lib/moneybird/resource/sales_invoice.rb +98 -0
  53. data/lib/moneybird/resource/synchronization.rb +15 -0
  54. data/lib/moneybird/resource/tax_rate.rb +21 -0
  55. data/lib/moneybird/resource/webhook.rb +24 -0
  56. data/lib/moneybird/resource/workflow.rb +24 -0
  57. data/lib/moneybird/resource.rb +126 -0
  58. data/lib/moneybird/service/administration.rb +29 -0
  59. data/lib/moneybird/service/contact.rb +27 -0
  60. data/lib/moneybird/service/custom_field.rb +19 -0
  61. data/lib/moneybird/service/document_style.rb +19 -0
  62. data/lib/moneybird/service/documents/general_document.rb +23 -0
  63. data/lib/moneybird/service/documents/general_journal_document.rb +23 -0
  64. data/lib/moneybird/service/documents/purchase_invoice.rb +23 -0
  65. data/lib/moneybird/service/documents/receipt.rb +23 -0
  66. data/lib/moneybird/service/documents/typeless_document.rb +23 -0
  67. data/lib/moneybird/service/estimate.rb +23 -0
  68. data/lib/moneybird/service/external_sales_invoice.rb +22 -0
  69. data/lib/moneybird/service/financial_account.rb +19 -0
  70. data/lib/moneybird/service/financial_mutation.rb +24 -0
  71. data/lib/moneybird/service/identity.rb +22 -0
  72. data/lib/moneybird/service/ledger_account.rb +22 -0
  73. data/lib/moneybird/service/payment.rb +21 -0
  74. data/lib/moneybird/service/product.rb +22 -0
  75. data/lib/moneybird/service/recurring_sales_invoice.rb +23 -0
  76. data/lib/moneybird/service/sales_invoice.rb +26 -0
  77. data/lib/moneybird/service/tax_rate.rb +19 -0
  78. data/lib/moneybird/service/webhook.rb +21 -0
  79. data/lib/moneybird/service/workflow.rb +19 -0
  80. data/lib/moneybird/token.rb +20 -0
  81. data/lib/moneybird/traits/administration_service.rb +16 -0
  82. data/lib/moneybird/traits/delete.rb +12 -0
  83. data/lib/moneybird/traits/download_pdf.rb +11 -0
  84. data/lib/moneybird/traits/find.rb +11 -0
  85. data/lib/moneybird/traits/find_all.rb +51 -0
  86. data/lib/moneybird/traits/mark_as_uncollectible.rb +17 -0
  87. data/lib/moneybird/traits/save.rb +26 -0
  88. data/lib/moneybird/traits/send_invoice.rb +17 -0
  89. data/lib/moneybird/traits/service.rb +15 -0
  90. data/lib/moneybird/traits/synchronization.rb +13 -0
  91. data/lib/moneybird/version.rb +5 -0
  92. data/lib/moneybird/webhook.rb +31 -0
  93. data/lib/moneybird.rb +115 -0
  94. data/moneybird.gemspec +37 -0
  95. metadata +235 -0
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at maartenvanvliet@gmail.com. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in the gemspec
6
+ gemspec
7
+
8
+ group :coverage do
9
+ gem "simplecov", "~> 0.22"
10
+ gem "simplecov-cobertura", "~> 2.1"
11
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Maarten van Vliet
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,110 @@
1
+ # Moneybird [![CI](https://codeberg.org/odeva/moneybird/actions/workflows/ruby.yml/badge.svg)](https://codeberg.org/odeva/moneybird/actions/workflows/ruby.yml)
2
+
3
+ Ruby client for the [Moneybird REST API](https://developer.moneybird.com/). Supports both personal API tokens and OAuth2 authentication.
4
+
5
+ > [!IMPORTANT]
6
+ > This is a fork of [maartenvanvliet/moneybird](https://github.com/maartenvanvliet/moneybird), this is a very solid implementation that I really wanted to build on top of.
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'moneybird-client', require: 'moneybird'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ ## Usage
21
+
22
+ ```ruby
23
+ client = Moneybird::Client.new('your_bearer_token')
24
+
25
+ # List administrations
26
+ administrations = client.administrations
27
+
28
+ administration = administrations.first
29
+
30
+ # List invoices
31
+ administration.sales_invoices.all
32
+
33
+ # List invoices with a specific state
34
+ administration.sales_invoices.all(filter: 'state:uncollectible')
35
+
36
+ # List contacts
37
+ administration.contacts.all
38
+
39
+ # Find contact
40
+ administration.contacts.find(moneybird_id)
41
+
42
+ # Find contact by customer id
43
+ administration.contacts.find_by_customer_id(customer_id)
44
+
45
+ # Create contact
46
+ administration.contacts.create(company_name: 'ACME', firstname: 'Foo', lastname: 'Bar')
47
+
48
+ # Update contact
49
+ contact = administration.contacts.all.first
50
+ contact.company_name = 'Something new'
51
+ administrations.contacts.save(contact)
52
+
53
+ # Delete contact
54
+ administrations.contacts.delete(contact)
55
+
56
+ # Works similarly with other resources
57
+
58
+ ```
59
+ ### OAuth2
60
+
61
+ Moneybird uses OAuth2 for authentication. Use `Moneybird::OAuth` to handle the flow:
62
+
63
+ ```ruby
64
+ oauth = Moneybird::OAuth.new(
65
+ client_id: "your_client_id",
66
+ client_secret: "your_client_secret",
67
+ redirect_uri: "https://yourapp.com/callback"
68
+ )
69
+
70
+ # Step 1: Redirect user
71
+ redirect_to oauth.authorize_url(scope: %w[sales_invoices documents])
72
+
73
+ # Step 2: Exchange code (in callback)
74
+ token = oauth.token(params[:code])
75
+
76
+ # Step 3: Use the token
77
+ client = token.client
78
+ administrations = client.administrations
79
+
80
+ # Step 4: Refresh when needed
81
+ new_token = oauth.refresh(token.refresh_token)
82
+ ```
83
+
84
+ Available scopes: `sales_invoices`, `documents`, `estimates`, `bank`, `time_entries`, `settings`.
85
+
86
+ The `authorize_url` method accepts an optional `state` parameter for CSRF protection.
87
+
88
+ ### Webhooks
89
+
90
+ Moneybird, if so configured, sends webhooks to specified endpoints. This gem can deal with these requests.
91
+ ```ruby
92
+ webhook = Moneybird::Webhook.from_json(request.body)
93
+ sales_invoice = webhook.build_entity
94
+ sales_invoice.state
95
+ ```
96
+
97
+ ## Development
98
+
99
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
100
+
101
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
102
+
103
+ ## Contributing
104
+
105
+ Bug reports and pull requests are welcome on Codeberg at https://codeberg.org/odeva/moneybird. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
106
+
107
+
108
+ ## License
109
+
110
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rake/testtask"
5
+
6
+ Rake::TestTask.new do |t|
7
+ t.libs.push 'spec'
8
+ t.test_files = FileList['spec/**/*_spec.rb']
9
+ end
10
+
11
+ task default: :test
data/bin/console ADDED
@@ -0,0 +1,13 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "moneybird"
5
+ require "irb"
6
+
7
+ if ENV['MONEYBIRD_API_TOKEN']
8
+ @client = Moneybird::Client.new(ENV['MONEYBIRD_API_TOKEN'])
9
+ end
10
+
11
+ IRB.start
12
+
13
+
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,80 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'faraday'
4
+
5
+ module Moneybird
6
+ class Client
7
+ attr_reader :bearer_token
8
+ attr_accessor :errors
9
+ attr_writer :http, :faraday_adapter
10
+
11
+ def initialize(bearer_token)
12
+ @bearer_token = bearer_token
13
+ end
14
+
15
+ def base_url
16
+ "https://moneybird.com/"
17
+ end
18
+
19
+ def version
20
+ @version ||= 'v2'
21
+ end
22
+
23
+ def faraday_adapter
24
+ @faraday_adapter ||= Faraday.default_adapter
25
+ end
26
+
27
+ def http
28
+ @http ||= Faraday.new(url: base_url) do |faraday|
29
+ faraday.headers = faraday_headers
30
+ faraday.request :url_encoded
31
+ faraday.response :json
32
+ faraday.use Moneybird::Middleware::ErrorHandling
33
+ faraday.use Moneybird::Middleware::Pagination
34
+
35
+ faraday.adapter faraday_adapter
36
+ end
37
+ end
38
+
39
+ %i[get patch post delete].each do |call|
40
+ define_method call do |path, options = {}|
41
+ response = http.public_send(call, "/api/#{version}/#{path}", options)
42
+ if response.status == 302
43
+ response['Location']
44
+ else
45
+ response.body
46
+ end
47
+ end
48
+ end
49
+
50
+ def get_all_pages(path, options = {})
51
+ get_each_page(path, options).inject([]) do |array, objects|
52
+ array += objects
53
+ end
54
+ end
55
+
56
+ def get_each_page(path, options = {})
57
+ return enum_for(:get_each_page, path, options) unless block_given?
58
+ path = "/api/#{version}/#{path}"
59
+ while path
60
+ response = http.get(path, options)
61
+ yield response.body
62
+ path = (response[:pagination_links].next if response[:pagination_links])
63
+ end
64
+ end
65
+
66
+ def administrations
67
+ Moneybird::Service::Administration.new(self).all
68
+ end
69
+
70
+ private
71
+
72
+ def faraday_headers
73
+ {
74
+ 'Accept' => 'application/json',
75
+ 'Content-Type' => 'application/json',
76
+ 'Authorization' => "Bearer #{bearer_token}"
77
+ }
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Moneybird
4
+ module HttpError
5
+ class AuthorizationRequired < Faraday::Error
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Moneybird
4
+ module HttpError
5
+ class BadRequest < Faraday::Error
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Moneybird
4
+ module HttpError
5
+ class Forbidden < Faraday::Error
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Moneybird
4
+ module HttpError
5
+ class InternalServerError < Faraday::Error
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Moneybird
4
+ module HttpError
5
+ class MethodNotAllowed < Faraday::Error
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Moneybird
4
+ module HttpError
5
+ class NotAccepted < Faraday::Error
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Moneybird
4
+ module HttpError
5
+ class NotFound < Faraday::Error
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Moneybird
4
+ module HttpError
5
+ class PaymentRequired < Faraday::Error
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Moneybird
4
+ module HttpError
5
+ class TooManyRequests < Faraday::Error
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Moneybird
4
+ module HttpError
5
+ class UnprocessableEntity < Faraday::Error
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Moneybird
4
+ module Middleware
5
+ class ErrorHandling < Faraday::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_values(response) if ERROR_MAP.key? key
22
+ end
23
+
24
+ def response_values(response)
25
+ { status: response.status, headers: response.response_headers, body: response.body }
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'link_header'
4
+
5
+ module Moneybird
6
+ module Middleware
7
+ class Pagination < Faraday::Middleware
8
+ class Links
9
+ attr_accessor :first, :prev, :current, :next, :last
10
+ alias previous prev
11
+ alias prevous= prev=
12
+
13
+ def last_page?
14
+ !current.nil? && !last.nil? && current == last
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'link_header'
4
+
5
+ module Moneybird
6
+ module Middleware
7
+ class Pagination < Faraday::Middleware
8
+ def on_complete(response)
9
+ return unless response[:response_headers]
10
+ link_header = response[:response_headers][:link]
11
+ return unless link_header
12
+ response[:response_headers][:pagination_links] = find_links(link_header)
13
+ end
14
+
15
+ private
16
+
17
+ def find_links(link_header)
18
+ Links.new.tap do |links|
19
+ %w[prev next first last current].each do |page|
20
+ links.public_send("#{page}=", find_link(link_header, page))
21
+ end
22
+ end
23
+ end
24
+
25
+ def find_link(header, rel)
26
+ link = ::LinkHeader.parse(header).links.find { |l| l['rel'] == rel }
27
+ link.to_a.first if link
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,91 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'uri'
4
+ require 'faraday'
5
+
6
+ module Moneybird
7
+ class OAuth
8
+ AUTHORIZE_URL = "https://moneybird.com/oauth/authorize"
9
+ TOKEN_URL = "https://moneybird.com/oauth/token"
10
+
11
+ SCOPES = %w[sales_invoices documents estimates bank time_entries settings].freeze
12
+
13
+ attr_reader :client_id, :client_secret, :redirect_uri
14
+
15
+ def initialize(client_id:, client_secret:, redirect_uri:)
16
+ @client_id = client_id
17
+ @client_secret = client_secret
18
+ @redirect_uri = redirect_uri
19
+ end
20
+
21
+ # Returns the URL to redirect the user to for authorization
22
+ def authorize_url(scope: nil, state: nil)
23
+ scope = scope.is_a?(Array) ? scope.join(" ") : (scope || "sales_invoices")
24
+
25
+ params = {
26
+ client_id: client_id,
27
+ redirect_uri: redirect_uri,
28
+ response_type: "code",
29
+ scope: scope
30
+ }
31
+ params[:state] = state if state
32
+
33
+ uri = URI.parse(AUTHORIZE_URL)
34
+ uri.query = URI.encode_www_form(params)
35
+ uri.to_s
36
+ end
37
+
38
+ # Exchange authorization code for tokens
39
+ # Returns a Token object
40
+ def token(code)
41
+ response = post_token(
42
+ grant_type: "authorization_code",
43
+ client_id: client_id,
44
+ client_secret: client_secret,
45
+ code: code,
46
+ redirect_uri: redirect_uri
47
+ )
48
+ build_token(response)
49
+ end
50
+
51
+ # Refresh an existing token
52
+ # Returns a new Token object
53
+ def refresh(refresh_token)
54
+ response = post_token(
55
+ grant_type: "refresh_token",
56
+ client_id: client_id,
57
+ client_secret: client_secret,
58
+ refresh_token: refresh_token
59
+ )
60
+ build_token(response)
61
+ end
62
+
63
+ private
64
+
65
+ def post_token(params)
66
+ connection = Faraday.new do |faraday|
67
+ faraday.request :url_encoded
68
+ faraday.response :json
69
+ faraday.adapter Faraday.default_adapter
70
+ end
71
+
72
+ response = connection.post(TOKEN_URL, params)
73
+
74
+ unless response.success?
75
+ raise "OAuth token request failed with status #{response.status}: #{response.body}"
76
+ end
77
+
78
+ response.body
79
+ end
80
+
81
+ def build_token(body)
82
+ Token.new(
83
+ access_token: body["access_token"],
84
+ refresh_token: body["refresh_token"],
85
+ token_type: body.fetch("token_type", "bearer"),
86
+ scope: body["scope"],
87
+ created_at: body["created_at"]
88
+ )
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,101 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Moneybird::Resource
4
+ class Administration
5
+ include Moneybird::Resource
6
+ extend Moneybird::Resource::ClassMethods
7
+
8
+ has_attributes %i(
9
+ id
10
+ name
11
+ language
12
+ currency
13
+ country
14
+ time_zone
15
+ )
16
+
17
+ def ledger_accounts
18
+ Moneybird::Service::LedgerAccount.new(@client, id)
19
+ end
20
+
21
+ def purchase_invoices
22
+ Moneybird::Service::PurchaseInvoice.new(@client, id)
23
+ end
24
+
25
+ def sales_invoices
26
+ Moneybird::Service::SalesInvoice.new(@client, id)
27
+ end
28
+
29
+ def external_sales_invoices
30
+ Moneybird::Service::ExternalSalesInvoice.new(@client, id)
31
+ end
32
+
33
+ def recurring_sales_invoices
34
+ Moneybird::Service::RecurringSalesInvoice.new(@client, id)
35
+ end
36
+
37
+ def contacts
38
+ Moneybird::Service::Contact.new(@client, id)
39
+ end
40
+
41
+ def estimates
42
+ Moneybird::Service::Estimate.new(@client, id)
43
+ end
44
+
45
+ def products
46
+ Moneybird::Service::Product.new(@client, id)
47
+ end
48
+
49
+ def webhooks
50
+ Moneybird::Service::Webhook.new(@client, id)
51
+ end
52
+
53
+ def identities
54
+ Moneybird::Service::Identity.new(@client, id)
55
+ end
56
+
57
+ def workflows
58
+ Moneybird::Service::Workflow.new(@client, id)
59
+ end
60
+
61
+ def custom_fields
62
+ Moneybird::Service::CustomField.new(@client, id)
63
+ end
64
+
65
+ def document_styles
66
+ Moneybird::Service::DocumentStyle.new(@client, id)
67
+ end
68
+
69
+ def tax_rates
70
+ Moneybird::Service::TaxRate.new(@client, id)
71
+ end
72
+
73
+ def financial_accounts
74
+ Moneybird::Service::FinancialAccount.new(@client, id)
75
+ end
76
+
77
+ def financial_mutations
78
+ Moneybird::Service::FinancialMutation.new(@client, id)
79
+ end
80
+
81
+ def general_documents
82
+ Moneybird::Service::Document::GeneralDocument.new(@client, id)
83
+ end
84
+
85
+ def general_journal_documents
86
+ Moneybird::Service::Document::GeneralJournalDocument.new(@client, id)
87
+ end
88
+
89
+ def receipts
90
+ Moneybird::Service::Document::Receipt.new(@client, id)
91
+ end
92
+
93
+ def purchase_invoice_documents
94
+ Moneybird::Service::Document::PurchaseInvoice.new(@client, id)
95
+ end
96
+
97
+ def typeless_documents
98
+ Moneybird::Service::Document::TypelessDocument.new(@client, id)
99
+ end
100
+ end
101
+ end