Class: Algolia::Client
- Inherits:
-
Object
- Object
- Algolia::Client
- Defined in:
- lib/algolia/client.rb
Overview
A class which encapsulates the HTTPS communication with the Algolia API server. Uses the Curb (Curl) library for low-level HTTP communication.
Instance Attribute Summary collapse
-
#api_key ⇒ Object
readonly
Returns the value of attribute api_key.
-
#application_id ⇒ Object
readonly
Returns the value of attribute application_id.
-
#hosts ⇒ Object
readonly
Returns the value of attribute hosts.
Instance Method Summary collapse
- #delete(uri) ⇒ Object
- #get(uri) ⇒ Object
-
#initialize(data = {}) ⇒ Client
constructor
A new instance of Client.
- #post(uri, body) ⇒ Object
- #put(uri, body) ⇒ Object
-
#request(uri, method, data = nil) ⇒ Object
Perform an HTTP request for the given uri and method with common basic response handling.
-
#thread_local_hosts ⇒ Object
this method returns a thread-local array of sessions.
Constructor Details
#initialize(data = {}) ⇒ Client
Returns a new instance of Client.
16 17 18 19 20 21 22 |
# File 'lib/algolia/client.rb', line 16 def initialize(data = {}) @ssl = data[:ssl].nil? ? true : data[:ssl] @application_id = data[:application_id] @api_key = data[:api_key] @hosts = (data[:hosts] || 1.upto(3).map { |i| "#{@application_id}-#{i}.algolia.io" }).shuffle @debug = data[:debug] end |
Instance Attribute Details
#api_key ⇒ Object (readonly)
Returns the value of attribute api_key.
14 15 16 |
# File 'lib/algolia/client.rb', line 14 def api_key @api_key end |
#application_id ⇒ Object (readonly)
Returns the value of attribute application_id.
13 14 15 |
# File 'lib/algolia/client.rb', line 13 def application_id @application_id end |
#hosts ⇒ Object (readonly)
Returns the value of attribute hosts.
12 13 14 |
# File 'lib/algolia/client.rb', line 12 def hosts @hosts end |
Instance Method Details
#delete(uri) ⇒ Object
56 57 58 |
# File 'lib/algolia/client.rb', line 56 def delete(uri) request(uri, :DELETE) end |
#get(uri) ⇒ Object
44 45 46 |
# File 'lib/algolia/client.rb', line 44 def get(uri) request(uri, :GET) end |
#post(uri, body) ⇒ Object
48 49 50 |
# File 'lib/algolia/client.rb', line 48 def post(uri, body) request(uri, :POST, body) end |
#put(uri, body) ⇒ Object
52 53 54 |
# File 'lib/algolia/client.rb', line 52 def put(uri, body) request(uri, :PUT, body) end |
#request(uri, method, data = nil) ⇒ Object
Perform an HTTP request for the given uri and method with common basic response handling. Will raise a AlgoliaProtocolError if the response has an error status code, and will return the parsed JSON body on success, if there is one.
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/algolia/client.rb', line 28 def request(uri, method, data = nil) exceptions = [] thread_local_hosts.each do |host| begin session = perform_request(host, uri, method, data) return JSON.parse(session.body_str) rescue AlgoliaProtocolError => e raise if e.code != Protocol::ERROR_TIMEOUT and e.code != Protocol::ERROR_UNAVAILABLE exceptions << e rescue Curl::Err::CurlError => e exceptions << e end end raise AlgoliaProtocolError.new(0, "Cannot reach any host: #{exceptions.map { |e| e.to_s }.join(', ')}") end |
#thread_local_hosts ⇒ Object
this method returns a thread-local array of sessions
61 62 63 64 65 66 67 68 69 |
# File 'lib/algolia/client.rb', line 61 def thread_local_hosts Thread.current[:algolia_hosts] ||= hosts.map do |host| hinfo = {} hinfo["base_url"] = "http#{@ssl ? 's' : ''}://#{host}" hinfo["host"] = host hinfo["session"] = init_session hinfo end end |