Class: Enigma::Endpoint
- Inherits:
-
Object
- Object
- Enigma::Endpoint
- 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
Instance Attribute Summary collapse
-
#datapath ⇒ Object
Returns the value of attribute datapath.
-
#params ⇒ Object
Returns the value of attribute params.
-
#url ⇒ Object
Returns the value of attribute url.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(datapath, opts = {}) ⇒ Endpoint
constructor
A new instance of Endpoint.
- #path ⇒ Object
- #request ⇒ Object
-
#serialize_search(search) ⇒ String
Serialize a search clause.
-
#serialize_select(select) ⇒ String
Serialize a search clause.
-
#serialize_where(where) ⇒ String
Serialize a where clause.
-
#url_chunk ⇒ Object
Endpoints show up in urls as a lowercase version of their class names.
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
#datapath ⇒ Object
Returns the value of attribute datapath.
13 14 15 |
# File 'lib/enigma/endpoint.rb', line 13 def datapath @datapath end |
#params ⇒ Object
Returns the value of attribute params.
13 14 15 |
# File 'lib/enigma/endpoint.rb', line 13 def params @params end |
#url ⇒ Object
Returns the value of attribute url.
13 14 15 |
# File 'lib/enigma/endpoint.rb', line 13 def url @url end |
Class Method Details
.descendants ⇒ Object
15 16 17 |
# File 'lib/enigma/endpoint.rb', line 15 def self.descendants ObjectSpace.each_object(Class).select { |klass| klass < self } end |
.url_chunk ⇒ Object
24 25 26 |
# File 'lib/enigma/endpoint.rb', line 24 def self.url_chunk to_s.gsub(/.*::/, '').downcase end |
Instance Method Details
#path ⇒ Object
92 93 94 |
# File 'lib/enigma/endpoint.rb', line 92 def path [Enigma.api_version, url_chunk, Enigma.key, datapath].join('/') end |
#request ⇒ Object
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
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
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.
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_chunk ⇒ Object
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 |