Class: BoletoSimples::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/boletosimples/configuration.rb

Constant Summary collapse

BASE_URI =
{
  sandbox: 'https://api-sandbox.kobana.com.br/v1',
  production: 'https://api.kobana.com.br/v1',
  development: 'http://localhost:5000/api/v1'
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration.



13
14
15
16
17
18
19
20
# File 'lib/boletosimples/configuration.rb', line 13

def initialize
  @environment = ENV.fetch('BOLETOSIMPLES_ENV', :sandbox).to_sym
  @api_token = ENV.fetch('BOLETOSIMPLES_API_TOKEN', nil)
  @user_agent = ENV.fetch('BOLETOSIMPLES_USER_AGENT', nil)
  @custom_headers = {}
  @cache = nil
  @debug = ENV.fetch('BOLETOSIMPLES_DEBUG', nil)
end

Instance Attribute Details

#api_tokenObject

Returns the value of attribute api_token.



5
6
7
# File 'lib/boletosimples/configuration.rb', line 5

def api_token
  @api_token
end

#cacheObject

Returns the value of attribute cache.



5
6
7
# File 'lib/boletosimples/configuration.rb', line 5

def cache
  @cache
end

#custom_headersObject

Returns the value of attribute custom_headers.



5
6
7
# File 'lib/boletosimples/configuration.rb', line 5

def custom_headers
  @custom_headers
end

#debugObject

Returns the value of attribute debug.



5
6
7
# File 'lib/boletosimples/configuration.rb', line 5

def debug
  @debug
end

#environmentObject

Returns the value of attribute environment.



5
6
7
# File 'lib/boletosimples/configuration.rb', line 5

def environment
  @environment
end

#user_agentObject

Returns the value of attribute user_agent.



5
6
7
# File 'lib/boletosimples/configuration.rb', line 5

def user_agent
  @user_agent
end

Instance Method Details

#api_token?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/boletosimples/configuration.rb', line 26

def api_token?
  !@api_token.nil?
end

#base_uriObject



22
23
24
# File 'lib/boletosimples/configuration.rb', line 22

def base_uri
  BASE_URI[@environment]
end

#debug?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/boletosimples/configuration.rb', line 30

def debug?
  !@debug.nil?
end

#setup_herObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/boletosimples/configuration.rb', line 34

def setup_her
  Her::API.setup url: base_uri do |c|
    # Request
    c.use BoletoSimples::Middleware::UserAgent
    c.use BoletoSimples::Middleware::Bearer if api_token?
    c.use BoletoSimples::Middleware::CustomHeaders
    c.use Faraday::Request::Multipart
    c.use FaradayMiddleware::EncodeJson
    c.use Her::Middleware::AcceptJSON
    c.use Faraday::HttpCache, store: cache, serializer: Marshal unless cache.nil?

    # Response
    c.use BoletoSimples::Middleware::Debug if debug?
    c.use BoletoSimples::Middleware::LastRequest
    c.use BoletoSimples::Middleware::RaiseError
    c.use Her::Middleware::DefaultParseJSON

    # Adapter
    c.adapter Faraday::Adapter::NetHttp
  end

  # Because Her set the api on the moment module is included we need to call use_api again, after changing the configuration.
  [BankBillet, BankBilletAccount, Customer, CustomerImport, CustomerSubscription,
   CustomerSubscriptionImport, Installment, Transaction, Webhook, Discharge,
   Remittance, WebhookDelivery, Event, EmailDelivery, BankBilletDischarge,
   BankBilletPayment, BankBilletRemittance, SmsDelivery].each do |klass|
    klass.send(:use_api, Her::API.default_api)
  end
end