boletosimples 0.6.0 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (138) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +16 -12
  3. data/README.md +21 -59
  4. data/lib/boletosimples/configuration.rb +25 -28
  5. data/lib/boletosimples/last_request.rb +17 -10
  6. data/lib/boletosimples/middlewares/bearer.rb +12 -0
  7. data/lib/boletosimples/middlewares/custom_headers.rb +12 -0
  8. data/lib/boletosimples/middlewares/debug.rb +30 -0
  9. data/lib/boletosimples/middlewares/last_request.rb +3 -1
  10. data/lib/boletosimples/middlewares/raise_error.rb +9 -2
  11. data/lib/boletosimples/middlewares/user_agent.rb +3 -1
  12. data/lib/boletosimples/resources/bank_billet.rb +5 -11
  13. data/lib/boletosimples/resources/bank_billet_account.rb +3 -1
  14. data/lib/boletosimples/resources/bank_billet_discharge.rb +3 -1
  15. data/lib/boletosimples/resources/bank_billet_payment.rb +3 -1
  16. data/lib/boletosimples/resources/bank_billet_remittance.rb +3 -1
  17. data/lib/boletosimples/resources/base_model.rb +3 -1
  18. data/lib/boletosimples/resources/customer.rb +3 -1
  19. data/lib/boletosimples/resources/customer_import.rb +5 -3
  20. data/lib/boletosimples/resources/customer_subscription.rb +3 -10
  21. data/lib/boletosimples/resources/customer_subscription_import.rb +5 -3
  22. data/lib/boletosimples/resources/discharge.rb +4 -11
  23. data/lib/boletosimples/resources/email_delivery.rb +3 -1
  24. data/lib/boletosimples/resources/event.rb +3 -1
  25. data/lib/boletosimples/resources/installment.rb +3 -1
  26. data/lib/boletosimples/resources/remittance.rb +3 -1
  27. data/lib/boletosimples/resources/sms_delivery.rb +6 -0
  28. data/lib/boletosimples/resources/transaction.rb +3 -1
  29. data/lib/boletosimples/resources/webhook.rb +3 -1
  30. data/lib/boletosimples/resources/webhook_delivery.rb +3 -1
  31. data/lib/boletosimples/response_error.rb +10 -5
  32. data/lib/boletosimples/version.rb +3 -1
  33. data/lib/boletosimples.rb +6 -6
  34. data/spec/boletosimples/configuration_spec.rb +67 -80
  35. data/spec/boletosimples/last_request_spec.rb +17 -28
  36. data/spec/boletosimples/middlewares/custom_headers_spec.rb +26 -0
  37. data/spec/boletosimples/resources/bank_billet_account_spec.rb +57 -33
  38. data/spec/boletosimples/resources/bank_billet_discharge_spec.rb +7 -12
  39. data/spec/boletosimples/resources/bank_billet_payment_spec.rb +7 -12
  40. data/spec/boletosimples/resources/bank_billet_remittance_spec.rb +7 -12
  41. data/spec/boletosimples/resources/bank_billet_spec.rb +56 -42
  42. data/spec/boletosimples/resources/customer_import_spec.rb +41 -30
  43. data/spec/boletosimples/resources/customer_spec.rb +57 -39
  44. data/spec/boletosimples/resources/customer_subscription_import_spec.rb +41 -30
  45. data/spec/boletosimples/resources/customer_subscription_spec.rb +61 -33
  46. data/spec/boletosimples/resources/discharge_spec.rb +64 -11
  47. data/spec/boletosimples/resources/email_delivery_spec.rb +6 -11
  48. data/spec/boletosimples/resources/event_spec.rb +7 -12
  49. data/spec/boletosimples/resources/installment_spec.rb +61 -32
  50. data/spec/boletosimples/resources/remittance_spec.rb +7 -12
  51. data/spec/boletosimples/resources/sms_delivery_spec.rb +12 -0
  52. data/spec/boletosimples/resources/transactions_spec.rb +7 -12
  53. data/spec/boletosimples/resources/webhook_delivery_spec.rb +7 -12
  54. data/spec/boletosimples/resources/webhook_spec.rb +7 -12
  55. data/spec/fixtures/discharge.RET +3 -0
  56. data/spec/fixtures/vcr_cassettes/custom_headers.yml +113 -0
  57. data/spec/fixtures/vcr_cassettes/last_request/bank_billets.yml +409 -95
  58. data/spec/fixtures/vcr_cassettes/resources/bank_billet/all.yml +101 -99
  59. data/spec/fixtures/vcr_cassettes/resources/bank_billet/cancel/success.yml +55 -95
  60. data/spec/fixtures/vcr_cassettes/resources/bank_billet/create/invalid_params.yml +55 -27
  61. data/spec/fixtures/vcr_cassettes/resources/bank_billet/create/invalid_root.yml +51 -24
  62. data/spec/fixtures/vcr_cassettes/resources/bank_billet/create/valid.yml +64 -34
  63. data/spec/fixtures/vcr_cassettes/resources/bank_billet/duplicate/success.yml +97 -0
  64. data/spec/fixtures/vcr_cassettes/resources/bank_billet/find.yml +73 -34
  65. data/spec/fixtures/vcr_cassettes/resources/bank_billet_account/all.yml +82 -27
  66. data/spec/fixtures/vcr_cassettes/resources/bank_billet_account/create/invalid_params.yml +57 -27
  67. data/spec/fixtures/vcr_cassettes/resources/bank_billet_account/create/invalid_root.yml +53 -26
  68. data/spec/fixtures/vcr_cassettes/resources/bank_billet_account/create/valid.yml +64 -28
  69. data/spec/fixtures/vcr_cassettes/resources/bank_billet_account/find.yml +61 -25
  70. data/spec/fixtures/vcr_cassettes/resources/bank_billet_discharge/all.yml +59 -46
  71. data/spec/fixtures/vcr_cassettes/resources/bank_billet_payment/all.yml +74 -71
  72. data/spec/fixtures/vcr_cassettes/resources/bank_billet_remittance/all.yml +63 -47
  73. data/spec/fixtures/vcr_cassettes/resources/customer/all.yml +141 -39
  74. data/spec/fixtures/vcr_cassettes/resources/customer/create/invalid_params.yml +54 -25
  75. data/spec/fixtures/vcr_cassettes/resources/customer/create/invalid_root.yml +54 -24
  76. data/spec/fixtures/vcr_cassettes/resources/customer/create/valid.yml +56 -28
  77. data/spec/fixtures/vcr_cassettes/resources/customer/find.yml +56 -26
  78. data/spec/fixtures/vcr_cassettes/resources/customer_import/all.yml +58 -59
  79. data/spec/fixtures/vcr_cassettes/resources/customer_import/create/invalid_params.yml +53 -51
  80. data/spec/fixtures/vcr_cassettes/resources/customer_import/create/invalid_root.yml +54 -52
  81. data/spec/fixtures/vcr_cassettes/resources/customer_import/create/valid.yml +58 -67
  82. data/spec/fixtures/vcr_cassettes/resources/customer_import/find.yml +55 -53
  83. data/spec/fixtures/vcr_cassettes/resources/customer_subscription/all.yml +65 -51
  84. data/spec/fixtures/vcr_cassettes/resources/customer_subscription/create/invalid_params.yml +56 -50
  85. data/spec/fixtures/vcr_cassettes/resources/customer_subscription/create/invalid_root.yml +52 -48
  86. data/spec/fixtures/vcr_cassettes/resources/customer_subscription/create/valid.yml +63 -52
  87. data/spec/fixtures/vcr_cassettes/resources/customer_subscription/find.yml +61 -51
  88. data/spec/fixtures/vcr_cassettes/resources/customer_subscription/next_charge.yml +60 -132
  89. data/spec/fixtures/vcr_cassettes/resources/customer_subscription_import/all.yml +58 -54
  90. data/spec/fixtures/vcr_cassettes/resources/customer_subscription_import/create/invalid_params.yml +53 -52
  91. data/spec/fixtures/vcr_cassettes/resources/customer_subscription_import/create/invalid_root.yml +55 -53
  92. data/spec/fixtures/vcr_cassettes/resources/customer_subscription_import/create/valid.yml +58 -69
  93. data/spec/fixtures/vcr_cassettes/resources/customer_subscription_import/find.yml +55 -54
  94. data/spec/fixtures/vcr_cassettes/resources/discharge/all.yml +63 -34
  95. data/spec/fixtures/vcr_cassettes/resources/discharge/create/invalid_params.yml +86 -0
  96. data/spec/fixtures/vcr_cassettes/resources/discharge/create/invalid_root.yml +85 -0
  97. data/spec/fixtures/vcr_cassettes/resources/discharge/create/valid.yml +100 -0
  98. data/spec/fixtures/vcr_cassettes/resources/discharge/find.yml +89 -0
  99. data/spec/fixtures/vcr_cassettes/resources/email_delivery/all.yml +40 -43
  100. data/spec/fixtures/vcr_cassettes/resources/event/all.yml +326 -167
  101. data/spec/fixtures/vcr_cassettes/resources/installment/all.yml +113 -51
  102. data/spec/fixtures/vcr_cassettes/resources/installment/create/invalid_params.yml +57 -52
  103. data/spec/fixtures/vcr_cassettes/resources/installment/create/invalid_root.yml +52 -48
  104. data/spec/fixtures/vcr_cassettes/resources/installment/create/valid.yml +63 -52
  105. data/spec/fixtures/vcr_cassettes/resources/installment/find.yml +83 -51
  106. data/spec/fixtures/vcr_cassettes/resources/remittance/all.yml +83 -62
  107. data/spec/fixtures/vcr_cassettes/resources/sns_delivery/all.yml +89 -0
  108. data/spec/fixtures/vcr_cassettes/resources/transaction/all.yml +73 -60
  109. data/spec/fixtures/vcr_cassettes/resources/web_hook/all.yml +59 -34
  110. data/spec/fixtures/vcr_cassettes/resources/webhook_delivery/all.yml +2684 -55
  111. data/spec/spec_helper.rb +3 -20
  112. data/spec/support/stub_env.rb +7 -0
  113. data/spec/support/vcr.rb +3 -4
  114. metadata +42 -176
  115. data/.coveralls.yml +0 -2
  116. data/.gitignore +0 -19
  117. data/.ruby-version +0 -1
  118. data/.travis.yml +0 -46
  119. data/Gemfile +0 -4
  120. data/Rakefile +0 -14
  121. data/boletosimples.gemspec +0 -38
  122. data/gemfiles/rails_42.gemfile +0 -8
  123. data/gemfiles/rails_50.gemfile +0 -8
  124. data/gemfiles/rails_51.gemfile +0 -8
  125. data/gemfiles/rails_52.gemfile +0 -9
  126. data/lib/boletosimples/extra.rb +0 -8
  127. data/lib/boletosimples/resources/partner/user.rb +0 -7
  128. data/lib/oauth2_patch.rb +0 -24
  129. data/spec/boletosimples/extra_spec.rb +0 -31
  130. data/spec/boletosimples/resources/partner/user_spec.rb +0 -19
  131. data/spec/fixtures/vcr_cassettes/configuration/client_credentials/invalid.yml +0 -64
  132. data/spec/fixtures/vcr_cassettes/configuration/client_credentials/valid.yml +0 -56
  133. data/spec/fixtures/vcr_cassettes/extra/userinfo/authenticated.yml +0 -58
  134. data/spec/fixtures/vcr_cassettes/extra/userinfo/not_authenticated.yml +0 -54
  135. data/spec/fixtures/vcr_cassettes/last_request/userinfo.yml +0 -58
  136. data/spec/fixtures/vcr_cassettes/resources/bank_billet/cancel/failure.yml +0 -118
  137. data/spec/fixtures/vcr_cassettes/resources/partner/user/create.yml +0 -60
  138. data/spec/spec.opts +0 -8
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 267fcb0a89580d774bde7f6f875cedffb19d56cf81b5fea7adc7a25d51948320
4
- data.tar.gz: 121d8f4fa802857f9b401ff37f33ac72fcecb0c43f1ceb817687d342e097b8fb
3
+ metadata.gz: 4b0b065f756a9ee10d080e10686637aee38ca6b63306878dc4879dbf56fd676c
4
+ data.tar.gz: c83c0eadff5245a19d07ea7a6cbe8c184eeed809e0895db4eb9ca78802250cf8
5
5
  SHA512:
