fakturoid 0.4.0 → 1.0.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 (73) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +10 -11
  3. data/.ruby-version +1 -1
  4. data/CHANGELOG.md +15 -0
  5. data/README.md +597 -107
  6. data/Rakefile +3 -1
  7. data/fakturoid.gemspec +36 -25
  8. data/lib/fakturoid/api/account.rb +13 -0
  9. data/lib/fakturoid/api/bank_account.rb +13 -0
  10. data/lib/fakturoid/api/base.rb +18 -0
  11. data/lib/fakturoid/api/event.rb +23 -0
  12. data/lib/fakturoid/api/expense.rb +55 -0
  13. data/lib/fakturoid/api/expense_payment.rb +20 -0
  14. data/lib/fakturoid/api/generator.rb +36 -0
  15. data/lib/fakturoid/api/inbox_file.rb +34 -0
  16. data/lib/fakturoid/api/inventory_item.rb +66 -0
  17. data/lib/fakturoid/api/inventory_move.rb +40 -0
  18. data/lib/fakturoid/api/invoice.rb +62 -0
  19. data/lib/fakturoid/api/invoice_message.rb +14 -0
  20. data/lib/fakturoid/api/invoice_payment.rb +26 -0
  21. data/lib/fakturoid/api/number_format.rb +13 -0
  22. data/lib/fakturoid/api/recurring_generator.rb +36 -0
  23. data/lib/fakturoid/api/subject.rb +42 -0
  24. data/lib/fakturoid/api/todo.rb +20 -0
  25. data/lib/fakturoid/api/user.rb +17 -0
  26. data/lib/fakturoid/api.rb +84 -9
  27. data/lib/fakturoid/client.rb +46 -10
  28. data/lib/fakturoid/config.rb +69 -12
  29. data/lib/fakturoid/oauth/access_token_service.rb +46 -0
  30. data/lib/fakturoid/oauth/credentials.rb +63 -0
  31. data/lib/fakturoid/oauth/flow/authorization_code.rb +53 -0
  32. data/lib/fakturoid/oauth/flow/base.rb +42 -0
  33. data/lib/fakturoid/oauth/flow/client_credentials.rb +27 -0
  34. data/lib/fakturoid/oauth/flow.rb +5 -0
  35. data/lib/fakturoid/oauth/request/api.rb +11 -0
  36. data/lib/fakturoid/oauth/request/base.rb +60 -0
  37. data/lib/fakturoid/oauth/request/oauth.rb +24 -0
  38. data/lib/fakturoid/oauth/request.rb +5 -0
  39. data/lib/fakturoid/oauth/token_response.rb +56 -0
  40. data/lib/fakturoid/oauth.rb +39 -0
  41. data/lib/fakturoid/response.rb +8 -20
  42. data/lib/fakturoid/utils.rb +27 -0
  43. data/lib/fakturoid/version.rb +1 -1
  44. data/lib/fakturoid.rb +22 -22
  45. metadata +48 -51
  46. data/.github/workflows/rubocop.yml +0 -20
  47. data/.github/workflows/tests.yml +0 -27
  48. data/.gitignore +0 -7
  49. data/Gemfile +0 -6
  50. data/lib/fakturoid/api/arguments.rb +0 -21
  51. data/lib/fakturoid/api/http_methods.rb +0 -23
  52. data/lib/fakturoid/client/account.rb +0 -11
  53. data/lib/fakturoid/client/bank_account.rb +0 -11
  54. data/lib/fakturoid/client/event.rb +0 -19
  55. data/lib/fakturoid/client/expense.rb +0 -49
  56. data/lib/fakturoid/client/generator.rb +0 -44
  57. data/lib/fakturoid/client/invoice.rb +0 -73
  58. data/lib/fakturoid/client/number_format.rb +0 -11
  59. data/lib/fakturoid/client/subject.rb +0 -41
  60. data/lib/fakturoid/client/todo.rb +0 -18
  61. data/lib/fakturoid/client/user.rb +0 -20
  62. data/lib/fakturoid/connection.rb +0 -30
  63. data/lib/fakturoid/request.rb +0 -31
  64. data/test/api_test.rb +0 -24
  65. data/test/config_test.rb +0 -40
  66. data/test/fixtures/blocked_account.json +0 -8
  67. data/test/fixtures/invoice.json +0 -81
  68. data/test/fixtures/invoice.pdf +0 -0
  69. data/test/fixtures/invoice_error.json +0 -7
  70. data/test/fixtures/subjects.json +0 -52
  71. data/test/request_test.rb +0 -20
  72. data/test/response_test.rb +0 -189
  73. data/test/test_helper.rb +0 -19
