Class: SimpleSpark::Client
- Inherits:
-
Object
- Object
- SimpleSpark::Client
- Defined in:
- lib/simple_spark/client.rb
Instance Method Summary collapse
- #call(method, path, body_values = {}, query_params = {}) ⇒ Object
- #default_headers ⇒ Object
- #inbound_domains ⇒ Object
-
#initialize(api_key = nil, api_host = 'https://api.sparkpost.com', base_path = '/api/v1/', debug = nil) ⇒ Client
constructor
A new instance of Client.
- #message_events ⇒ Object
- #process_response(response) ⇒ Object
- #relay_webhooks ⇒ Object
- #sending_domains ⇒ Object
- #templates ⇒ Object
- #transmissions ⇒ Object
-
#url_encode(s) ⇒ Object
Copied from apidock.com/ruby/ERB/Util/url_encode.
- #webhooks ⇒ Object
Constructor Details
#initialize(api_key = nil, api_host = 'https://api.sparkpost.com', base_path = '/api/v1/', debug = nil) ⇒ Client
Returns a new instance of Client.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/simple_spark/client.rb', line 7 def initialize(api_key = nil, api_host = 'https://api.sparkpost.com', base_path = '/api/v1/', debug = nil) @api_key = api_key || ENV['SPARKPOST_API_KEY'] @api_host = api_host || 'https://api.sparkpost.com' @base_path = base_path || '/api/v1/' 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 rails_development = !(defined?(Rails) && Rails.env.development?).nil? @debug = debug.nil? ? rails_development : debug @session = Excon.new(@api_host, debug: @debug) end |
Instance Method Details
#call(method, path, body_values = {}, query_params = {}) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/simple_spark/client.rb', line 22 def call(method, path, body_values = {}, query_params = {}) 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: default_headers } params[:body] = body_values.to_json unless body_values.empty? params[:query] = query_params unless query_params.empty? response = @session.send(method.to_s, params) process_response(response) end |
#default_headers ⇒ Object
50 51 52 53 54 55 56 |
# File 'lib/simple_spark/client.rb', line 50 def default_headers { 'User-Agent' => 'simple_spark/' + VERSION, 'Content-Type' => 'application/json', 'Authorization' => @api_key } end |
#inbound_domains ⇒ Object
58 59 60 |
# File 'lib/simple_spark/client.rb', line 58 def inbound_domains Endpoints::InboundDomains.new(self) end |
#message_events ⇒ Object
74 75 76 |
# File 'lib/simple_spark/client.rb', line 74 def Endpoints::MessageEvents.new(self) end |
#process_response(response) ⇒ Object
34 35 36 37 38 39 40 41 42 43 |
# File 'lib/simple_spark/client.rb', line 34 def process_response(response) return true if response.status == 204 response_body = JSON.parse(response.body) if response_body['errors'] Exceptions::Error.fail_with_exception_for_status(response.status, response_body['errors']) else response_body['results'] ? response_body['results'] : true end end |
#relay_webhooks ⇒ Object
82 83 84 |
# File 'lib/simple_spark/client.rb', line 82 def relay_webhooks Endpoints::RelayWebhooks.new(self) end |
#sending_domains ⇒ Object
62 63 64 |
# File 'lib/simple_spark/client.rb', line 62 def sending_domains Endpoints::SendingDomains.new(self) end |
#templates ⇒ Object
66 67 68 |
# File 'lib/simple_spark/client.rb', line 66 def templates Endpoints::Templates.new(self) end |
#transmissions ⇒ Object
70 71 72 |
# File 'lib/simple_spark/client.rb', line 70 def transmissions Endpoints::Transmissions.new(self) end |
#url_encode(s) ⇒ Object
Copied from apidock.com/ruby/ERB/Util/url_encode
46 47 48 |
# File 'lib/simple_spark/client.rb', line 46 def url_encode(s) s.to_s.dup.force_encoding("ASCII-8BIT").gsub(/[^a-zA-Z0-9_\-.]/) { sprintf("%%%02X", $&.unpack("C")[0]) } end |
#webhooks ⇒ Object
78 79 80 |
# File 'lib/simple_spark/client.rb', line 78 def webhooks Endpoints::Webhooks.new(self) end |