6
- metadata.gz: a3e8c7819279bc063e647329d89bda39047cc70182df5c6832fbc9635382b92896dbbf670dceba67698f88bd6fca35defb2a4bad49911b9aaa19b0fad2a39e27
7
- data.tar.gz: 1eb282604546f93c830134f9137c3f78a73190d3461d4c1e7a6f8e065a188321f8d030a4bbcc92892903123d6978d1c755f21ff8e4a2ce999cee8e477803eaac
6
+ metadata.gz: 977fb2769a02f9fcc6da3e2a4a4fe35ba9fe9f80327584bfc3b48dbb1446ea666f1b1cb0c5b61d09422cc1f1ff702a88768e3b58c820b8c83121972dab9ae4cd
7
+ data.tar.gz: fa35fda07bc7f85c31c03f4991fed958107e4709418292798c22122da79940719688bc337c5ab686224877102c30e475421e79e9d117b4f7ba66c883fba7c333
data/CHANGELOG.md CHANGED
@@ -1,24 +1,28 @@
1
1
  # Changelog
2
2
 
3
- ## 0.4.2
3
+ ## 2.0.0
4
4
 
5
- ### improvements
5
+ ### Changes
6
6
 
7
- - Update dependencies
7
+ - Update URLs based on new API: https://developers.kobana.com.br/reference/endpoints-1
8
8
 
