Method: SimpleSpark::Client#initialize

Defined in:
lib/simple_spark/client.rb

#initialize(opts = {}) ⇒ Client

Returns a new instance of Client.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/simple_spark/client.rb', line 10

def initialize(opts = {})
  @api_key = opts[:api_key] || ENV['SPARKPOST_API_KEY']
  @api_host = opts[:api_host] || 'https://api.sparkpost.com'
  @base_path = opts[:base_path] || '/api/v1/'
  @subaccount_id = opts[:subaccount_id]
  @headers = opts[:headers]

  @logger = opts[:logger] || SimpleSpark::Client.default_logger

  fail Exceptions::InvalidConfiguration.new, 'You must provide a SparkPost API key' unless @api_key
  fail Exceptions::InvalidConfiguration.new, 'You must provide a SparkPost API host' unless @api_host # this should never occur unless the default above is changed
  fail Exceptions::InvalidConfiguration.new, 'You must provide a SparkPost base path' unless @base_path # this should never occur unless the default above is changed
  fail Exceptions::InvalidConfiguration.new, 'The headers options provided must be a valid Hash' if @headers && !@headers.is_a?(Hash)

  rails_development = true & defined?(Rails) && Rails.env.development?

  @debug = opts[:debug].nil? ? rails_development : opts[:debug]

  # switch debug params, allow for old and new parameters and supress Excon warning
  debug_options = Excon::VALID_REQUEST_KEYS.any? { |k| k == :debug } ? { debug: @debug } : { debug_request: @debug, debug_response: @debug }

  @session = Excon.new(@api_host, debug_options)
end