class SendgridToolkit::AbstractSendgridClient
Public Class Methods
new(api_user = nil, api_key = nil)
click to toggle source
# File lib/sendgrid_toolkit/abstract_sendgrid_client.rb, line 4 def initialize(api_user = nil, api_key = nil) @api_user = api_user || SendgridToolkit.api_user || ENV['SMTP_USERNAME'] @api_key = api_key || SendgridToolkit.api_key || ENV['SMTP_PASSWORD'] raise SendgridToolkit::NoAPIUserSpecified if @api_user.nil? || @api_user.length == 0 raise SendgridToolkit::NoAPIKeySpecified if @api_key.nil? || @api_key.length == 0 end
Protected Instance Methods
api_post(module_name, action_name, opts = {})
click to toggle source
# File lib/sendgrid_toolkit/abstract_sendgrid_client.rb, line 14 def api_post(module_name, action_name, opts = {}) base_path = compose_base_path(module_name, action_name) response = HTTParty.post("https://#{BASE_URI}/#{base_path}.json?", :query => get_credentials.merge(opts), :format => :json) if response.code > 401 raise(SendgridToolkit::SendgridServerError, "The sengrid server returned an error. #{response.inspect}") elsif has_error?(response) and response['error'].respond_to?(:has_key?) and response['error'].has_key?('code') and response['error']['code'].to_i == 401 raise SendgridToolkit::AuthenticationFailed elsif has_error?(response) raise(SendgridToolkit::APIError, response['error']) end response end
compose_base_path(module_name, action_name)
click to toggle source
# File lib/sendgrid_toolkit/abstract_sendgrid_client.rb, line 36 def compose_base_path(module_name, action_name) "#{module_name}.#{action_name}" end
get_credentials()
click to toggle source
# File lib/sendgrid_toolkit/abstract_sendgrid_client.rb, line 32 def get_credentials {:api_user => @api_user, :api_key => @api_key} end
Private Instance Methods
has_error?(response)
click to toggle source
# File lib/sendgrid_toolkit/abstract_sendgrid_client.rb, line 41 def has_error?(response) response.kind_of?(Hash) && response.has_key?('error') end