9
- ## 0.2.2
9
+ ## 1.0.4
10
10
 
11
- ### features
11
+ ### Changes
12
12
 
13
- - Add BankBilletAccount resource
13
+ - Fix cache
14
14
 
15
- ## 0.1.1
15
+ ## 1.0.3
16
16
 
17
- ### features
17
+ ### Changes
18
18
 
19
- - Created `client.cancel_bank_billet(id, options)` to cancel bank billets
19
+ - Add debug option
20
20
 
21
- ### improvements
21
+ ## 1.0.0
22
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)`
23
+ ### Breaking changes
24
+
25
+ - Remove Oauth code
26
+ - Require user_agent configuration
27
+ - Require api_token configuration
28
+ - All Errors now return with same root `errors`
data/README.md CHANGED
@@ -1,18 +1,11 @@
1
1
  # Boleto Simples Ruby
2
2
 
3
3
  [![Gem Version](http://img.shields.io/gem/v/boletosimples.svg)][gem]
4
- [![Build Status](http://img.shields.io/travis/BoletoSimples/boletosimples-ruby.svg)][travis]
5
- [![Dependency Status](http://img.shields.io/gemnasium/BoletoSimples/boletosimples-ruby.svg)][gemnasium]
6
- [![Code Climate](http://img.shields.io/codeclimate/github/BoletoSimples/boletosimples-ruby.svg)][codeclimate]
7
- [![Coverage Status](http://img.shields.io/coveralls/BoletoSimples/boletosimples-ruby.svg)][coveralls]
4
+ [![Ruby](https://github.com/BoletoSimples/boletosimples-ruby/actions/workflows/ruby.yml/badge.svg?branch=master)](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
- Biblioteca Ruby para acessar informações do [Boleto Simples](http://boletosimples.com.br) através da [API](http://api.boletosimples.com.br).
8
+ Biblioteca Ruby para acessar informações da [Kobana](https://www.kobana.com.br) através da [API](https://api.kobana.com.br).
16
9
 
17
10
  ## Instalação
18
11
 
@@ -30,12 +23,19 @@ Ou instale você mesmo:
30
23
 
31
24
  ## Configuração
32
25
 
26
+ Saiba mais sobre o [Token de API ](https://developers.kobana.com.br/reference/token-de-acesso)
27
+
33
28
  ```ruby
