Class: S3Search::Client

Inherits:
Object
  • Object
show all
Includes:
API::Documents, API::Searches, API::Stats
Defined in:
lib/s3search/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from API::Stats

#query_count

Methods included from API::Searches

#search, #simple_search

Methods included from API::Documents

#create, #create_many, #delete, #get, #get_all, #update

Constructor Details

#initializeClient

Returns a new instance of Client.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/s3search/client.rb', line 20

def initialize
  %w{ S3SEARCH_URL S3SEARCH_API_KEY S3SEARCH_API_SECRET }.each do |var|
    raise "ENV['#{var}'] must be set" unless ENV[var]
  end

  @url = ENV['S3SEARCH_URL'] || 'https://us-east-1.s3search.io'
  @http = Faraday.new(url: @url, ssl: {verify: false}) do |builder|
    builder.response :mashify
    builder.response :json, :content_type => /\bjson$/
    builder.request :json
    builder.request :basic_auth, ENV['S3SEARCH_API_KEY'], ENV['S3SEARCH_API_SECRET']
    builder.options[:read_timeout] = 4
    builder.options[:open_timeout] = 2
    builder.adapter :excon
  end

  @http.headers = {
    accept:  'application/json',
    user_agent: "S3Search Ruby Gem #{S3Search::VERSION}",
    "Content-Type" => "application/json"
  }

  @logger = Logger.new(STDOUT)

  # 0=DEBUG, 1=INFO, 2=WARN, 3=ERROR, 4=FATAL
  @logger.level = ENV['S3SEARCH_LOG_LEVEL'].try(:to_i) || 2

  @logger.formatter = proc do |severity, datetime, progname, msg|
    "[S3Search][#{severity}]: #{msg}\n"
  end
end

Instance Attribute Details

#httpObject (readonly)

Returns the value of attribute http.



18
19
20
# File 'lib/s3search/client.rb', line 18

def http
  @http
end

#loggerObject (readonly)

Returns the value of attribute logger.



18
19
20
# File 'lib/s3search/client.rb', line 18

def logger
  @logger
end

#urlObject (readonly)

Returns the value of attribute url.



18
19
20
# File 'lib/s3search/client.rb', line 18

def url
  @url
end

Instance Method Details

#_delete(path, params = {}) ⇒ Object



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

def _delete path, params={}
 request :delete, path, params
end

#_get(path, params = {}) ⇒ Object



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

def _get path, params={}
 request :get, path, params
end

#_post(path, params = {}) ⇒ Object



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

def _post path, params={}
 request :post, path, params
end

#_put(path, params = {}) ⇒ Object



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

def _put path, params={}
 request :put, path, params
end

#request(method, path, params) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/s3search/client.rb', line 68

def request method, path, params
  response = http.send(method, path, params)

  case response.status
  when 200..299
    response.body
  when 404, 410
    raise RequestError::NotFound.new(response)
  when 401
    raise RequestError::Unauthorized.new(response)
  else
    raise RequestError.new(response)
  end
end