Class: Enigma::Endpoint

Inherits:
Object
  • Object
show all
Defined in:
lib/enigma/endpoint.rb

Overview

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

Direct Known Subclasses

Data, Export, Meta, Stats

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(datapath, opts = {}) ⇒ Endpoint

Returns a new instance of Endpoint.



19
20
21
22
# File 'lib/enigma/endpoint.rb', line 19

def initialize(datapath, opts = {})
  self.datapath = datapath
  self.params = opts
end

Instance Attribute Details

#datapathObject

Returns the value of attribute datapath.



13
14
15
# File 'lib/enigma/endpoint.rb', line 13

def datapath
  @datapath
end

#paramsObject

Returns the value of attribute params.



13
14
15
# File 'lib/enigma/endpoint.rb', line 13

def params
  @params
end

#urlObject

Returns the value of attribute url.



13
14
15
# File 'lib/enigma/endpoint.rb', line 13

def url
  @url
end

Class Method Details

.descendantsObject



15
16
17
# File 'lib/enigma/endpoint.rb', line 15

def self.descendants
  ObjectSpace.each_object(Class).select { |klass| klass < self }
end

.url_chunkObject



24
25
26
# File 'lib/enigma/endpoint.rb', line 24

def self.url_chunk
  to_s.gsub(/.*::/, '').downcase
end

Instance Method Details

#pathObject



92
93
94
# File 'lib/enigma/endpoint.rb', line 92

def path
  [Enigma.api_version, url_chunk, Enigma.key, datapath].join('/')
end

#requestObject



100
101
102
103
104
# File 'lib/enigma/endpoint.rb', line 100

def request
  Enigma.logger.info "Making request to #{url}"
  req = Typhoeus::Request.new(url, method: :get, params: params).run
  Response.parse(req)
end

#serialize_search(search) ⇒ String

Serialize a search clause. Allows you to pass in a hash of one or more fieldName: value pairs

Parameters:

  • search (String|Hash)

    clause to convert

Returns:

  • (String)

    parameter ready for the request



61
62
63
64
65
66
67
68
69
70
# File 'lib/enigma/endpoint.rb', line 61

def serialize_search(search)
  if search.is_a? Hash
    search.map do |field, value|
      value = [value].flatten.join('|')
      "@#{field} (#{value})"
    end.join ' '
  else
    search
  end
end

#serialize_select(select) ⇒ String

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

Parameters:

  • select (String|Array)

    clause to convert

Returns:

  • (String)

    parameter ready for the request



78
79
80
81
82
83
84
# File 'lib/enigma/endpoint.rb', line 78

def serialize_select(select)
  if select.is_a? Enumerable
    select.join(',')
  else
    select
  end
end

#serialize_where(where) ⇒ String

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.

Parameters:

  • where (String|Hash)

    clause to convert

Returns:

  • (String)

    parameter ready for the request



46
47
48
49
50
51
52
53
# File 'lib/enigma/endpoint.rb', line 46

def serialize_where(where)
  if where.is_a? Hash
    column, value = where.first
    "#{column}=#{value}"
  else
    where
  end
end

#url_chunkObject

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



88
89
90
# File 'lib/enigma/endpoint.rb', line 88

def url_chunk
  self.class.url_chunk
end