34
29
  require 'boletosimples'
35
30
 
36
31
  BoletoSimples.configure do |c|
37
32
  c.environment = :production # defaut :sandbox
38
- c.access_token = 'access-token'
33
+ # production - https://app.kobana.com.br/conta/api/tokens
34
+ # sandbox - https://app-sandbox.kobana.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
38
+ # c.custom_headers = { 'X-CUSTOM' => 'CONTENT' }
39
39
  end
40
40
  ```
41
41
 
@@ -45,14 +45,13 @@ Você também pode configurar as variáveis de ambiente a seguir e não será ne
45
45
 
46
46
  ```bash
47
47
  ENV['BOLETOSIMPLES_ENV']
48
- ENV['BOLETOSIMPLES_APP_ID']
49
- ENV['BOLETOSIMPLES_APP_SECRET']
50
- ENV['BOLETOSIMPLES_ACCESS_TOKEN']
48
+ ENV['BOLETOSIMPLES_API_TOKEN']
49
+ ENV['BOLETOSIMPLES_DEBUG']
51
50
  ```
52
51
 
53
52
  ### Configurando cache
54
53
 
55
- É altamente recomendável utilizar o cache para evitar chegar no [limite de requisições](http://api.boletosimples.com.br/#limite-de-requisicoes)
54
+ É altamente recomendável utilizar o cache para evitar chegar no [limite de requisições](https://developers.kobana.com.br/reference/limite-de-requisições)
56
55
 
57
56
  Para isso recomendamos a utilização da gem [Dalli](https://github.com/mperham/dalli)
58
57
 
@@ -62,7 +61,9 @@ Exemplo:
62
61
  require 'dalli'
63
62
 
64
63
  BoletoSimples.configure do |c|
65
- c.cache = ActiveSupport::Cache.lookup_store(:dalli_store, ['localhost:11211'], namespace: 'boletosimples_client', compress: true)
64
+ c.cache = ActiveSupport::Cache.lookup_store(:mem_cache_store, ['localhost:11211'],
65
+ namespace: 'boletosimples_client',
66
+ compress: true)
66
67
  end
67
68
  ```