@@ -1,73 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Fakturoid
4
- module Client
5
- class Invoice < Fakturoid::Api
6
- INDEX_PARAMS = [:page, :since, :until, :updated_since, :updated_until, :number, :status, :subject_id, :custom_id].freeze
7
-
8
- def self.all(params = {})
9
- request_params = permit_params(params, *INDEX_PARAMS) || {}
10
-
11
- get_request("invoices.json", request_params: request_params)
12
- end
13
-
14
- def self.regular(params = {})
15
- request_params = permit_params(params, *INDEX_PARAMS) || {}
16
-
17
- get_request("invoices/regular.json", request_params: request_params)
18
- end
19
-
20
- def self.proforma(params = {})
21
- request_params = permit_params(params, *INDEX_PARAMS) || {}
22
-
23
- get_request("invoices/proforma.json", request_params: request_params)
24
- end
25
-
26
- def self.find(id)
27
- validate_numerical_id(id)
28
- get_request("invoices/#{id}.json")
29
- end
30
-
31
- def self.search(query, params = {})
32
- validate_search_query(query)
33
-
34
- request_params = permit_params(params, :page)
35
- request_params[:query] = query
36
-
37
- get_request("invoices/search.json", request_params: request_params)
38
- end
39
-
40
- def self.download_pdf(id)
41
- validate_numerical_id(id)
42
- get_request("invoices/#{id}/download.pdf", headers: { content_type: "application/pdf" })
43
- end
44
-
45
- def self.fire(id, event, params = {})
46
- request_params = permit_params(params, :paid_at, :paid_amount, :variable_symbol, :bank_account_id) || {}
47
- request_params[:event] = event
48
-
49
- validate_numerical_id(id)
50
- post_request("invoices/#{id}/fire.json", request_params: request_params)
51
- end
52
-
53
- def self.deliver_message(invoice_id, payload = {})
54
- validate_numerical_id(invoice_id)
55
- post_request("invoices/#{invoice_id}/message.json", payload: payload)
56
- end
57
-
58
- def self.create(payload = {})
59
- post_request("invoices.json", payload: payload)
60
- end
61
-
62
- def self.update(id, payload = {})
63
- validate_numerical_id(id)
64
- patch_request("invoices/#{id}.json", payload: payload)
65
- end
66
-
67
- def self.delete(id)
68
- validate_numerical_id(id)
69
- delete_request("invoices/#{id}.json")
70
- end
71
- end
72
- end
73
- end
@@ -1,11 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Fakturoid
4
- module Client
5
- class NumberFormat < Fakturoid::Api
6
- def self.invoices
7
- get_request("number_formats/invoices.json")
8
- end
9
- end
10
- end
11
- end
@@ -1,41 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Fakturoid
4
- module Client
5
- class Subject < Fakturoid::Api
6
- def self.all(params = {})
7
- request_params = permit_params(params, :page, :since, :updated_since, :custom_id) || {}
8
-
9
- get_request("subjects.json", request_params: request_params)
10
- end
11
-
12
- def self.find(id)
13
- validate_numerical_id(id)
14
- get_request("subjects/#{id}.json")
15
- end
16
-
17
- def self.search(query, params = {})
18
- validate_search_query(query)
19
-
20
- request_params = permit_params(params, :page)
21
- request_params[:query] = query
22
-
23
- get_request("subjects/search.json", request_params: request_params)
24
- end
25
-
26
- def self.create(payload = {})
27
- post_request("subjects.json", payload: payload)
28
- end
29
-
30
- def self.update(id, payload = {})
31
- validate_numerical_id(id)
32
- patch_request("subjects/#{id}.json", payload: payload)
33
- end
34
-
35
- def self.delete(id)
36
- validate_numerical_id(id)
37
- delete_request("subjects/#{id}.json")
38
- end
39
- end
40
- end
41
- end
@@ -1,18 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Fakturoid
4
- module Client
5
- class Todo < Fakturoid::Api
6
- def self.all(params = {})
7
- request_params = permit_params(params, :page, :since) || {}
8
-
9
- get_request("todos.json", request_params: request_params)
10
- end
11
-
12
- def self.toggle_completion(id)
13
- validate_numerical_id(id)
14
- post_request("todos/#{id}/toggle_completion.json")
15
- end
16
- end
17
- end
18
- end
@@ -1,20 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Fakturoid
4
- module Client
5
- class User < Api
6
- def self.current
7
- get_request("user.json", url: Fakturoid::Api.config.endpoint_without_account)
8
- end
9
-
10
- def self.find(id)
11
- validate_numerical_id(id)
12
- get_request("users/#{id}.json")
13
- end
14
-
15
- def self.all
16
- get_request("users.json")
17
- end
18
- end
19
- end
20
- end
@@ -1,30 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Fakturoid
4
- module Connection
5
- DEFAULT_CONTENT_TYPE = "application/json"
6
-
7
- def default_options(options = {})
8
- {
9
- headers: {
10
- content_type: options.dig(:headers, :content_type) || DEFAULT_CONTENT_TYPE,
11
- user_agent: Fakturoid::Api.config.user_agent
12
- },
13
- url: options[:url] || Fakturoid::Api.config.endpoint
14
- }
15
- end
16
-
17
- def connection(options = {})
18
- @connection = Faraday.new default_options(options)
19
-
20
- # https://lostisland.github.io/faraday/middleware/authentication
21
- if Fakturoid::Api.config.faraday_v1?
22
- @connection.request :basic_auth, Fakturoid::Api.config.email, Fakturoid::Api.config.api_key
23
- else
24
- @connection.request :authorization, :basic, Fakturoid::Api.config.email, Fakturoid::Api.config.api_key
25
- end
26
-
27
- @connection
28
- end
29
- end
30
- end
@@ -1,31 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Fakturoid
4
- class Request
5
- include Connection
6
-
7
- attr_reader :method, :path, :caller
8
-
9
- HTTP_METHODS = [:get, :post, :patch, :delete].freeze
10
-
11
- def initialize(method, path, caller)
12
- @method = method
13
- @path = path
14
- @caller = caller
15
- end
16
-
17
- def call(params = {})
18
- raise ArgumentError, "Unknown http method: #{method}" unless HTTP_METHODS.include?(method.to_sym)
19
-
20
- request_params = params[:request_params] || {}
21
-
22
- http_connection = connection(params)
23
- response = http_connection.send(method) do |req|
24
- req.url path, request_params
25
- req.headers["X-Client-Env"] = "Ruby #{RUBY_VERSION}"
26
- req.body = MultiJson.dump(params[:payload]) if params.key?(:payload)
27
- end
28
- Response.new(response, caller, method)
29
- end
30
- end
31
- end
data/test/api_test.rb DELETED
@@ -1,24 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "test_helper"
4
-
5
- class Fakturoid::ApiTest < Fakturoid::TestCase
6
- should "permit only required arguments" do
7
- hash = { page: 4, number: "2015-0015", account: 15 }
8
- permitted_params = Fakturoid::Api.permit_params(hash, :page, :number, :status)
9
- assert_equal({ page: 4, number: "2015-0015" }, permitted_params)
10
- end
11
-
12
- should "raise argument error if id is in wrong format" do
13
- assert_raises(ArgumentError) { Fakturoid::Api.validate_numerical_id(nil) }
14
- assert_raises(ArgumentError) { Fakturoid::Api.validate_numerical_id("nil") }
15
- assert Fakturoid::Api.validate_numerical_id(15)
16
- assert Fakturoid::Api.validate_numerical_id("15")
17
- end
18
-
19
- should "raise argument error if search query is not given" do
20
- assert_raises(ArgumentError) { Fakturoid::Api.validate_search_query(nil) }
21
- assert_raises(ArgumentError) { Fakturoid::Api.validate_search_query("") }
22
- assert Fakturoid::Api.validate_search_query("Company name")
23
- end
24
- end
data/test/config_test.rb DELETED
@@ -1,40 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "test_helper"
4
-
5
- class Fakturoid::ConfigTest < Fakturoid::TestCase
6
- should "configure with block param" do
7
- config = Fakturoid::Config.new do |c|
8
- c.email = "test@email.cz"
9
- c.api_key = "XXXXXXXXXXX"
10
- c.account = "testaccount"
11
- c.user_agent = "My test app (test@email.cz)"
12
- end
13
-
14
- assert_equal "test@email.cz", config.email
15
- assert_equal "XXXXXXXXXXX", config.api_key
16
- assert_equal "testaccount", config.account
17
- assert_equal "My test app (test@email.cz)", config.user_agent
18
- end
19
-
20
- should "use default user agent" do
21
- config = Fakturoid::Config.new do |c|
22
- c.email = "test@email.cz"
23
- c.api_key = "XXXXXXXXXXX"
24
- c.account = "testaccount"
25
- end
26
-
27
- assert_equal "Fakturoid ruby gem (test@email.cz)", config.user_agent
28
- end
29
-
30
- should "return correct endpoints" do
31
- config = Fakturoid::Config.new do |c|
32
- c.email = "test@email.cz"
33
- c.api_key = "XXXXXXXXXXX"
34
- c.account = "testaccount"
35
- end
36
-
37
- assert_equal "https://app.fakturoid.cz/api/v2/accounts/testaccount", config.endpoint
38
- assert_equal "https://app.fakturoid.cz/api/v2", config.endpoint_without_account
39
- end
40
- end
@@ -1,8 +0,0 @@
1
- {
2
- "status": "Account blocked",
3
- "overdue_invoices": [
4
- {
5
- "invoice_url": "http://app.fakturoid.dev/robot/p/3WfeLRO5HZ/2015-0002"
6
- }
7
- ]
8
- }
@@ -1,81 +0,0 @@
1
- {
2
- "id": 5,
3
- "proforma": false,
4
- "number": "2012-0004",
5
- "variable_symbol": "20120004",
6
- "your_name": "Alexandr Hejsek",
7
- "your_street": "Hopsinkov\u00e1 14",
8
- "your_street2": null,
9
- "your_city": "Praha",
10
- "your_zip": "10000",
11
- "your_country": "CZ",
12
- "your_registration_no": "87654321",
13
- "your_vat_no": "CZ87654321",
14
- "client_name": "Microsoft a. s.",
15
- "client_street": "Trojanova 1216/46",
16
- "client_street2": null,
17
- "client_city": "Praha",
18
- "client_zip": "11000",
19
- "client_country": "CZ",
20
- "client_registration_no": "28444501",
21
- "client_vat_no": "CZ28444501",
22
- "subject_id": 4,
23
- "generator_id": 4,
24
- "related_id": null,
25
- "correction": false,
26
- "correction_id": null,
27
- "token": "udDTG8Q88M",
28
- "status": "paid",
29
- "order_number": null,
30
- "issued_on": "2011-10-13",
31
- "taxable_fulfillment_due": "2011-10-13",
32
- "due": 10,
33
- "due_on": "2011-10-23",
34
- "sent_at": null,
35
- "paid_at": "2011-10-20T12:11:37+02:00",
36
- "reminder_sent_at": null,
37
- "accepted_at": null,
38
- "cancelled_at": null,
39
- "note": null,
40
- "footer_note": null,
41
- "bank_account": "1234/1234",
42
- "iban": null,
43
- "swift_bic": null,
44
- "payment_method": "bank",
45
- "currency": "CZK",
46
- "exchange_rate": "1.0",
47
- "language": "cz",
48
- "transferred_tax_liability": false,
49
- "vat_price_mode": "without_vat",
50
- "subtotal": "40000.0",
51
- "total": "48400.0",
52
- "native_subtotal": "40000.0",
53
- "native_total": "48400.0",
54
- "lines": [
55
- {
56
- "id": 1234,
57
- "name": "PC",
58
- "quantity": "1.0",
59
- "unit_name": "",
60
- "unit_price": "20000.0",
61
- "vat_rate": 21,
62
- "unit_price_without_vat": "20000.0",
63
- "unit_price_with_vat": "24200.0"
64
- },
65
- {
66
- "id": 1235,
67
- "name": "Notebook",
68
- "quantity": "1.0",
69
- "unit_name": "",
70
- "unit_price": "20000.0",
71
- "vat_rate": 21,
72
- "unit_price_without_vat": "20000.0",
73
- "unit_price_with_vat": "24200.0"
74
- }
75
- ],
76
- "html_url": "https://app.fakturoid.cz/applecorp/invoices/9",
77
- "public_html_url": "https://app.fakturoid.cz/applecorp/p/udDTG8Q88M/2012-0004",
78
- "url": "https://app.fakturoid.cz/api/v2/accounts/applecorp/invoices/9.json",
79
- "subject_url": "https://app.fakturoid.cz/api/v2/accounts/applecorp/subjects/4.json",
80
- "updated_at": "2012-05-13T12:11:37+02:00"
81
- }
Binary file
@@ -1,7 +0,0 @@
1
- {
2
- "errors": {
3
- "number": [
4
- "je povinná položka"
5
- ]
6
- }
7
- }
@@ -1,52 +0,0 @@
1
- [
2
- {
3
- "id": 6,
4
- "custom_id": null,
5
- "type": "customer",
6
- "name": "Apple Czech s.r.o.",
7
- "street": "Klimentská 1216/46",
8
- "street2": null,
9
- "city": "Praha",
10
- "zip": "11000",
11
- "country": "CZ",
12
- "registration_no": "28897501",
13
- "vat_no": "CZ28897501",
14
- "bank_account": null,
15
- "iban": null,
16
- "variable_symbol": "",
17
- "full_name": null,
18
- "email": "",
19
- "email_copy": null,
20
- "phone": null,
21
- "web": "https://www.apple.cz",
22
- "avatar_url": "https://ssl.fakturoid.cz/images/company-contact.png",
23
- "html_url": "https://app.fakturoid.cz/applecorp/subjects/6",
24
- "url": "https://app.fakturoid.cz/api/v2/accounts/applecorp/subjects/6.json",
25
- "updated_at": "2012-05-13T12:11:38+02:00"
26
- },
27
- {
28
- "id": 28,
29
- "custom_id": null,
30
- "type": "supplier",
31
- "name": "MICROSOFT s.r.o.",
32
- "street": "Vyskočilova 1461/2a",
33
- "street2": null,
34
- "city": "Praha",
35
- "zip": "14000",
36
- "country": "Česká republika",
37
- "registration_no": "47123737",
38
- "vat_no": "CZ47123737",
39
- "bank_account": "",
40
- "iban": "",
41
- "variable_symbol": "1234567890",
42
- "full_name": "",
43
- "email": "",
44
- "email_copy": "",
45
- "phone": "",
46
- "web": "",
47
- "avatar_url": "https://ssl.fakturoid.cz/images/company-contact.png",
48
- "html_url": "https://app.fakturoid.cz/applecorp/subjects/28",
49
- "url": "https://app.fakturoid.cz/api/v2/accounts/applecorp/subjects/28.json",
50
- "updated_at": "2012-06-02T09:34:47+02:00"
51
- }
52
- ]
data/test/request_test.rb DELETED
@@ -1,20 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "test_helper"
4
-
5
- class Fakturoid::RequestTest < Fakturoid::TestCase
6
- should "should return pdf" do
7
- pdf = load_fixture("invoice.pdf")
8
- test_connection = Faraday.new do |builder|
9
- builder.adapter :test do |stub|
10
- stub.get("invoices/5/download.pdf") { |_env| [200, { content_type: "application/pdf" }, pdf] }
11
- end
12
- builder.headers = { content_type: "application/pdf" }
13
- end
14
- Fakturoid::Request.any_instance.stubs(:connection).returns(test_connection)
15
-
16
- response = Fakturoid::Request.new(:get, "invoices/5/download.pdf", Fakturoid::Client::Invoice).call
17
- assert !response.json?
18
- assert_raises(NoMethodError) { response.name }
19
- end
20
- end
@@ -1,189 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "test_helper"
4
-
5
- class Fakturoid::ResponseTest < Fakturoid::TestCase
6
- should "should return json invoice" do
7
- json = load_fixture("invoice.json")
8
- test_connection = Faraday.new do |builder|
9
- builder.adapter :test do |stub|
10
- stub.get("invoices/5.json") { |_env| [200, { content_type: "application/json" }, json] }
11
- end
12
- builder.headers = { content_type: "application/json", user_agent: "Fakturoid gem (email@testmail.cz)" }
13
- end
14
-
15
- response = Fakturoid::Response.new(test_connection.get("invoices/5.json"), Fakturoid::Client::Invoice, :get)
16
- assert response.json?
17
- assert_equal 200, response.status_code
18
- assert_equal 5, response.id
19
- assert_equal 5, response.body["id"]
20
- assert response.respond_to?(:body)
21
- assert response.respond_to?(:id)
22
- assert_raises(NoMethodError) { response.name }
23
- end
24
-
25
- context "Exceptions" do
26
- should "raise user agent error" do
27
- test_connection = Faraday.new do |builder|
28
- builder.adapter :test do |stub|
29
- stub.get("invoices/5.json") { |_env| [400, { content_type: "application/json" }, " "] }
30
- end
31
- builder.headers = { content_type: "application/json", user_agent: "" }
32
- end
33
-
34
- assert_raises(Fakturoid::UserAgentError) { Fakturoid::Response.new(test_connection.get("invoices/5.json"), Fakturoid::Client::Invoice, :get) }
35
- end
36
-
37
- should "raise pagination error" do
38
- test_connection = Faraday.new do |builder|
39
- builder.adapter :test do |stub|
40
- stub.get("invoices.json?page=4") { |_env| [400, { content_type: "application/json" }, " "] }
41
- end
42
- builder.headers = { content_type: "application/json", user_agent: "Fakturoid gem (email@testmail.cz)" }
43
- end
44
-
45
- assert_raises(Fakturoid::PaginationError) { Fakturoid::Response.new(test_connection.get("invoices.json?page=4"), Fakturoid::Client::Invoice, :get) }
46
- end
47
-
48
- should "raise authentication error" do
49
- test_connection = Faraday.new do |builder|
50
- builder.adapter :test do |stub|
51
- stub.get("invoices.json?page=4") { |_env| [401, { content_type: "application/json" }, " "] }
52
- end
53
- builder.headers = { content_type: "application/json", user_agent: "Fakturoid gem (email@testmail.cz)" }
54
- end
55
-
56
- assert_raises(Fakturoid::AuthenticationError) { Fakturoid::Response.new(test_connection.get("invoices.json?page=4"), Fakturoid::Client::Invoice, :get) }
57
- end
58
-
59
- should "raise blocked account error" do
60
- json = load_fixture("blocked_account.json")
61
- test_connection = Faraday.new do |builder|
62
- builder.adapter :test do |stub|
63
- stub.get("account.json") { |_env| [402, { content_type: "application/json" }, json] }
64
- end
65
- builder.headers = { content_type: "application/json", user_agent: "Fakturoid gem (email@testmail.cz)" }
66
- end
67
-
68
- begin
69
- Fakturoid::Response.new(test_connection.get("account.json"), Fakturoid::Client::Account, :get)
70
- rescue Fakturoid::BlockedAccountError => e
71
- assert_equal 402, e.response_code
72
- assert e.response_body.key?("status")
73
- rescue => e
74
- assert false, "Raised exception was not expected: #{e.class}"
75
- else
76
- assert false, "Exception was expected"
77
- end
78
- end
79
-
80
- should "raise destroy subject error" do
81
- test_connection = Faraday.new do |builder|
82
- builder.adapter :test do |stub|
83
- stub.delete("subjects/5.json") { |_env| [403, { content_type: "application/json" }, " "] }
84
- end
85
- builder.headers = { content_type: "application/json", user_agent: "Fakturoid gem (email@testmail.cz)" }
86
- end
87
-
88
- assert_raises(Fakturoid::DestroySubjectError) { Fakturoid::Response.new(test_connection.delete("subjects/5.json"), Fakturoid::Client::Subject, :delete) }
89
- end
90
-
91
- should "raise subject limit error" do
92
- test_connection = Faraday.new do |builder|
93
- builder.adapter :test do |stub|
94
- stub.post("subjects.json") { |_env| [403, { content_type: "application/json" }, " "] }
95
- end
96
- builder.headers = { content_type: "application/json", user_agent: "Fakturoid gem (email@testmail.cz)" }
97
- end
98
-
99
- assert_raises(Fakturoid::SubjectLimitError) { Fakturoid::Response.new(test_connection.post("subjects.json", name: "Customer s.r.o."), Fakturoid::Client::Subject, :post) }
100
- end
101
-
102
- should "raise generator limit error" do
103
- test_connection = Faraday.new do |builder|
104
- builder.adapter :test do |stub|
105
- stub.post("generators.json") { |_env| [403, { content_type: "application/json" }, " "] }
106
- end
107
- builder.headers = { content_type: "application/json", user_agent: "Fakturoid gem (email@testmail.cz)" }
108
- end
109
-
110
- assert_raises(Fakturoid::GeneratorLimitError) { Fakturoid::Response.new(test_connection.post("generators.json", name: "Customer s.r.o.", recurring: true), Fakturoid::Client::Generator, :post) }
111
- end
112
-
113
- should "raise unsupported feature error" do
114
- test_connection = Faraday.new do |builder|
115
- builder.adapter :test do |stub|
116
- stub.post("invoices/5/message.json") { |_env| [403, { content_type: "application/json" }, " "] }
117
- end
118
- builder.headers = { content_type: "application/json", user_agent: "Fakturoid gem (email@testmail.cz)" }
119
- end
120
-
121
- assert_raises(Fakturoid::UnsupportedFeatureError) { Fakturoid::Response.new(test_connection.post("invoices/5/message.json", email: "customer@email.cz"), Fakturoid::Client::Invoice, :post) }
122
- end
123
-
124
- should "raise record not found error" do
125
- test_connection = Faraday.new do |builder|
126
- builder.adapter :test do |stub|
127
- stub.get("invoices/10.json") { |_env| [404, { content_type: "application/json" }, " "] }
128
- end
129
- builder.headers = { content_type: "application/json", user_agent: "Fakturoid gem (email@testmail.cz)" }
130
- end
131
-
132
- assert_raises(Fakturoid::RecordNotFoundError) { Fakturoid::Response.new(test_connection.get("invoices/10.json"), Fakturoid::Client::Invoice, :get) }
133
- end
134
-
135
- should "raise content type error" do
136
- test_connection = Faraday.new do |builder|
137
- builder.adapter :test do |stub|
138
- stub.get("invoices/5.xml") { |_env| [415, { content_type: "application/xml" }, " "] }
139
- end
140
- builder.headers = { content_type: "application/xml", user_agent: "Fakturoid gem (email@testmail.cz)" }
141
- end
142
-
143
- assert_raises(Fakturoid::ContentTypeError) { Fakturoid::Response.new(test_connection.get("invoices/5.xml"), Fakturoid::Client::Invoice, :get) }
144
- end
145
-
146
- should "raise invalid record error" do
147
- json = load_fixture("invoice_error.json")
148
- test_connection = Faraday.new do |builder|
149
- builder.adapter :test do |stub|
150
- stub.patch("invoice/5.json") { |_env| [422, { content_type: "application/json" }, json] }
151
- end
152
- builder.headers = { content_type: "application/json", user_agent: "Fakturoid gem (email@testmail.cz)" }
153
- end
154
-
155
- begin
156
- Fakturoid::Response.new(test_connection.patch("invoice/5.json"), Fakturoid::Client::Invoice, :patch)
157
- rescue Fakturoid::InvalidRecordError => e
158
- assert_equal 422, e.response_code
159
- assert e.response_body.key?("errors")
160
- rescue => e
161
- assert false, "Raised exception was not expected: #{e.class}"
162
- else
163
- assert false, "Exception was expected"
164
- end
165
- end
166
-
167
- should "raise rate limit error" do
168
- test_connection = Faraday.new do |builder|
169
- builder.adapter :test do |stub|
170
- stub.get("invoices/5.json") { |_env| [429, { content_type: "application/json" }, " "] }
171
- end
172
- builder.headers = { content_type: "application/json", user_agent: "Fakturoid gem (email@testmail.cz)" }
173
- end
174
-
175
- assert_raises(Fakturoid::RateLimitError) { Fakturoid::Response.new(test_connection.get("invoices/5.json"), Fakturoid::Client::Invoice, :get) }
176
- end
177
-
178
- should "raise read only site error" do
179
- test_connection = Faraday.new do |builder|
180
- builder.adapter :test do |stub|
181
- stub.delete("invoices/5.json") { |_env| [503, { content_type: "application/json" }, " "] }
182
- end
183
- builder.headers = { content_type: "application/json", user_agent: "Fakturoid gem (email@testmail.cz)" }
184
- end
185
-
186
- assert_raises(Fakturoid::ReadOnlySiteError) { Fakturoid::Response.new(test_connection.delete("invoices/5.json"), Fakturoid::Client::Invoice, :delete) }
187
- end
188
- end
189
- end