class SimpleSpark::Client

Attributes

logger[R]

Public Class Methods

default_logger() click to toggle source
# File lib/simple_spark/client.rb, line 101
def self.default_logger
  logger = defined?(Rails) ? Rails.logger : Logger.new(STDOUT)
  logger.progname = 'simple_spark' if logger.respond_to?(:progname=)
  logger
end
new(opts = {}) click to toggle source
# 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

Public Instance Methods

account() click to toggle source
# File lib/simple_spark/client.rb, line 107
def account
  Endpoints::Account.new(self)
end
call(opts) click to toggle source
# File lib/simple_spark/client.rb, line 34
def call(opts)
  method = opts[:method]
  path = opts[:path]
  body_values = opts[:body_values] || {}
  query_params = opts[:query_values] || {}
  extract_results = opts[:extract_results].nil? ? true : opts[:extract_results]

  fail Exceptions::InvalidConfiguration.new(method: method), 'Only GET, POST, PUT and DELETE are supported' unless [:get, :post, :put, :delete].include?(method)

  path = "#{@base_path}#{path}"
  params = { path: path, headers: headers }
  params[:body] = JSON.generate(body_values) unless body_values.empty?
  params[:query] = query_params unless query_params.empty?

  if @debug
    logger.debug("Calling #{method}")
    logger.debug(params)
  end

  response = @session.send(method.to_s, params)

  if @debug
    logger.debug("Response #{response.status}")
    logger.debug(response)
  end

  fail Exceptions::GatewayTimeoutExceeded, 'Received 504 from SparkPost API' if response.status == 504

  process_response(response, extract_results)

rescue Excon::Errors::Timeout
  raise Exceptions::GatewayTimeoutExceeded
end
events() click to toggle source
# File lib/simple_spark/client.rb, line 135
def events
  Endpoints::Events.new(self)
end
headers() click to toggle source
# File lib/simple_spark/client.rb, line 89
def headers
  defaults = {
    'User-Agent' => 'simple_spark/' + VERSION,
    'Content-Type' => 'application/json',
    'Accept' => 'application/json',
    'Authorization' => @api_key
  }
  defaults.merge!('X-MSYS-SUBACCOUNT' => @subaccount_id) if @subaccount_id
  defaults.merge!(@headers) if @headers
  defaults
end
inbound_domains() click to toggle source
# File lib/simple_spark/client.rb, line 119
def inbound_domains
  Endpoints::InboundDomains.new(self)
end
message_events() click to toggle source
# File lib/simple_spark/client.rb, line 139
def message_events
  Endpoints::MessageEvents.new(self)
end
metrics() click to toggle source
# File lib/simple_spark/client.rb, line 111
def metrics
  Endpoints::Metrics.new(self)
end
process_response(response, extract_results) click to toggle source
# File lib/simple_spark/client.rb, line 68
def process_response(response, extract_results)
  logger.warn('Response had an empty body') if (response.body.nil? || response.body == '') && response.status != 204
  return {} if response.status == 204 || response.body.nil? || response.body == ''

  response_body = JSON.parse(response.body)
  if response_body['errors']
    Exceptions::Error.fail_with_exception_for_status(response.status, response_body['errors'], response_body['results'])
  else
    if extract_results
      response_body['results'] ? response_body['results'] : {}
    else
      response_body
    end
  end
end
recipient_lists() click to toggle source
# File lib/simple_spark/client.rb, line 155
def recipient_lists
  Endpoints::RecipientLists.new(self)
end
relay_webhooks() click to toggle source
# File lib/simple_spark/client.rb, line 147
def relay_webhooks
  Endpoints::RelayWebhooks.new(self)
end
sending_domains() click to toggle source
# File lib/simple_spark/client.rb, line 123
def sending_domains
  Endpoints::SendingDomains.new(self)
end
subaccounts() click to toggle source
# File lib/simple_spark/client.rb, line 115
def subaccounts
  Endpoints::Subaccounts.new(self)
end
suppression_list() click to toggle source
# File lib/simple_spark/client.rb, line 151
def suppression_list
  Endpoints::SuppressionList.new(self)
end
templates() click to toggle source
# File lib/simple_spark/client.rb, line 127
def templates
  Endpoints::Templates.new(self)
end
transmissions() click to toggle source
# File lib/simple_spark/client.rb, line 131
def transmissions
  Endpoints::Transmissions.new(self)
end
url_encode(s) click to toggle source

Copied from apidock.com/ruby/ERB/Util/url_encode

# File lib/simple_spark/client.rb, line 85
def url_encode(s)
  s.to_s.dup.force_encoding('ASCII-8BIT').gsub(/[^a-zA-Z0-9_\-.]/) { sprintf('%%%02X', $&.unpack('C')[0]) }
end
webhooks() click to toggle source
# File lib/simple_spark/client.rb, line 143
def webhooks
  Endpoints::Webhooks.new(self)
end