68
69
 
@@ -72,7 +73,7 @@ Exemplo:
72
73
 
73
74
  ```ruby
74
75
  # Criar um boleto
75
- @bank_billet = BoletoSimples::BankBillet.create({
76
+ @bank_billet = BoletoSimples::BankBillet.create(
76
77
  amount: 9.01,
77
78
  description: 'Despesas do contrato 0012',
78
79
  expire_at: '2014-01-01',
@@ -88,7 +89,7 @@ Exemplo:
88
89
  customer_phone_number: '2112123434',
89
90
  customer_state: 'RJ',
90
91
  customer_zipcode: '12312-123'
91
- })
92
+ )
92
93
 
93
94
  # Criar um novo boleto instanciando o objeto
94
95
  @bank_billet = BoletoSimples::BankBillet.new(amount: 199.99, expire_at: '2020-01-01')
@@ -112,17 +113,8 @@ Exemplo:
112
113
  puts bank_billet.attributes
113
114
  end
114
115
 
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
116
  # Cancelar um boleto
124
- @bank_billet = BoletoSimples::BankBillet.find(1)
125
- @bank_billet.cancel
117
+ @bank_billet = BoletoSimples::BankBillet.cancel(id: 1)
126
118
 
127
119
  ```
128
120
 
@@ -130,7 +122,7 @@ BoletoSimples.last_request.links[:last] # url da última página
130
122
 
131
123
  ```ruby
132
124
  # Criar um cliente
133
- @customer = BoletoSimples::Customer.create({
125
+ @customer = BoletoSimples::Customer.create(
134
126
  person_name: "Joao da Silva",
135
127
  cnpj_cpf: "012.345.678-90",
136
128
  email: "cliente@example.com",
@@ -142,7 +134,7 @@ BoletoSimples.last_request.links[:last] # url da última página
142
134
  address_number: "111",
143
135
  address_complement: "Sala 4",
144
136
  phone_number: "2112123434"
145
- })
137
+ )
146
138
 
147
139
  # Mensagens de erro na criação do cliente
148
140
  @customer = BoletoSimples::Customer.new(person_name: '')
@@ -155,38 +147,8 @@ BoletoSimples.last_request.links[:last] # url da última página
155
147
  puts customer.attributes
156
148
  end
157
149
 
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
150
  # Atualizar um cliente
167
151
  @customer = BoletoSimples::Customer.find(1)
168
152
  @customer.person_name = 'Novo nome'
169
153
  @customer.save
170
154
  ```
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
-
@@ -1,67 +1,64 @@
1
- # encoding: utf-8
2
- module BoletoSimples
1
+ # frozen_string_literal: true
3
2
 
