Class: Algolia::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/algolia/client.rb

Overview

A class which encapsulates the HTTPS communication with the Algolia API server. Uses the HTTPClient library for low-level HTTP communication.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = {}) ⇒ Client

Returns a new instance of Client.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# 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
  @connect_timeout = data[:connect_timeout]
  @send_timeout    = data[:send_timeout]
  @receive_timeout = data[:receive_timeout]
  @search_timeout  = data[:search_timeout]
  @headers = {
    Protocol::HEADER_API_KEY => api_key,
    Protocol::HEADER_APP_ID  => application_id,
    'Content-Type'           => 'application/json; charset=utf-8',
    'User-Agent'             => "Algolia for Ruby #{::Algolia::VERSION}"
  }
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



13
14
15
# File 'lib/algolia/client.rb', line 13

def api_key
  @api_key
end

#application_idObject (readonly)

Returns the value of attribute application_id.



13
14
15
# File 'lib/algolia/client.rb', line 13

def application_id
  @application_id
end

#connect_timeoutObject (readonly)

Returns the value of attribute connect_timeout.



13
14
15
# File 'lib/algolia/client.rb', line 13

def connect_timeout
  @connect_timeout
end

#headersObject (readonly)

Returns the value of attribute headers.



13
14
15
# File 'lib/algolia/client.rb', line 13

def headers
  @headers
end

#hostsObject (readonly)

Returns the value of attribute hosts.



13
14
15
# File 'lib/algolia/client.rb', line 13

def hosts
  @hosts
end

#receive_timeoutObject (readonly)

Returns the value of attribute receive_timeout.



13
14
15
# File 'lib/algolia/client.rb', line 13

def receive_timeout
  @receive_timeout
end

#search_timeoutObject (readonly)

Returns the value of attribute search_timeout.



13
14
15
# File 'lib/algolia/client.rb', line 13

def search_timeout
  @search_timeout
end

#send_timeoutObject (readonly)

Returns the value of attribute send_timeout.



13
14
15
# File 'lib/algolia/client.rb', line 13

def send_timeout
  @send_timeout
end

#sslObject (readonly)

Returns the value of attribute ssl.



13
14
15
# File 'lib/algolia/client.rb', line 13

def ssl
  @ssl
end

Instance Method Details

#delete(uri, timeout = nil) ⇒ Object



64
65
66
# File 'lib/algolia/client.rb', line 64

def delete(uri, timeout = nil)
  request(uri, :DELETE, nil, timeout)
end

#get(uri, timeout = nil) ⇒ Object



52
53
54
# File 'lib/algolia/client.rb', line 52

def get(uri, timeout = nil)
  request(uri, :GET, nil, timeout)
end

#post(uri, body = {}, timeout = nil) ⇒ Object



56
57
58
# File 'lib/algolia/client.rb', line 56

def post(uri, body = {}, timeout = nil)
  request(uri, :POST, body, timeout)
end

#put(uri, body = {}, timeout = nil) ⇒ Object



60
61
62
# File 'lib/algolia/client.rb', line 60

def put(uri, body = {}, timeout = nil)
  request(uri, :PUT, body, timeout)
end

#request(uri, method, data = nil, timeout = 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.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/algolia/client.rb', line 37

def request(uri, method, data = nil, timeout = nil)
  exceptions = []
  thread_local_hosts(timeout).each do |host|
    begin
      return perform_request(host[:session], host[:base_url] + uri, method, data)
    rescue AlgoliaProtocolError => e
      raise if e.code == Protocol::ERROR_BAD_REQUEST or e.code == Protocol::ERROR_FORBIDDEN or e.code == Protocol::ERROR_NOT_FOUND
      exceptions << e
    rescue => e
      exceptions << e
    end
  end
  raise AlgoliaProtocolError.new(0, "Cannot reach any host: #{exceptions.map { |e| e.to_s }.join(', ')}")
end