class Ptilinopus::API
Constants
- API_PATH
- DEFAULT_HEADER
Attributes
api_key[RW]
api_key[RW]
Public Class Methods
new(api_key = nil)
click to toggle source
# File lib/ptilinopus.rb, line 14 def initialize(api_key = nil) @api_key = api_key || self.class.api_key end
Private Class Methods
method_missing(sym, *args, &block)
click to toggle source
# File lib/ptilinopus.rb, line 51 def method_missing(sym, *args, &block) new(self.api_key).send(sym, *args, &block) end
Public Instance Methods
call(type, method, params = {})
click to toggle source
# File lib/ptilinopus.rb, line 18 def call(type, method, params = {}) ensure_api_key(params) params = params.merge({apiKey: @api_key}) response = self.class.send(type, API_PATH + method, body: params.to_json, headers: DEFAULT_HEADER) if response.code != 200 case response.code when 400 raise MailerliteInvalidMethodError.new when 401 raise MailerliteInvalidApiKeyError.new when 404 raise MailerliteBadRequestItemError.new else raise MailerliteServerError.new end end return response.body end
Private Instance Methods
ensure_api_key(params)
click to toggle source
# File lib/ptilinopus.rb, line 42 def ensure_api_key(params) unless @api_key || params[:apiKey] raise StandardError, "You must set an api_key prior to making a call" end end