3
+ module BoletoSimples
4
4
  class Configuration
5
- attr_accessor :environment, :application_id, :application_secret, :access_token, :cache, :user_agent
5
+ attr_accessor :environment, :cache, :user_agent, :custom_headers, :api_token, :debug
6
6
 
7
7
  BASE_URI = {
8
- sandbox: 'https://sandbox.boletosimples.com.br/api/v1',
9
- production: 'https://boletosimples.com.br/api/v1',
8
+ sandbox: 'https://api-sandbox.kobana.com.br/v1',
9
+ production: 'https://api.kobana.com.br/v1',
10
10
  development: 'http://localhost:5000/api/v1'
11
- }
11
+ }.freeze
12
12
 
13
13
  def initialize
14
- @environment = (ENV['BOLETOSIMPLES_ENV'] || :sandbox).to_sym
15
- @application_id = ENV['BOLETOSIMPLES_APP_ID']
16
- @application_secret = ENV['BOLETOSIMPLES_APP_SECRET']
17
- @access_token = ENV['BOLETOSIMPLES_ACCESS_TOKEN']
14
+ @environment = ENV.fetch('BOLETOSIMPLES_ENV', :sandbox).to_sym
15
+ @api_token = ENV.fetch('BOLETOSIMPLES_API_TOKEN', nil)
16
+ @user_agent = ENV.fetch('BOLETOSIMPLES_USER_AGENT', nil)
17
+ @custom_headers = {}
18
18
  @cache = nil
19
- @user_agent = ENV['BOLETOSIMPLES_USER_AGENT'] || "BoletoSimples Ruby Client v#{BoletoSimples::VERSION} (contato@boletosimples.com.br)"
19
+ @debug = ENV.fetch('BOLETOSIMPLES_DEBUG', nil)
20
20
  end
21
21
 
22
22
  def base_uri
23
23
  BASE_URI[@environment]
24
24
  end
25
25
 
26
- def access_token?
27
- !@access_token.nil?
26
+ def api_token?
27
+ !@api_token.nil?
28
28
  end
29
29
 
30
- def client_credentials
31
- response = Her::API.default_api.connection.post 'oauth2/token', {
32
- grant_type: 'client_credentials',
33
- client_id: application_id,
34
- client_secret: application_secret
35
- }
36
- response.body[:data]
30
+ def debug?
31
+ !@debug.nil?
37
32
  end
38
33
 
39
34
  def setup_her
40
35
  Her::API.setup url: base_uri do |c|
41
36
  # Request
42
37
  c.use BoletoSimples::Middleware::UserAgent
43
- c.use FaradayMiddleware::OAuth2, access_token, token_type: 'param' if access_token?
38
+ c.use BoletoSimples::Middleware::Bearer if api_token?
39
+ c.use BoletoSimples::Middleware::CustomHeaders
44
40
  c.use Faraday::Request::Multipart
45
- c.use Faraday::Request::UrlEncoded
46
41
  c.use FaradayMiddleware::EncodeJson
47
- c.use Faraday::HttpCache, store: cache unless cache.nil?
42
+ c.use Her::Middleware::AcceptJSON
43
+ c.use Faraday::HttpCache, store: cache, serializer: Marshal unless cache.nil?
48
44
 
49
45
  # Response
46
+ c.use BoletoSimples::Middleware::Debug if debug?
50
47
  c.use BoletoSimples::Middleware::LastRequest
51
48
  c.use BoletoSimples::Middleware::RaiseError
52
49
  c.use Her::Middleware::DefaultParseJSON
53
50
 
54
51
  # Adapter
55
- c.use Faraday::Adapter::NetHttp
52
+ c.adapter Faraday::Adapter::NetHttp
56
53
  end
57
54
 
58
55
  # Because Her set the api on the moment module is included we need to call use_api again, after changing the configuration.
