easybill-rest-client 2.0.2

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 (87) hide show
  1. checksums.yaml +7 -0
  2. data/Gemfile +6 -0
  3. data/Gemfile.lock +152 -0
  4. data/README.md +8 -0
  5. data/Rakefile +4 -0
  6. data/config.json +5 -0
  7. data/easybill-rest-client.gemspec +29 -0
  8. data/lib/easybill-rest-client.rb +15 -0
  9. data/lib/easybill_rest_client/.document_address.rb.swp +0 -0
  10. data/lib/easybill_rest_client/.document_item.rb.swp +0 -0
  11. data/lib/easybill_rest_client/.document_payment.rb.swp +0 -0
  12. data/lib/easybill_rest_client/.document_recurring_options.rb.swp +0 -0
  13. data/lib/easybill_rest_client/.easybill_error.rb.swp +0 -0
  14. data/lib/easybill_rest_client/.log_formatter.rb.swp +0 -0
  15. data/lib/easybill_rest_client/.request_builder.rb.swp +0 -0
  16. data/lib/easybill_rest_client/.too_many_requests_error.rb.swp +0 -0
  17. data/lib/easybill_rest_client/api_client.rb +61 -0
  18. data/lib/easybill_rest_client/client.rb +39 -0
  19. data/lib/easybill_rest_client/customer.rb +70 -0
  20. data/lib/easybill_rest_client/customer_api.rb +15 -0
  21. data/lib/easybill_rest_client/document.rb +53 -0
  22. data/lib/easybill_rest_client/document_address.rb +19 -0
  23. data/lib/easybill_rest_client/document_api.rb +31 -0
  24. data/lib/easybill_rest_client/document_item.rb +34 -0
  25. data/lib/easybill_rest_client/document_payment.rb +15 -0
  26. data/lib/easybill_rest_client/document_payment_api.rb +15 -0
  27. data/lib/easybill_rest_client/document_recurring_options.rb +23 -0
  28. data/lib/easybill_rest_client/document_service_date.rb +12 -0
  29. data/lib/easybill_rest_client/generic_api.rb +39 -0
  30. data/lib/easybill_rest_client/pdf.rb +21 -0
  31. data/lib/easybill_rest_client/post_box.rb +24 -0
  32. data/lib/easybill_rest_client/post_box_api.rb +15 -0
  33. data/lib/easybill_rest_client/request.rb +107 -0
  34. data/lib/easybill_rest_client/request_logger.rb +24 -0
  35. data/lib/easybill_rest_client/response.rb +36 -0
  36. data/lib/easybill_rest_client/retry_on.rb +46 -0
  37. data/lib/easybill_rest_client/version.rb +5 -0
  38. data/log/.gitkeep +0 -0
  39. data/log/test.log +5723 -0
  40. data/script/.generate.swo +0 -0
  41. data/spec/.spec_helper.rb.swp +0 -0
  42. data/spec/fixtures/vcr/EasybillRestClient_CustomerApi/_customers_get/.returns_customers.yml.swo +0 -0
  43. data/spec/fixtures/vcr/EasybillRestClient_CustomerApi/_customers_get/.returns_customers.yml.swp +0 -0
  44. data/spec/fixtures/vcr/EasybillRestClient_CustomerApi/a_list_of_customers/gets_all_customers.yml +84 -0
  45. data/spec/fixtures/vcr/EasybillRestClient_CustomerApi/a_list_of_customers/gets_customers_matching_a_filter.yml +72 -0
  46. data/spec/fixtures/vcr/EasybillRestClient_CustomerApi/creates_updates_and_deletes_customer.yml +377 -0
  47. data/spec/fixtures/vcr/EasybillRestClient_Document/_all/returns_documents.yml +44 -0
  48. data/spec/fixtures/vcr/EasybillRestClient_DocumentApi/_cancel/cancels_a_document.yml +212 -0
  49. data/spec/fixtures/vcr/EasybillRestClient_DocumentApi/_create/creates_a_document.yml +53 -0
  50. data/spec/fixtures/vcr/EasybillRestClient_DocumentApi/_delete/deletes_a_document.yml +174 -0
  51. data/spec/fixtures/vcr/EasybillRestClient_DocumentApi/_find/returns_a_document.yml +57 -0
  52. data/spec/fixtures/vcr/EasybillRestClient_DocumentApi/_find_all/paid_and_unpaid_documents_exist/returns_only_unpaid_documents.yml +795 -0
  53. data/spec/fixtures/vcr/EasybillRestClient_DocumentApi/_find_all/returns_documents.yml +58 -0
  54. data/spec/fixtures/vcr/EasybillRestClient_DocumentApi/_find_all/returns_documents_by_number.yml +58 -0
  55. data/spec/fixtures/vcr/EasybillRestClient_DocumentApi/_finish/marks_a_drafted_document_as_finished.yml +212 -0
  56. data/spec/fixtures/vcr/EasybillRestClient_DocumentApi/_get_pdf/returns_a_PDF.yml +3889 -0
  57. data/spec/fixtures/vcr/EasybillRestClient_DocumentApi/_send_email/passes_on_additional_params.yml +63 -0
  58. data/spec/fixtures/vcr/EasybillRestClient_DocumentApi/_send_email/sends_an_email.yml +63 -0
  59. data/spec/fixtures/vcr/EasybillRestClient_DocumentApi/_update/updates_a_document.yml +107 -0
  60. data/spec/fixtures/vcr/EasybillRestClient_DocumentPaymentApi/_create/creates_a_document_payment.yml +195 -0
  61. data/spec/fixtures/vcr/EasybillRestClient_DocumentPaymentApi/_delete/deletes_a_document_payment.yml +168 -0
  62. data/spec/fixtures/vcr/EasybillRestClient_DocumentPaymentApi/_document_payments_get/returns_a_document_s_payments.yml +460 -0
  63. data/spec/fixtures/vcr/EasybillRestClient_DocumentPaymentApi/_document_payments_post/creates_a_document_payment.yml +47 -0
  64. data/spec/fixtures/vcr/EasybillRestClient_DocumentPaymentApi/_find/returns_a_document_payment.yml +66 -0
  65. data/spec/fixtures/vcr/EasybillRestClient_DocumentPaymentApi/_find_all/returns_document_payments.yml +66 -0
  66. data/spec/fixtures/vcr/EasybillRestClient_PostBoxApi/_delete/deletes_a_post_box.yml +174 -0
  67. data/spec/fixtures/vcr/EasybillRestClient_PostBoxApi/_find/returns_a_post_box.yml +72 -0
  68. data/spec/fixtures/vcr/EasybillRestClient_PostBoxApi/_find_all/returns_post_boxes.yml +73 -0
  69. data/spec/fixtures/vcr/EasybillRestClient_PostBoxApi/_post_boxes_get/returns_post_boxes.yml +52 -0
  70. data/spec/integration/.customer_api_spec.rb.swn +0 -0
  71. data/spec/integration/.customer_api_spec.rb.swo +0 -0
  72. data/spec/integration/customer_api_spec.rb +47 -0
  73. data/spec/integration/easybill_rest_client/document_api_spec.rb +129 -0
  74. data/spec/integration/easybill_rest_client/document_payment_api_spec.rb +55 -0
  75. data/spec/integration/easybill_rest_client/post_box_api_spec.rb +31 -0
  76. data/spec/lib/easybill_rest_client/.api_client_spec.rb.swp +0 -0
  77. data/spec/lib/easybill_rest_client/easybill_rest_api/.generic_api_spec.rb.swp +0 -0
  78. data/spec/lib/easybill_rest_client/generic_api_spec.rb +55 -0
  79. data/spec/lib/easybill_rest_client/pdf_spec.rb +29 -0
  80. data/spec/lib/easybill_rest_client/request_logger_spec.rb +14 -0
  81. data/spec/lib/easybill_rest_client/request_spec.rb +32 -0
  82. data/spec/lib/easybill_rest_client/retry_on_spec.rb +37 -0
  83. data/spec/spec_helper.rb +33 -0
  84. data/spec/support/.setup_api_client.rb.swo +0 -0
  85. data/spec/support/setup_client.rb +12 -0
  86. data/swagger.json +3964 -0
  87. metadata +257 -0
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.describe EasybillRestClient::GenericApi do
4
+ class Resource
5
+ attr_reader :attributes
6
+
7
+ def initialize(attributes)
8
+ @attributes = attributes
9
+ end
10
+
11
+ def id
12
+ attributes[:id]
13
+ end
14
+
15
+ def method_missing(method)
16
+ respond_to_missing?(method) ? attributes[method] : super
17
+ end
18
+
19
+ def respond_to_missing?(method)
20
+ attributes.key?(method)
21
+ end
22
+ end
23
+
24
+ class ApiClass < EasybillRestClient::GenericApi
25
+ def resource_class
26
+ Resource
27
+ end
28
+
29
+ def resource_name
30
+ 'resource'
31
+ end
32
+ end
33
+
34
+ let(:api_client) { instance_double(EasybillRestClient::ApiClient) }
35
+
36
+ subject(:api) { ApiClass.new(api_client) }
37
+
38
+ describe '#save' do
39
+ context 'entity does not exist' do
40
+ it 'creates it' do
41
+ expect(api_client).to receive(:request).with(:post, '/resource', foo: 'bar')
42
+
43
+ api.save(Resource.new(foo: 'bar'))
44
+ end
45
+ end
46
+
47
+ context 'entity does exist' do
48
+ it 'updates it' do
49
+ expect(api_client).to receive(:request).with(:put, '/resource/1', id: 1, foo: 'bar')
50
+
51
+ api.save(Resource.new(id: 1, foo: 'bar'))
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.describe EasybillRestClient::Pdf do
4
+ describe '#filename' do
5
+ context 'filename ends with ".pdf"' do
6
+ subject(:pdf_asset) { described_class.new(filename: 'something.pdf', content: '') }
7
+
8
+ it 'returns the original filename' do
9
+ expect(pdf_asset.filename).to eq('something.pdf')
10
+ end
11
+ end
12
+
13
+ context 'filename does not end with ".pdf"' do
14
+ subject(:pdf_asset) { described_class.new(filename: 'something', content: '') }
15
+
16
+ it 'appends ".pdf" to the filename' do
17
+ expect(pdf_asset.filename).to eq('something.pdf')
18
+ end
19
+ end
20
+
21
+ context 'filename ends with ".PDF"' do
22
+ subject(:pdf_asset) { described_class.new(filename: 'SOMETHING.PDF', content: '') }
23
+
24
+ it 'returns the original filename' do
25
+ expect(pdf_asset.filename).to eq('SOMETHING.PDF')
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.describe EasybillRestClient::RequestLogger do
4
+ let(:logger) { instance_double(Logger) }
5
+
6
+ subject(:request_logger) { described_class.new(logger: logger, request_id: '123') }
7
+
8
+ %i[info warn error].each do |method|
9
+ it "logs #{method}s" do
10
+ expect(logger).to receive(method).with('[easybill-rest-client] RequestId=123 foo')
11
+ request_logger.public_send(method, 'foo')
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.describe EasybillRestClient::Request do
4
+ subject(:request) do
5
+ described_class.new(
6
+ api_key: 'api-key',
7
+ method: :get,
8
+ endpoint: '/',
9
+ params: {},
10
+ logger: Logger.new(nil),
11
+ tries: 2,
12
+ retry_cool_off_time: 0
13
+ )
14
+ end
15
+
16
+ context 'too many requests have been sent' do
17
+ let(:too_many_requests) { Net::HTTPTooManyRequests.new(nil, '429', nil) }
18
+ let(:ok) do
19
+ response = Net::HTTPOK.new(nil, '200', nil)
20
+ response.body = 'BODYBODYBODY'
21
+ response.instance_variable_set('@read', true)
22
+ response.content_type = 'application/json'
23
+ response
24
+ end
25
+
26
+ it 'retries api calls that return a "Too Many Requests" error' do
27
+ allow(Net::HTTP).to receive(:start).and_return(too_many_requests, ok)
28
+
29
+ expect(request.run.body).to eq('BODYBODYBODY')
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.describe EasybillRestClient::RetryOn do
4
+ subject(:retry_handler) { described_class.new(logger, 2, 0) }
5
+
6
+ let(:logger) { instance_double(Logger) }
7
+ let(:open_timeout) { EasybillRestClient::Request::OPEN_TIMEOUT }
8
+
9
+ context 'timeout while opening connection' do
10
+ it 'retries the request' do
11
+ expect(logger).to receive(:warn)
12
+ .with("Unable to open connection after #{open_timeout}s, retrying...")
13
+
14
+ raise_error = true
15
+ retry_handler.retry_on(Net::OpenTimeout) do
16
+ if raise_error
17
+ raise_error = false
18
+ raise Net::OpenTimeout
19
+ end
20
+ end
21
+ end
22
+ end
23
+
24
+ context 'too many requests' do
25
+ it 'retries the request' do
26
+ expect(logger).to receive(:warn).with('Too many requests!')
27
+
28
+ raise_error = true
29
+ retry_handler.retry_on(EasybillRestClient::TooManyRequests) do
30
+ if raise_error
31
+ raise_error = false
32
+ raise EasybillRestClient::TooManyRequests
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'pry'
4
+ require 'vcr'
5
+ Dir[File.expand_path('../support/*.rb', __FILE__)].each { |rb| require rb }
6
+ require 'easybill-rest-client'
7
+ require 'webmock/rspec'
8
+
9
+ RSpec.configure do |config|
10
+ config.include SetupClient
11
+
12
+ config.expect_with :rspec do |expectations|
13
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
14
+ end
15
+
16
+ config.mock_with :rspec do |mocks|
17
+ mocks.verify_partial_doubles = true
18
+ end
19
+ end
20
+
21
+ VCR.configure do |c|
22
+ c.default_cassette_options = { match_requests_on: %i[method uri body] }
23
+ c.configure_rspec_metadata!
24
+ c.cassette_library_dir = 'spec/fixtures/vcr'
25
+ c.hook_into :webmock
26
+ c.filter_sensitive_data('easybill-username') { ENV.fetch('EASYBILL_USERNAME') }
27
+ c.filter_sensitive_data('easybill-api-key') { ENV.fetch('EASYBILL_API_KEY') }
28
+ c.filter_sensitive_data('easybill-basic-auth-key') { ENV.fetch('EASYBILL_BASIC_AUTH_KEY') }
29
+ end
30
+
31
+ ENV['EASYBILL_USERNAME'] ||= 'easybill-username'
32
+ ENV['EASYBILL_API_KEY'] ||= 'easybill-api-key'
33
+ ENV['EASYBILL_BASIC_AUTH_KEY'] ||= 'easybill-basic-auth-key'
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'logger'
4
+
5
+ module SetupClient
6
+ def client
7
+ @client ||= EasybillRestClient::Client.new(
8
+ api_key: ENV.fetch('EASYBILL_API_KEY'),
9
+ logger: Logger.new('log/test.log')
10
+ )
11
+ end
12
+ end