class OntraportApi::Client

Public Class Methods

new(app_id, api_key) click to toggle source
# File lib/ontraport_api/client.rb, line 40
def initialize(app_id, api_key)
  raise InvalidAppIdOrApiKey if [app_id, api_key].any? { |w| w !~ blank_regex }
  @app_id = app_id
  @api_key = api_key
end

Public Instance Methods

method_missing(method, *args, &block) click to toggle source
Calls superclass method
# File lib/ontraport_api/client.rb, line 46
def method_missing(method, *args, &block)
  if method.to_s =~ /^get_[\w]+_by_[\w]+/
    match, subject, keyword = method.to_s.match(/^get_(\w+)_by_(\w+)/).to_a

    if !respond_to?("get_#{subject}".to_sym) || [subject, keyword].any? { |w| w !~ blank_regex }
      return super
    end

    self.send("get_#{subject}", "#{keyword} = '#{args[0]}'")
  else
    super
  end
end

Private Instance Methods

api_credentials_headers() click to toggle source
# File lib/ontraport_api/client.rb, line 73
def api_credentials_headers
  { 'Api-Appid' => @app_id, 'Api-Key' => @api_key }
end
blank_regex() click to toggle source
# File lib/ontraport_api/client.rb, line 77
def blank_regex
  /[^[:space:]]/
end
query(method, path, payload = {}) click to toggle source
# File lib/ontraport_api/client.rb, line 62
def query(method, path, payload = {})
  raise InvalidAPIMethodOrPath if [method, path].any? { |w| w !~ blank_regex } || ![:get, :post, :put, :delete].include?(method)
  response = self.class.send(method, path, query: payload, body: payload, headers: api_credentials_headers )
  response.parsed_response['data']
rescue JSON::ParserError => e
  {
    'error'   => true,
    'message' => response.body
  }
end