59
- [BankBillet, BankBilletAccount, Customer, CustomerImport, CustomerSubscription, CustomerSubscriptionImport, Installment, Transaction, Partner::User,
60
- Webhook, Discharge, Remittance, WebhookDelivery, Event, EmailDelivery, BankBilletDischarge, BankBilletPayment, BankBilletRemittance].each do |klass|
56
+ [BankBillet, BankBilletAccount, Customer, CustomerImport, CustomerSubscription,
57
+ CustomerSubscriptionImport, Installment, Transaction, Webhook, Discharge,
58
+ Remittance, WebhookDelivery, Event, EmailDelivery, BankBilletDischarge,
59
+ BankBilletPayment, BankBilletRemittance, SmsDelivery].each do |klass|
61
60
  klass.send(:use_api, Her::API.default_api)
62
61
  end
63
62
  end
64
-
65
63
  end
66
-
67
64
  end
@@ -1,6 +1,8 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module BoletoSimples
2
4
  class LastRequest
3
- attr_reader :body, :response_headers, :total, :ratelimit_limit, :ratelimit_remaining, :links
5
+ attr_reader :body, :request_headers, :response_headers, :total, :ratelimit_limit, :ratelimit_remaining, :links
4
6
 
5
7
  def initialize(env)
6
8
  @env = env
@@ -10,34 +12,39 @@ module BoletoSimples
10
12
  @body ||= @env[:body][:data]
11
13
  end
12
14
 
15
+ def request_headers
16
+ @request_headers ||= @env.request_headers
17
+ end
18
+
13
19
  def response_headers
14
20
  @response_headers ||= @env[:response_headers]
15
21
  end
16
22
 
17
23
  def total
18
- @total ||= @env[:response_headers]["total"].to_i
24
+ @total ||= @env[:response_headers]['total'].to_i
19
25
  end
20
26
 
21
27
  def ratelimit_limit
22
- ratelimit_limit ||= @env[:response_headers]["x-ratelimit-limit"].to_i
28
+ ratelimit_limit ||= @env[:response_headers]['x-ratelimit-limit'].to_i
23
29
  end
24
30
 
25
31
  def ratelimit_remaining
26
- @ratelimit_remaining ||= @env[:response_headers]["x-ratelimit-remaining"].to_i
32
+ @ratelimit_remaining ||= @env[:response_headers]['x-ratelimit-remaining'].to_i
27
33
  end
28
34
 
29
35
  def links
30
36
  return @links unless @links.nil?
31
- link_header = @env[:response_headers]["link"]
37
+
38
+ link_header = @env[:response_headers]['link']
32
39
  return {} if link_header.nil?
40
+
33
41
  @links = {}
34
- link_header.split(", ").each do |link|
35
- key = /rel=\"(.*)\"/.match(link)[1]
36
- value = /\<(.*)\>/.match(link)[1]
42
+ link_header.split(', ').each do |link|
43
+ key = /rel="(.*)"/.match(link)[1]
44
+ value = /<(.*)>/.match(link)[1]
37
45
  @links[key] = value
38
46
  end
39
47
  @links.symbolize_keys!
40
48
  end
41
-
42
49
  end
43
- end
50
+ 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,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ module BoletoSimples
4
+ module Middleware
5
+ class CustomHeaders < Faraday::Middleware
6
+ def call(env)
7
+ env[:request_headers].merge!(BoletoSimples.configuration.custom_headers)
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,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module BoletoSimples
2
4
  module Middleware
3
5
  class LastRequest < Faraday::Response::Middleware
@@ -6,4 +8,4 @@ module BoletoSimples
6
8
  end
7
9
  end
8
10
  end
9
- end
11
+ 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
- raise klass.new(env) if (400..599).include?(status) and env[:body][:data][:error]
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
@@ -1,8 +1,10 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module BoletoSimples
2
4
  module Middleware
3
5
  class UserAgent < Faraday::Middleware
4
6
  def call(env)
5
- env[:request_headers]["User-Agent"] = BoletoSimples.configuration.user_agent
7
+ env[:request_headers]['User-Agent'] = BoletoSimples.configuration.user_agent
6
8
  @app.call(env)
7
9
  end
8
10
  end
@@ -1,14 +1,8 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module BoletoSimples
2
4
  class BankBillet < BaseModel
