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 Curb (Curl) 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
# 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]
  @gzip           = data[:gzip].nil? ? true : data[:gzip]
  @hosts          = (data[:hosts] || 1.upto(3).map { |i| "#{@application_id}-#{i}.algolia.io" }).shuffle
  @debug          = data[:debug]
end

Instance Attribute Details

#api_keyObject (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_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

#hostsObject (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



76
77
78
# File 'lib/algolia/client.rb', line 76

def delete(uri)
  request(uri, :DELETE)
end

#get(uri) ⇒ Object



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

def get(uri)
  request(uri, :GET)
end

#post(uri, body) ⇒ Object



68
69
70
# File 'lib/algolia/client.rb', line 68

def post(uri, body)
  request(uri, :POST, body)
end

#put(uri, body) ⇒ Object



72
73
74
# File 'lib/algolia/client.rb', line 72

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.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/algolia/client.rb', line 29

def request(uri, method, data = nil)
  exceptions = []
  thread_local_hosts.each do |host|
    begin
      session = host["session"]
      session.url = host["base_url"] + uri
      case method
      when :GET
        session.http_get
      when :POST
        session.post_body = data
        session.http_post
      when :PUT
        session.put(data)
      when :DELETE
        session.http_delete
      end
      if session.response_code >= 400 || session.response_code < 200
        raise AlgoliaProtocolError.new(session.response_code, "Cannot #{method} to #{session.url}: #{session.body_str} (#{session.response_code})")
      end
	  if @gzip
  gz = Zlib::GzipReader.new(StringIO.new(session.body_str))
  return JSON.parse(gz.read)
	  end
      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