easybill-rest-client 2.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +152 -0
- data/README.md +8 -0
- data/Rakefile +4 -0
- data/config.json +5 -0
- data/easybill-rest-client.gemspec +29 -0
- data/lib/easybill-rest-client.rb +15 -0
- data/lib/easybill_rest_client/.document_address.rb.swp +0 -0
- data/lib/easybill_rest_client/.document_item.rb.swp +0 -0
- data/lib/easybill_rest_client/.document_payment.rb.swp +0 -0
- data/lib/easybill_rest_client/.document_recurring_options.rb.swp +0 -0
- data/lib/easybill_rest_client/.easybill_error.rb.swp +0 -0
- data/lib/easybill_rest_client/.log_formatter.rb.swp +0 -0
- data/lib/easybill_rest_client/.request_builder.rb.swp +0 -0
- data/lib/easybill_rest_client/.too_many_requests_error.rb.swp +0 -0
- data/lib/easybill_rest_client/api_client.rb +61 -0
- data/lib/easybill_rest_client/client.rb +39 -0
- data/lib/easybill_rest_client/customer.rb +70 -0
- data/lib/easybill_rest_client/customer_api.rb +15 -0
- data/lib/easybill_rest_client/document.rb +53 -0
- data/lib/easybill_rest_client/document_address.rb +19 -0
- data/lib/easybill_rest_client/document_api.rb +31 -0
- data/lib/easybill_rest_client/document_item.rb +34 -0
- data/lib/easybill_rest_client/document_payment.rb +15 -0
- data/lib/easybill_rest_client/document_payment_api.rb +15 -0
- data/lib/easybill_rest_client/document_recurring_options.rb +23 -0
- data/lib/easybill_rest_client/document_service_date.rb +12 -0
- data/lib/easybill_rest_client/generic_api.rb +39 -0
- data/lib/easybill_rest_client/pdf.rb +21 -0
- data/lib/easybill_rest_client/post_box.rb +24 -0
- data/lib/easybill_rest_client/post_box_api.rb +15 -0
- data/lib/easybill_rest_client/request.rb +107 -0
- data/lib/easybill_rest_client/request_logger.rb +24 -0
- data/lib/easybill_rest_client/response.rb +36 -0
- data/lib/easybill_rest_client/retry_on.rb +46 -0
- data/lib/easybill_rest_client/version.rb +5 -0
- data/log/.gitkeep +0 -0
- data/log/test.log +5723 -0
- data/script/.generate.swo +0 -0
- data/spec/.spec_helper.rb.swp +0 -0
- data/spec/fixtures/vcr/EasybillRestClient_CustomerApi/_customers_get/.returns_customers.yml.swo +0 -0
- data/spec/fixtures/vcr/EasybillRestClient_CustomerApi/_customers_get/.returns_customers.yml.swp +0 -0
- data/spec/fixtures/vcr/EasybillRestClient_CustomerApi/a_list_of_customers/gets_all_customers.yml +84 -0
- data/spec/fixtures/vcr/EasybillRestClient_CustomerApi/a_list_of_customers/gets_customers_matching_a_filter.yml +72 -0
- data/spec/fixtures/vcr/EasybillRestClient_CustomerApi/creates_updates_and_deletes_customer.yml +377 -0
- data/spec/fixtures/vcr/EasybillRestClient_Document/_all/returns_documents.yml +44 -0
- data/spec/fixtures/vcr/EasybillRestClient_DocumentApi/_cancel/cancels_a_document.yml +212 -0
- data/spec/fixtures/vcr/EasybillRestClient_DocumentApi/_create/creates_a_document.yml +53 -0
- data/spec/fixtures/vcr/EasybillRestClient_DocumentApi/_delete/deletes_a_document.yml +174 -0
- data/spec/fixtures/vcr/EasybillRestClient_DocumentApi/_find/returns_a_document.yml +57 -0
- data/spec/fixtures/vcr/EasybillRestClient_DocumentApi/_find_all/paid_and_unpaid_documents_exist/returns_only_unpaid_documents.yml +795 -0
- data/spec/fixtures/vcr/EasybillRestClient_DocumentApi/_find_all/returns_documents.yml +58 -0
- data/spec/fixtures/vcr/EasybillRestClient_DocumentApi/_find_all/returns_documents_by_number.yml +58 -0
- data/spec/fixtures/vcr/EasybillRestClient_DocumentApi/_finish/marks_a_drafted_document_as_finished.yml +212 -0
- data/spec/fixtures/vcr/EasybillRestClient_DocumentApi/_get_pdf/returns_a_PDF.yml +3889 -0
- data/spec/fixtures/vcr/EasybillRestClient_DocumentApi/_send_email/passes_on_additional_params.yml +63 -0
- data/spec/fixtures/vcr/EasybillRestClient_DocumentApi/_send_email/sends_an_email.yml +63 -0
- data/spec/fixtures/vcr/EasybillRestClient_DocumentApi/_update/updates_a_document.yml +107 -0
- data/spec/fixtures/vcr/EasybillRestClient_DocumentPaymentApi/_create/creates_a_document_payment.yml +195 -0
- data/spec/fixtures/vcr/EasybillRestClient_DocumentPaymentApi/_delete/deletes_a_document_payment.yml +168 -0
- data/spec/fixtures/vcr/EasybillRestClient_DocumentPaymentApi/_document_payments_get/returns_a_document_s_payments.yml +460 -0
- data/spec/fixtures/vcr/EasybillRestClient_DocumentPaymentApi/_document_payments_post/creates_a_document_payment.yml +47 -0
- data/spec/fixtures/vcr/EasybillRestClient_DocumentPaymentApi/_find/returns_a_document_payment.yml +66 -0
- data/spec/fixtures/vcr/EasybillRestClient_DocumentPaymentApi/_find_all/returns_document_payments.yml +66 -0
- data/spec/fixtures/vcr/EasybillRestClient_PostBoxApi/_delete/deletes_a_post_box.yml +174 -0
- data/spec/fixtures/vcr/EasybillRestClient_PostBoxApi/_find/returns_a_post_box.yml +72 -0
- data/spec/fixtures/vcr/EasybillRestClient_PostBoxApi/_find_all/returns_post_boxes.yml +73 -0
- data/spec/fixtures/vcr/EasybillRestClient_PostBoxApi/_post_boxes_get/returns_post_boxes.yml +52 -0
- data/spec/integration/.customer_api_spec.rb.swn +0 -0
- data/spec/integration/.customer_api_spec.rb.swo +0 -0
- data/spec/integration/customer_api_spec.rb +47 -0
- data/spec/integration/easybill_rest_client/document_api_spec.rb +129 -0
- data/spec/integration/easybill_rest_client/document_payment_api_spec.rb +55 -0
- data/spec/integration/easybill_rest_client/post_box_api_spec.rb +31 -0
- data/spec/lib/easybill_rest_client/.api_client_spec.rb.swp +0 -0
- data/spec/lib/easybill_rest_client/easybill_rest_api/.generic_api_spec.rb.swp +0 -0
- data/spec/lib/easybill_rest_client/generic_api_spec.rb +55 -0
- data/spec/lib/easybill_rest_client/pdf_spec.rb +29 -0
- data/spec/lib/easybill_rest_client/request_logger_spec.rb +14 -0
- data/spec/lib/easybill_rest_client/request_spec.rb +32 -0
- data/spec/lib/easybill_rest_client/retry_on_spec.rb +37 -0
- data/spec/spec_helper.rb +33 -0
- data/spec/support/.setup_api_client.rb.swo +0 -0
- data/spec/support/setup_client.rb +12 -0
- data/swagger.json +3964 -0
- 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
|
data/spec/spec_helper.rb
ADDED
@@ -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'
|
Binary file
|