3
-
4
- def cancel
5
- self.class.request(:_method => :put, :_path => self.class.build_request_path('bank_billets/:id/cancel', {self.class.primary_key => id})) do |parsed_data, response|
6
- assign_attributes(self.class.parse(parsed_data[:data])) if parsed_data[:data].any?
7
- @metadata = parsed_data[:metadata]
8
- @response_errors = parsed_data[:errors]
9
- @response = response
10
- end
11
- return @response.success?
12
- end
5
+ custom_put :pay, :cancel
6
+ custom_post :duplicate
13
7
  end
14
- end
8
+ end
@@ -1,4 +1,6 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module BoletoSimples
2
4
  class BankBilletAccount < BaseModel
3
5
  end
4
- end
6
+ end
@@ -1,4 +1,6 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module BoletoSimples
2
4
  class BankBilletDischarge < BaseModel
3
5
  end
4
- end
6
+ end
@@ -1,4 +1,6 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module BoletoSimples
2
4
  class BankBilletPayment < BaseModel
3
5
  end
4
- end
6
+ end
@@ -1,4 +1,6 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module BoletoSimples
2
4
  class BankBilletRemittance < BaseModel
3
5
  end
4
- end
6
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module BoletoSimples
2
4
  class BaseModel
3
5
  include Her::Model
@@ -5,4 +7,4 @@ module BoletoSimples
5
7
  parse_root_in_json true
6
8
  include_root_in_json true
7
9
  end
8
- end
10
+ end
@@ -1,4 +1,6 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module BoletoSimples
2
4
  class Customer < BaseModel
3
5
  end
4
- end
6
+ end
@@ -1,6 +1,8 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module BoletoSimples
2
4
  class CustomerImport < BaseModel
3
- collection_path "imports/customers"
4
- resource_path "imports/customers/:id"
5
+ collection_path 'imports/customers'
6
+ resource_path 'imports/customers/:id'
5
7
  end
6
- end
8
+ end
@@ -1,14 +1,7 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module BoletoSimples
2
4
  class CustomerSubscription < BaseModel
3
-
4
- def next_charge
5
- self.class.request(:_method => :post, :_path => self.class.build_request_path('customer_subscriptions/:id/next_charge', {self.class.primary_key => id})) do |parsed_data, response|
6
- assign_attributes(self.class.parse(parsed_data[:data])) if parsed_data[:data].any?
7
- @metadata = parsed_data[:metadata]
8
- @response_errors = parsed_data[:errors]
9
- @response = response
10
- end
11
- return @response.success?
12
- end
5
+ custom_post :next_charge
13
6
  end
14
7
  end
@@ -1,6 +1,8 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module BoletoSimples
2
4
  class CustomerSubscriptionImport < BaseModel
3
- collection_path "imports/customer_subscriptions"
4
- resource_path "imports/customer_subscriptions/:id"
5
+ collection_path 'imports/customer_subscriptions'
6
+ resource_path 'imports/customer_subscriptions/:id'
5
7
  end
6
- end
8
+ end
@@ -1,14 +1,7 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module BoletoSimples
2
4
  class Discharge < BaseModel
3
-
4
- def pay_off
5
- self.class.request(:_method => :put, :_path => self.class.build_request_path('discharges/:id/pay_off', {self.class.primary_key => id})) do |parsed_data, response|
6
- assign_attributes(self.class.parse(parsed_data[:data])) if parsed_data[:data].any?
7
- @metadata = parsed_data[:metadata]
8
- @response_errors = parsed_data[:errors]
9
- @response = response
10
- end
11
- return @response.success?
12
- end
5
+ custom_put :pay_off
13
6
  end
14
- end
7
+ end
@@ -1,4 +1,6 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module BoletoSimples
2
4
  class EmailDelivery < BaseModel
3
5
  end
4
- end
6
+ end
@@ -1,4 +1,6 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module BoletoSimples
2
4
  class Event < BaseModel
3
5
  end
4
- end
6
+ end
@@ -1,4 +1,6 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module BoletoSimples
2
4
  class Installment < BaseModel
3
5
  end
4
- end
6
+ end
@@ -1,4 +1,6 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module BoletoSimples
2
4
  class Remittance < BaseModel
3
5
  end
4
- end
6
+ end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ module BoletoSimples
4
+ class SmsDelivery < BaseModel
5
+ end
6
+ end