Class: SimpleSpark::Client
- Inherits:
-
Object
- Object
- SimpleSpark::Client
- Defined in:
- lib/simple_spark/client.rb
Instance Attribute Summary collapse
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
Class Method Summary collapse
Instance Method Summary collapse
- #call(opts) ⇒ Object
- #headers ⇒ Object
- #inbound_domains ⇒ Object
-
#initialize(opts = {}) ⇒ Client
constructor
A new instance of Client.
- #message_events ⇒ Object
- #metrics ⇒ Object
- #process_response(response, extract_results) ⇒ Object
- #relay_webhooks ⇒ Object
- #sending_domains ⇒ Object
- #subaccounts ⇒ Object
- #templates ⇒ Object
- #transmissions ⇒ Object
-
#url_encode(s) ⇒ Object
Copied from apidock.com/ruby/ERB/Util/url_encode.
- #webhooks ⇒ Object
Constructor Details
#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 |
# 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] @session = Excon.new(@api_host, debug: @debug) end |
Instance Attribute Details
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
8 9 10 |
# File 'lib/simple_spark/client.rb', line 8 def logger @logger end |
Class Method Details
.default_logger ⇒ Object
98 99 100 101 102 |
# File 'lib/simple_spark/client.rb', line 98 def self.default_logger logger = defined?(Rails) ? Rails.logger : Logger.new(STDOUT) logger.progname = 'simple_spark' if logger.respond_to?(:progname=) logger end |
Instance Method Details
#call(opts) ⇒ Object
31 32 33 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 63 |
# File 'lib/simple_spark/client.rb', line 31 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] = body_values.to_json 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 |
#headers ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/simple_spark/client.rb', line 86 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 ⇒ Object
112 113 114 |
# File 'lib/simple_spark/client.rb', line 112 def inbound_domains Endpoints::InboundDomains.new(self) end |
#message_events ⇒ Object
128 129 130 |
# File 'lib/simple_spark/client.rb', line 128 def Endpoints::MessageEvents.new(self) end |
#metrics ⇒ Object
104 105 106 |
# File 'lib/simple_spark/client.rb', line 104 def metrics Endpoints::Metrics.new(self) end |
#process_response(response, extract_results) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/simple_spark/client.rb', line 65 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']) else if extract_results response_body['results'] ? response_body['results'] : {} else response_body end end end |
#relay_webhooks ⇒ Object
136 137 138 |
# File 'lib/simple_spark/client.rb', line 136 def relay_webhooks Endpoints::RelayWebhooks.new(self) end |
#sending_domains ⇒ Object
116 117 118 |
# File 'lib/simple_spark/client.rb', line 116 def sending_domains Endpoints::SendingDomains.new(self) end |
#subaccounts ⇒ Object
108 109 110 |
# File 'lib/simple_spark/client.rb', line 108 def subaccounts Endpoints::Subaccounts.new(self) end |
#templates ⇒ Object
120 121 122 |
# File 'lib/simple_spark/client.rb', line 120 def templates Endpoints::Templates.new(self) end |
#transmissions ⇒ Object
124 125 126 |
# File 'lib/simple_spark/client.rb', line 124 def transmissions Endpoints::Transmissions.new(self) end |
#url_encode(s) ⇒ Object
Copied from apidock.com/ruby/ERB/Util/url_encode
82 83 84 |
# File 'lib/simple_spark/client.rb', line 82 def url_encode(s) s.to_s.dup.force_encoding('ASCII-8BIT').gsub(/[^a-zA-Z0-9_\-.]/) { sprintf('%%%02X', $&.unpack('C')[0]) } end |