Class: S3Search::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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.



18
19
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
# File 'lib/s3search/client.rb', line 18

def initialize
  @url = ENV['S3SEARCH_URL'] || 'https://api.s3searchapp.com'
  @http = Faraday.new(:url => @url) 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.



16
17
18
# File 'lib/s3search/client.rb', line 16

def http
  @http
end

#loggerObject (readonly)

Returns the value of attribute logger.



16
17
18
# File 'lib/s3search/client.rb', line 16

def logger
  @logger
end

#urlObject (readonly)

Returns the value of attribute url.



16
17
18
# File 'lib/s3search/client.rb', line 16

def url
  @url
end

Instance Method Details

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



46
47
48
# File 'lib/s3search/client.rb', line 46

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

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



50
51
52
# File 'lib/s3search/client.rb', line 50

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

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



54
55
56
# File 'lib/s3search/client.rb', line 54

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

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



58
59
60
# File 'lib/s3search/client.rb', line 58

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

#request(method, path, params) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/s3search/client.rb', line 62

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