boletosimples 0.4.2 → 1.0.3
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.
- checksums.yaml +5 -5
- data/CHANGELOG.md +9 -17
- data/README.md +14 -54
- data/lib/boletosimples.rb +9 -6
- data/lib/boletosimples/configuration.rb +19 -28
- data/lib/boletosimples/last_request.rb +12 -9
- data/lib/boletosimples/middlewares/bearer.rb +12 -0
- data/lib/boletosimples/middlewares/debug.rb +30 -0
- data/lib/boletosimples/middlewares/last_request.rb +3 -1
- data/lib/boletosimples/middlewares/raise_error.rb +9 -2
- data/lib/boletosimples/middlewares/user_agent.rb +3 -1
- data/lib/boletosimples/resources/bank_billet.rb +5 -11
- data/lib/boletosimples/resources/bank_billet_account.rb +3 -1
- data/lib/boletosimples/resources/bank_billet_discharge.rb +6 -0
- data/lib/boletosimples/resources/bank_billet_payment.rb +6 -0
- data/lib/boletosimples/resources/bank_billet_remittance.rb +6 -0
- data/lib/boletosimples/resources/base_model.rb +3 -1
- data/lib/boletosimples/resources/customer.rb +3 -1
- data/lib/boletosimples/resources/customer_import.rb +5 -3
- data/lib/boletosimples/resources/customer_subscription.rb +3 -10
- data/lib/boletosimples/resources/customer_subscription_import.rb +5 -3
- data/lib/boletosimples/resources/discharge.rb +4 -11
- data/lib/boletosimples/resources/email_delivery.rb +6 -0
- data/lib/boletosimples/resources/event.rb +3 -1
- data/lib/boletosimples/resources/installment.rb +3 -1
- data/lib/boletosimples/resources/remittance.rb +3 -1
- data/lib/boletosimples/resources/sms_delivery.rb +6 -0
- data/lib/boletosimples/resources/transaction.rb +3 -1
- data/lib/boletosimples/resources/webhook.rb +3 -1
- data/lib/boletosimples/resources/webhook_delivery.rb +3 -1
- data/lib/boletosimples/response_error.rb +8 -3
- data/lib/boletosimples/version.rb +3 -1
- data/spec/boletosimples/configuration_spec.rb +71 -99
- data/spec/boletosimples/last_request_spec.rb +16 -28
- data/spec/boletosimples/resources/bank_billet_account_spec.rb +57 -33
- data/spec/boletosimples/resources/bank_billet_discharge_spec.rb +12 -0
- data/spec/boletosimples/resources/bank_billet_payment_spec.rb +12 -0
- data/spec/boletosimples/resources/bank_billet_remittance_spec.rb +12 -0
- data/spec/boletosimples/resources/bank_billet_spec.rb +56 -42
- data/spec/boletosimples/resources/customer_import_spec.rb +41 -30
- data/spec/boletosimples/resources/customer_spec.rb +52 -39
- data/spec/boletosimples/resources/customer_subscription_import_spec.rb +41 -30
- data/spec/boletosimples/resources/customer_subscription_spec.rb +61 -33
- data/spec/boletosimples/resources/discharge_spec.rb +64 -11
- data/spec/boletosimples/resources/email_delivery_spec.rb +12 -0
- data/spec/boletosimples/resources/event_spec.rb +7 -12
- data/spec/boletosimples/resources/installment_spec.rb +61 -32
- data/spec/boletosimples/resources/remittance_spec.rb +7 -12
- data/spec/boletosimples/resources/sms_delivery_spec.rb +12 -0
- data/spec/boletosimples/resources/transactions_spec.rb +7 -12
- data/spec/boletosimples/resources/webhook_delivery_spec.rb +7 -12
- data/spec/boletosimples/resources/webhook_spec.rb +7 -12
- data/spec/fixtures/discharge.RET +3 -0
- data/spec/fixtures/vcr_cassettes/last_request/bank_billets.yml +536 -88
- data/spec/fixtures/vcr_cassettes/resources/bank_billet/all.yml +537 -91
- data/spec/fixtures/vcr_cassettes/resources/bank_billet/cancel/success.yml +34 -92
- data/spec/fixtures/vcr_cassettes/resources/bank_billet/create/invalid_params.yml +32 -20
- data/spec/fixtures/vcr_cassettes/resources/bank_billet/create/invalid_root.yml +28 -17
- data/spec/fixtures/vcr_cassettes/resources/bank_billet/create/valid.yml +43 -27
- data/spec/fixtures/vcr_cassettes/resources/bank_billet/duplicate/success.yml +83 -0
- data/spec/fixtures/vcr_cassettes/resources/bank_billet/find.yml +51 -27
- data/spec/fixtures/vcr_cassettes/resources/bank_billet_account/all.yml +131 -20
- data/spec/fixtures/vcr_cassettes/resources/bank_billet_account/create/invalid_params.yml +34 -20
- data/spec/fixtures/vcr_cassettes/resources/bank_billet_account/create/invalid_root.yml +30 -19
- data/spec/fixtures/vcr_cassettes/resources/bank_billet_account/create/valid.yml +41 -21
- data/spec/fixtures/vcr_cassettes/resources/bank_billet_account/find.yml +38 -18
- data/spec/fixtures/vcr_cassettes/resources/bank_billet_discharge/all.yml +97 -0
- data/spec/fixtures/vcr_cassettes/resources/bank_billet_payment/all.yml +75 -0
- data/spec/fixtures/vcr_cassettes/resources/bank_billet_remittance/all.yml +126 -0
- data/spec/fixtures/vcr_cassettes/resources/customer/all.yml +79 -32
- data/spec/fixtures/vcr_cassettes/resources/customer/create/invalid_params.yml +30 -17
- data/spec/fixtures/vcr_cassettes/resources/customer/create/invalid_root.yml +28 -17
- data/spec/fixtures/vcr_cassettes/resources/customer/create/valid.yml +33 -21
- data/spec/fixtures/vcr_cassettes/resources/customer/find.yml +33 -19
- data/spec/fixtures/vcr_cassettes/resources/customer_import/all.yml +43 -60
- data/spec/fixtures/vcr_cassettes/resources/customer_import/create/invalid_params.yml +37 -51
- data/spec/fixtures/vcr_cassettes/resources/customer_import/create/invalid_root.yml +38 -52
- data/spec/fixtures/vcr_cassettes/resources/customer_import/create/valid.yml +43 -68
- data/spec/fixtures/vcr_cassettes/resources/customer_import/find.yml +40 -54
- data/spec/fixtures/vcr_cassettes/resources/customer_subscription/all.yml +102 -52
- data/spec/fixtures/vcr_cassettes/resources/customer_subscription/create/invalid_params.yml +40 -50
- data/spec/fixtures/vcr_cassettes/resources/customer_subscription/create/invalid_root.yml +36 -48
- data/spec/fixtures/vcr_cassettes/resources/customer_subscription/create/valid.yml +48 -53
- data/spec/fixtures/vcr_cassettes/resources/customer_subscription/find.yml +44 -50
- data/spec/fixtures/vcr_cassettes/resources/customer_subscription/next_charge.yml +45 -133
- data/spec/fixtures/vcr_cassettes/resources/customer_subscription_import/all.yml +44 -55
- data/spec/fixtures/vcr_cassettes/resources/customer_subscription_import/create/invalid_params.yml +37 -52
- data/spec/fixtures/vcr_cassettes/resources/customer_subscription_import/create/invalid_root.yml +39 -53
- data/spec/fixtures/vcr_cassettes/resources/customer_subscription_import/create/valid.yml +43 -70
- data/spec/fixtures/vcr_cassettes/resources/customer_subscription_import/find.yml +40 -55
- data/spec/fixtures/vcr_cassettes/resources/discharge/all.yml +64 -33
- data/spec/fixtures/vcr_cassettes/resources/discharge/create/invalid_params.yml +70 -0
- data/spec/fixtures/vcr_cassettes/resources/discharge/create/invalid_root.yml +69 -0
- data/spec/fixtures/vcr_cassettes/resources/discharge/create/valid.yml +84 -0
- data/spec/fixtures/vcr_cassettes/resources/discharge/find.yml +71 -0
- data/spec/fixtures/vcr_cassettes/resources/email_delivery/all.yml +73 -0
- data/spec/fixtures/vcr_cassettes/resources/event/all.yml +490 -166
- data/spec/fixtures/vcr_cassettes/resources/installment/all.yml +418 -52
- data/spec/fixtures/vcr_cassettes/resources/installment/create/invalid_params.yml +41 -52
- data/spec/fixtures/vcr_cassettes/resources/installment/create/invalid_root.yml +36 -48
- data/spec/fixtures/vcr_cassettes/resources/installment/create/valid.yml +48 -53
- data/spec/fixtures/vcr_cassettes/resources/installment/find.yml +44 -52
- data/spec/fixtures/vcr_cassettes/resources/remittance/all.yml +53 -33
- data/spec/fixtures/vcr_cassettes/resources/sns_delivery/all.yml +73 -0
- data/spec/fixtures/vcr_cassettes/resources/transaction/all.yml +34 -22
- data/spec/fixtures/vcr_cassettes/resources/web_hook/all.yml +42 -33
- data/spec/fixtures/vcr_cassettes/resources/webhook_delivery/all.yml +1232 -54
- data/spec/spec_helper.rb +3 -20
- data/spec/support/stub_env.rb +7 -0
- data/spec/support/vcr.rb +3 -4
- metadata +120 -55
- data/.coveralls.yml +0 -2
- data/.gitignore +0 -19
- data/.ruby-gemset +0 -1
- data/.ruby-version +0 -1
- data/.travis.yml +0 -12
- data/Gemfile +0 -4
- data/Rakefile +0 -6
- data/boletosimples.gemspec +0 -38
- data/lib/boletosimples/extra.rb +0 -8
- data/lib/boletosimples/resources/partner/user.rb +0 -7
- data/lib/oauth2_patch.rb +0 -24
- data/spec/boletosimples/extra_spec.rb +0 -31
- data/spec/boletosimples/resources/partner/user_spec.rb +0 -19
- data/spec/fixtures/vcr_cassettes/configuration/client_credentials/invalid.yml +0 -64
- data/spec/fixtures/vcr_cassettes/configuration/client_credentials/valid.yml +0 -56
- data/spec/fixtures/vcr_cassettes/extra/userinfo/authenticated.yml +0 -58
- data/spec/fixtures/vcr_cassettes/extra/userinfo/not_authenticated.yml +0 -54
- data/spec/fixtures/vcr_cassettes/last_request/userinfo.yml +0 -58
- data/spec/fixtures/vcr_cassettes/resources/bank_billet/cancel/failure.yml +0 -118
- data/spec/fixtures/vcr_cassettes/resources/partner/user/create.yml +0 -60
- data/spec/spec.opts +0 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 5047dd925f3b36110d38363bd557b1e2e0485c11106663e515aaa995c9678bf8
|
4
|
+
data.tar.gz: 0612c53269469fc2288bed2a0ae12b54fa31cccecfe2e0adbb5fb6dcb7dc6ea4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c091d334a34ea8c8fd8bd2e3fe7af7d3143fe53c1357f7453d27e0aaa9a416858bd7f3eef37fc346596169226d5b0ac1badfe3ab08d7024c3f5b3b4977f74f47
|
7
|
+
data.tar.gz: f170c056e9d3cd25363e2321ac3e277bd4c06aa65f03ed88406d874bf16d6723cfb1d57b444aa28d7b24e9d0664ad26c29ba18bc03abf254db2ece277224740e
|
data/CHANGELOG.md
CHANGED
@@ -1,24 +1,16 @@
|
|
1
1
|
# Changelog
|
2
2
|
|
3
|
-
## 0.
|
3
|
+
## 1.0.3
|
4
4
|
|
5
|
-
###
|
5
|
+
### Changes
|
6
6
|
|
7
|
-
-
|
7
|
+
- Add debug option
|
8
8
|
|
9
|
-
## 0.
|
9
|
+
## 1.0.0
|
10
10
|
|
11
|
-
###
|
11
|
+
### Breaking changes
|
12
12
|
|
13
|
-
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
### features
|
18
|
-
|
19
|
-
- Created `client.cancel_bank_billet(id, options)` to cancel bank billets
|
20
|
-
|
21
|
-
### improvements
|
22
|
-
|
23
|
-
- Changed `client.create_customer({ "customer" => params })` to `client.create_customer(params)`
|
24
|
-
- Changed `client.create_bank_billet({ "bank_billet" => params })` to `client.create_bank_billet(params)`
|
13
|
+
- Remove Oauth code
|
14
|
+
- Require user_agent configuration
|
15
|
+
- Require api_token configuration
|
16
|
+
- All Errors now return with same root `errors`
|
data/README.md
CHANGED
@@ -1,16 +1,9 @@
|
|
1
1
|
# Boleto Simples Ruby
|
2
2
|
|
3
3
|
[][gem]
|
4
|
-
[][gemnasium]
|
6
|
-
[][codeclimate]
|
7
|
-
[][coveralls]
|
4
|
+
[](https://github.com/BoletoSimples/boletosimples-ruby/actions/workflows/ruby.yml)
|
8
5
|
|
9
6
|
[gem]: https://rubygems.org/gems/boletosimples
|
10
|
-
[travis]: http://travis-ci.org/BoletoSimples/boletosimples-ruby
|
11
|
-
[gemnasium]: https://gemnasium.com/BoletoSimples/boletosimples-ruby
|
12
|
-
[codeclimate]: https://codeclimate.com/github/BoletoSimples/boletosimples-ruby
|
13
|
-
[coveralls]: https://coveralls.io/r/BoletoSimples/boletosimples-ruby
|
14
7
|
|
15
8
|
Biblioteca Ruby para acessar informações do [Boleto Simples](http://boletosimples.com.br) através da [API](http://api.boletosimples.com.br).
|
16
9
|
|
@@ -30,12 +23,18 @@ Ou instale você mesmo:
|
|
30
23
|
|
31
24
|
## Configuração
|
32
25
|
|
26
|
+
Saiba mais sobre o [Token de API ](https://api.boletosimples.com.br/authentication/token/)
|
27
|
+
|
33
28
|
```ruby
|
34
29
|
require 'boletosimples'
|
35
30
|
|
36
31
|
BoletoSimples.configure do |c|
|
37
32
|
c.environment = :production # defaut :sandbox
|
38
|
-
|
33
|
+
# production - https://boletosimples.com.br/conta/api/tokens
|
34
|
+
# sandbox - https://sandbox.boletosimples.com.br/conta/api/tokens
|
35
|
+
c.api_token = 'api-token'
|
36
|
+
c.user_agent = 'email@minhaempresa.com.br' #Colocar um e-mail válido para contatos técnicos relacionado ao uso da API.
|
37
|
+
# c.debug = true
|
39
38
|
end
|
40
39
|
```
|
41
40
|
|
@@ -45,9 +44,8 @@ Você também pode configurar as variáveis de ambiente a seguir e não será ne
|
|
45
44
|
|
46
45
|
```bash
|
47
46
|
ENV['BOLETOSIMPLES_ENV']
|
48
|
-
ENV['
|
49
|
-
ENV['
|
50
|
-
ENV['BOLETOSIMPLES_ACCESS_TOKEN']
|
47
|
+
ENV['BOLETOSIMPLES_API_TOKEN']
|
48
|
+
ENV['BOLETOSIMPLES_DEBUG']
|
51
49
|
```
|
52
50
|
|
53
51
|
### Configurando cache
|
@@ -72,7 +70,7 @@ Exemplo:
|
|
72
70
|
|
73
71
|
```ruby
|
74
72
|
# Criar um boleto
|
75
|
-
@bank_billet = BoletoSimples::BankBillet.create(
|
73
|
+
@bank_billet = BoletoSimples::BankBillet.create(
|
76
74
|
amount: 9.01,
|
77
75
|
description: 'Despesas do contrato 0012',
|
78
76
|
expire_at: '2014-01-01',
|
@@ -88,7 +86,7 @@ Exemplo:
|
|
88
86
|
customer_phone_number: '2112123434',
|
89
87
|
customer_state: 'RJ',
|
90
88
|
customer_zipcode: '12312-123'
|
91
|
-
|
89
|
+
)
|
92
90
|
|
93
91
|
# Criar um novo boleto instanciando o objeto
|
94
92
|
@bank_billet = BoletoSimples::BankBillet.new(amount: 199.99, expire_at: '2020-01-01')
|
@@ -112,14 +110,6 @@ Exemplo:
|
|
112
110
|
puts bank_billet.attributes
|
113
111
|
end
|
114
112
|
|
115
|
-
# Após realizar a chamada na listagem, você terá acesso aos seguintes dados:
|
116
|
-
|
117
|
-
BoletoSimples.last_request.total # número total de boletos
|
118
|
-
BoletoSimples.last_request.links[:first] # url da primeira página
|
119
|
-
BoletoSimples.last_request.links[:prev] # url da página anterior
|
120
|
-
BoletoSimples.last_request.links[:next] # url da próxima página
|
121
|
-
BoletoSimples.last_request.links[:last] # url da última página
|
122
|
-
|
123
113
|
# Cancelar um boleto
|
124
114
|
@bank_billet = BoletoSimples::BankBillet.find(1)
|
125
115
|
@bank_billet.cancel
|
@@ -130,7 +120,7 @@ BoletoSimples.last_request.links[:last] # url da última página
|
|
130
120
|
|
131
121
|
```ruby
|
132
122
|
# Criar um cliente
|
133
|
-
@customer = BoletoSimples::Customer.create(
|
123
|
+
@customer = BoletoSimples::Customer.create(
|
134
124
|
person_name: "Joao da Silva",
|
135
125
|
cnpj_cpf: "012.345.678-90",
|
136
126
|
email: "cliente@example.com",
|
@@ -142,7 +132,7 @@ BoletoSimples.last_request.links[:last] # url da última página
|
|
142
132
|
address_number: "111",
|
143
133
|
address_complement: "Sala 4",
|
144
134
|
phone_number: "2112123434"
|
145
|
-
|
135
|
+
)
|
146
136
|
|
147
137
|
# Mensagens de erro na criação do cliente
|
148
138
|
@customer = BoletoSimples::Customer.new(person_name: '')
|
@@ -155,38 +145,8 @@ BoletoSimples.last_request.links[:last] # url da última página
|
|
155
145
|
puts customer.attributes
|
156
146
|
end
|
157
147
|
|
158
|
-
# Após realizar a chamada na listagem, você terá acesso aos seguintes dados:
|
159
|
-
|
160
|
-
BoletoSimples.last_request.total # número total de clientes
|
161
|
-
BoletoSimples.last_request.links[:first] # url da primeira página
|
162
|
-
BoletoSimples.last_request.links[:prev] # url da página anterior
|
163
|
-
BoletoSimples.last_request.links[:next] # url da próxima página
|
164
|
-
BoletoSimples.last_request.links[:last] # url da última página
|
165
|
-
|
166
148
|
# Atualizar um cliente
|
167
149
|
@customer = BoletoSimples::Customer.find(1)
|
168
150
|
@customer.person_name = 'Novo nome'
|
169
151
|
@customer.save
|
170
152
|
```
|
171
|
-
|
172
|
-
### Extrato
|
173
|
-
|
174
|
-
```ruby
|
175
|
-
# Listar todas as transações
|
176
|
-
@transactions = BoletoSimples::Transaction.all
|
177
|
-
@transactions.each do |transaction|
|
178
|
-
puts transaction.attributes
|
179
|
-
end
|
180
|
-
```
|
181
|
-
|
182
|
-
### Extras
|
183
|
-
|
184
|
-
```ruby
|
185
|
-
# Dados do usuário logado
|
186
|
-
@userinfo = BoletoSimples::Extra.userinfo
|
187
|
-
```
|
188
|
-
|
189
|
-
## OAuth 2.0 Authentication (para acessar as contas dos usuários)
|
190
|
-
|
191
|
-
Comece [solicitando um cadastro de OAuth 2.0 application](http://suporte.boletosimples.com.br)
|
192
|
-
|
data/lib/boletosimples.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'her'
|
2
4
|
require 'faraday_middleware'
|
3
5
|
require 'faraday-http-cache'
|
@@ -5,13 +7,17 @@ require 'boletosimples/version'
|
|
5
7
|
|
6
8
|
module BoletoSimples
|
7
9
|
autoload :Configuration, 'boletosimples/configuration'
|
8
|
-
autoload :Extra, 'boletosimples/extra'
|
9
10
|
autoload :ResponseError, 'boletosimples/response_error'
|
10
11
|
autoload :LastRequest, 'boletosimples/last_request'
|
11
12
|
|
12
13
|
autoload :BankBillet, 'boletosimples/resources/bank_billet'
|
13
14
|
autoload :BankBilletAccount, 'boletosimples/resources/bank_billet_account'
|
15
|
+
autoload :BankBilletDischarge, 'boletosimples/resources/bank_billet_discharge'
|
16
|
+
autoload :BankBilletPayment, 'boletosimples/resources/bank_billet_payment'
|
17
|
+
autoload :BankBilletRemittance, 'boletosimples/resources/bank_billet_remittance'
|
14
18
|
autoload :Event, 'boletosimples/resources/event'
|
19
|
+
autoload :EmailDelivery, 'boletosimples/resources/email_delivery'
|
20
|
+
autoload :SmsDelivery, 'boletosimples/resources/sms_delivery'
|
15
21
|
autoload :Customer, 'boletosimples/resources/customer'
|
16
22
|
autoload :CustomerImport, 'boletosimples/resources/customer_import'
|
17
23
|
autoload :CustomerSubscription, 'boletosimples/resources/customer_subscription'
|
@@ -24,14 +30,12 @@ module BoletoSimples
|
|
24
30
|
autoload :Webhook, 'boletosimples/resources/webhook'
|
25
31
|
autoload :WebhookDelivery, 'boletosimples/resources/webhook_delivery'
|
26
32
|
|
27
|
-
module Partner
|
28
|
-
autoload :User, 'boletosimples/resources/partner/user'
|
29
|
-
end
|
30
|
-
|
31
33
|
module Middleware
|
32
34
|
autoload :UserAgent, 'boletosimples/middlewares/user_agent'
|
33
35
|
autoload :RaiseError, 'boletosimples/middlewares/raise_error'
|
34
36
|
autoload :LastRequest, 'boletosimples/middlewares/last_request'
|
37
|
+
autoload :Debug, 'boletosimples/middlewares/debug'
|
38
|
+
autoload :Bearer, 'boletosimples/middlewares/bearer'
|
35
39
|
end
|
36
40
|
|
37
41
|
class << self
|
@@ -43,7 +47,6 @@ module BoletoSimples
|
|
43
47
|
configuration.setup_her # after changing configuration gem her should be configured
|
44
48
|
end
|
45
49
|
end
|
46
|
-
|
47
50
|
end
|
48
51
|
|
49
52
|
BoletoSimples.configure
|
@@ -1,71 +1,62 @@
|
|
1
|
-
#
|
2
|
-
module BoletoSimples
|
1
|
+
# frozen_string_literal: true
|
3
2
|
|
3
|
+
module BoletoSimples
|
4
4
|
class Configuration
|
5
|
-
attr_accessor :environment, :
|
6
|
-
|
5
|
+
attr_accessor :environment, :cache, :user_agent, :api_token, :debug
|
7
6
|
|
8
7
|
BASE_URI = {
|
9
8
|
sandbox: 'https://sandbox.boletosimples.com.br/api/v1',
|
10
9
|
production: 'https://boletosimples.com.br/api/v1',
|
11
10
|
development: 'http://localhost:5000/api/v1'
|
12
|
-
}
|
11
|
+
}.freeze
|
13
12
|
|
14
13
|
def initialize
|
15
14
|
@environment = (ENV['BOLETOSIMPLES_ENV'] || :sandbox).to_sym
|
16
|
-
@
|
17
|
-
@
|
18
|
-
@access_token = ENV['BOLETOSIMPLES_ACCESS_TOKEN']
|
15
|
+
@api_token = ENV['BOLETOSIMPLES_API_TOKEN']
|
16
|
+
@user_agent = ENV['BOLETOSIMPLES_USER_AGENT']
|
19
17
|
@cache = nil
|
18
|
+
@debug = ENV['BOLETOSIMPLES_DEBUG']
|
20
19
|
end
|
21
20
|
|
22
21
|
def base_uri
|
23
22
|
BASE_URI[@environment]
|
24
23
|
end
|
25
24
|
|
26
|
-
def
|
27
|
-
|
25
|
+
def api_token?
|
26
|
+
!@api_token.nil?
|
28
27
|
end
|
29
28
|
|
30
|
-
def
|
31
|
-
!@
|
32
|
-
end
|
33
|
-
|
34
|
-
def client_credentials
|
35
|
-
response = Her::API.default_api.connection.post 'oauth2/token', {
|
36
|
-
grant_type: 'client_credentials',
|
37
|
-
client_id: application_id,
|
38
|
-
client_secret: application_secret
|
39
|
-
}
|
40
|
-
response.body[:data]
|
29
|
+
def debug?
|
30
|
+
!@debug.nil?
|
41
31
|
end
|
42
32
|
|
43
33
|
def setup_her
|
44
34
|
Her::API.setup url: base_uri do |c|
|
45
35
|
# Request
|
46
36
|
c.use BoletoSimples::Middleware::UserAgent
|
47
|
-
c.use
|
37
|
+
c.use BoletoSimples::Middleware::Bearer if api_token?
|
48
38
|
c.use Faraday::Request::Multipart
|
49
|
-
c.use Faraday::Request::UrlEncoded
|
50
39
|
c.use FaradayMiddleware::EncodeJson
|
40
|
+
c.use Her::Middleware::AcceptJSON
|
51
41
|
c.use Faraday::HttpCache, store: cache unless cache.nil?
|
52
42
|
|
53
43
|
# Response
|
44
|
+
c.use BoletoSimples::Middleware::Debug if debug?
|
54
45
|
c.use BoletoSimples::Middleware::LastRequest
|
55
46
|
c.use BoletoSimples::Middleware::RaiseError
|
56
47
|
c.use Her::Middleware::DefaultParseJSON
|
57
48
|
|
58
49
|
# Adapter
|
59
|
-
c.
|
50
|
+
c.adapter Faraday::Adapter::NetHttp
|
60
51
|
end
|
61
52
|
|
62
53
|
# Because Her set the api on the moment module is included we need to call use_api again, after changing the configuration.
|
63
|
-
[BankBillet, BankBilletAccount, Customer, CustomerImport, CustomerSubscription,
|
64
|
-
|
54
|
+
[BankBillet, BankBilletAccount, Customer, CustomerImport, CustomerSubscription,
|
55
|
+
CustomerSubscriptionImport, Installment, Transaction, Webhook, Discharge,
|
56
|
+
Remittance, WebhookDelivery, Event, EmailDelivery, BankBilletDischarge,
|
57
|
+
BankBilletPayment, BankBilletRemittance, SmsDelivery].each do |klass|
|
65
58
|
klass.send(:use_api, Her::API.default_api)
|
66
59
|
end
|
67
60
|
end
|
68
|
-
|
69
61
|
end
|
70
|
-
|
71
62
|
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module BoletoSimples
|
2
4
|
class LastRequest
|
3
5
|
attr_reader :body, :response_headers, :total, :ratelimit_limit, :ratelimit_remaining, :links
|
@@ -15,29 +17,30 @@ module BoletoSimples
|
|
15
17
|
end
|
16
18
|
|
17
19
|
def total
|
18
|
-
@total ||= @env[:response_headers][
|
20
|
+
@total ||= @env[:response_headers]['total'].to_i
|
19
21
|
end
|
20
22
|
|
21
23
|
def ratelimit_limit
|
22
|
-
ratelimit_limit ||= @env[:response_headers][
|
24
|
+
ratelimit_limit ||= @env[:response_headers]['x-ratelimit-limit'].to_i
|
23
25
|
end
|
24
26
|
|
25
27
|
def ratelimit_remaining
|
26
|
-
@ratelimit_remaining ||= @env[:response_headers][
|
28
|
+
@ratelimit_remaining ||= @env[:response_headers]['x-ratelimit-remaining'].to_i
|
27
29
|
end
|
28
30
|
|
29
31
|
def links
|
30
32
|
return @links unless @links.nil?
|
31
|
-
|
33
|
+
|
34
|
+
link_header = @env[:response_headers]['link']
|
32
35
|
return {} if link_header.nil?
|
36
|
+
|
33
37
|
@links = {}
|
34
|
-
link_header.split(
|
35
|
-
key = /rel
|
36
|
-
value =
|
38
|
+
link_header.split(', ').each do |link|
|
39
|
+
key = /rel="(.*)"/.match(link)[1]
|
40
|
+
value = /<(.*)>/.match(link)[1]
|
37
41
|
@links[key] = value
|
38
42
|
end
|
39
43
|
@links.symbolize_keys!
|
40
44
|
end
|
41
|
-
|
42
45
|
end
|
43
|
-
end
|
46
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module BoletoSimples
|
4
|
+
module Middleware
|
5
|
+
class Bearer < Faraday::Middleware
|
6
|
+
def call(env)
|
7
|
+
env[:request_headers]['Authorization'] = "Bearer #{BoletoSimples.configuration.api_token}"
|
8
|
+
@app.call(env)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module BoletoSimples
|
4
|
+
module Middleware
|
5
|
+
class Debug < Faraday::Response::Middleware
|
6
|
+
def initialize(app, logger = nil)
|
7
|
+
super(app)
|
8
|
+
@logger = logger || begin
|
9
|
+
require 'logger'
|
10
|
+
::Logger.new($stdout)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def on_complete(env)
|
15
|
+
@logger.info "\n::#{env[:method].upcase} #{env[:url]}"
|
16
|
+
@logger.info ' Request'
|
17
|
+
env[:request_headers].each do |key, value|
|
18
|
+
@logger.info " -- #{key}: #{value}"
|
19
|
+
end
|
20
|
+
@logger.info ' Response'
|
21
|
+
@logger.info " -- Status: #{env[:status]}"
|
22
|
+
env[:response_headers].each do |key, value|
|
23
|
+
@logger.info " -- #{key}: #{value}"
|
24
|
+
end
|
25
|
+
@logger.info ' Response body'
|
26
|
+
@logger.info " -- #{env[:body]} \n"
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -1,11 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module BoletoSimples
|
2
4
|
module Middleware
|
3
5
|
class RaiseError < Faraday::Response::Middleware
|
4
6
|
def on_complete(env)
|
5
7
|
status = env[:status].to_i
|
8
|
+
return if status == 422
|
9
|
+
|
6
10
|
klass = BoletoSimples::ResponseError
|
7
|
-
|
11
|
+
if (400..599).cover?(status) && env[:body][:errors]
|
12
|
+
puts env.inspect
|
13
|
+
raise klass, env
|
14
|
+
end
|
8
15
|
end
|
9
16
|
end
|
10
17
|
end
|
11
|
-
end
|
18
|
+
end
|