class Enigma::Endpoint

Generic Enigma endpoint. Knows how to construct a URL, request it, and wrap the response in an ‘Enigma::Response`

Assumes the api endpoints for its descendants are a lowercase version of their class names

Handles some nice conversion of select, search, and where clauses from ruby hashes to string parameters

Attributes

datapath[RW]
params[RW]
url[RW]

Public Class Methods

descendants() click to toggle source
# File lib/enigma/endpoint.rb, line 15
def self.descendants
  ObjectSpace.each_object(Class).select { |klass| klass < self }
end
new(datapath, opts = {}) click to toggle source
# File lib/enigma/endpoint.rb, line 19
def initialize(datapath, opts = {})
  self.datapath = datapath
  self.params = opts
end
url_chunk() click to toggle source
# File lib/enigma/endpoint.rb, line 24
def self.url_chunk
  to_s.gsub(/.*::/, '').downcase
end

Public Instance Methods

path() click to toggle source
# File lib/enigma/endpoint.rb, line 94
def path
  [Enigma.api_version, url_chunk, Enigma.key, datapath].join('/')
end
request() click to toggle source
# File lib/enigma/endpoint.rb, line 102
def request
  Enigma.logger.info "Making request to #{url}"
  req = Typhoeus::Request.new(url, method: :get, params: params).run
  Response.parse(req)
end
serialize_select(select) click to toggle source

Serialize a search clause. Allows you to pass in an array of column names

@param [String|Array] select clause to convert @return [String] parameter ready for the request

# File lib/enigma/endpoint.rb, line 80
def serialize_select(select)
  if select.is_a? Enumerable
    select.join(',')
  else
    select
  end
end
serialize_where(where) click to toggle source

Serialize a where clause. Allows you to pass in a hash and have it converted to an equality where

> Filter results with a SQL-style “where” clause. Only applies to > numerical columns - use the “search” parameter for strings. Valid > operators are >, < and =. Only one “where” clause per request is > currently supported.

@param [String|Hash] where clause to convert @return [String] parameter ready for the request

# File lib/enigma/endpoint.rb, line 48
def serialize_where(where)
  if where.is_a? Hash
    column, value = where.first
    "#{column}=#{value}"
  else
    where
  end
end
url_chunk() click to toggle source

Endpoints show up in urls as a lowercase version of their class names

# File lib/enigma/endpoint.rb, line 90
def url_chunk
  self.class.url_chunk
end