class S3Search::Client

Attributes

http[R]
logger[R]
url[R]

Public Class Methods

new() click to toggle source
# 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

Public Instance Methods

_delete(path, params={}) click to toggle source
# File lib/s3search/client.rb, line 52
def _delete path, params={}
 request :delete, path, params
end
_get(path, params={}) click to toggle source
# File lib/s3search/client.rb, line 56
def _get path, params={}
 request :get, path, params
end
_post(path, params={}) click to toggle source
# File lib/s3search/client.rb, line 60
def _post path, params={}
 request :post, path, params
end
_put(path, params={}) click to toggle source
# File lib/s3search/client.rb, line 64
def _put path, params={}
 request :put, path, params
end
request(method, path, params) click to toggle source
# 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