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
Public Class Methods
# File lib/enigma/endpoint.rb, line 15 def self.descendants ObjectSpace.each_object(Class).select { |klass| klass < self } end
# File lib/enigma/endpoint.rb, line 19 def initialize(datapath, opts = {}) self.datapath = datapath self.params = opts end
# File lib/enigma/endpoint.rb, line 24 def self.url_chunk to_s.gsub(/.*::/, '').downcase end
Public Instance Methods
# File lib/enigma/endpoint.rb, line 94 def path [Enigma.api_version, url_chunk, Enigma.key, datapath].join('/') end
# 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 a search clause. Allows you to pass in a hash of one or more fieldName: value pairs
@param [String|Hash] search clause to convert @return [String] parameter ready for the request
# File lib/enigma/endpoint.rb, line 63 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 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 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
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