Class: Billrb::Configuration

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

Constant Summary collapse

ENVIRONMENTS =
{
  production: "https://gateway.prod.bill.com",
  sandbox: "https://gateway.stage.bill.com",
}.freeze
SECRET_ATTRS =
%i[password dev_key].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(username: nil, password: nil, organization_id: nil, dev_key: nil, environment: :sandbox, timeout: 30, open_timeout: 5, logger: nil, max_retries: 2, retry_backoff: 0.5, adapter: nil, faraday: nil) ⇒ Configuration

Returns a new instance of Configuration.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/billrb/configuration.rb', line 17

def initialize(username: nil, password: nil, organization_id: nil, dev_key: nil,
               environment: :sandbox, timeout: 30, open_timeout: 5, logger: nil,
               max_retries: 2, retry_backoff: 0.5, adapter: nil, faraday: nil)
  self.environment = environment
  @username = username
  @password = password
  @organization_id = organization_id
  @dev_key = dev_key
  @timeout = timeout
  @open_timeout = open_timeout
  @logger = logger
  @max_retries = max_retries
  @retry_backoff = retry_backoff
  @adapter = adapter
  @faraday = faraday
end

Instance Attribute Details

#adapterObject

Returns the value of attribute adapter.



12
13
14
# File 'lib/billrb/configuration.rb', line 12

def adapter
  @adapter
end

#dev_keyObject

Returns the value of attribute dev_key.



12
13
14
# File 'lib/billrb/configuration.rb', line 12

def dev_key
  @dev_key
end

#environmentObject

Returns the value of attribute environment.



15
16
17
# File 'lib/billrb/configuration.rb', line 15

def environment
  @environment
end

#faradayObject

Returns the value of attribute faraday.



12
13
14
# File 'lib/billrb/configuration.rb', line 12

def faraday
  @faraday
end

#loggerObject

Returns the value of attribute logger.



12
13
14
# File 'lib/billrb/configuration.rb', line 12

def logger
  @logger
end

#max_retriesObject

Returns the value of attribute max_retries.



12
13
14
# File 'lib/billrb/configuration.rb', line 12

def max_retries
  @max_retries
end

#open_timeoutObject

Returns the value of attribute open_timeout.



12
13
14
# File 'lib/billrb/configuration.rb', line 12

def open_timeout
  @open_timeout
end

#organization_idObject

Returns the value of attribute organization_id.



12
13
14
# File 'lib/billrb/configuration.rb', line 12

def organization_id
  @organization_id
end

#passwordObject

Returns the value of attribute password.



12
13
14
# File 'lib/billrb/configuration.rb', line 12

def password
  @password
end

#retry_backoffObject

Returns the value of attribute retry_backoff.



12
13
14
# File 'lib/billrb/configuration.rb', line 12

def retry_backoff
  @retry_backoff
end

#timeoutObject

Returns the value of attribute timeout.



12
13
14
# File 'lib/billrb/configuration.rb', line 12

def timeout
  @timeout
end

#usernameObject

Returns the value of attribute username.



12
13
14
# File 'lib/billrb/configuration.rb', line 12

def username
  @username
end

Instance Method Details

#base_urlObject



43
44
45
# File 'lib/billrb/configuration.rb', line 43

def base_url
  ENVIRONMENTS.fetch(environment)
end

#inspectObject

Redact credentials so they never leak into logs or error dumps.



57
58
59
60
61
# File 'lib/billrb/configuration.rb', line 57

def inspect
  "#<#{self.class.name} environment=#{environment.inspect} " \
  "username=#{username.inspect} organization_id=#{organization_id.inspect} " \
  "password=#{redacted(password)} dev_key=#{redacted(dev_key)}>"
end

#validate!Object

Raises:



47
48
49
50
51
52
53
54
# File 'lib/billrb/configuration.rb', line 47

def validate!
  missing = %i[username password organization_id dev_key].select do |key|
    public_send(key).to_s.empty?
  end
  return if missing.empty?

  raise ConfigurationError, "missing required configuration: #{missing.join(", ")}"
end