class Hipflag::Client

Constants

BASE_URL
NotFound
ServerError
TIMEOUT
Unauthorized
UnprocessableEntity

Public Class Methods

new(options = {}) click to toggle source
# File lib/hipflag/client.rb, line 17
def initialize(options = {})
  Configurable::OPTIONS.each do |key|
    instance_variable_set(:"@#{key}", options[key] || Hipflag.send(key))
  end

  http_client.base_url = BASE_URL
end

Private Instance Methods

handle_errors() { || ... } click to toggle source
# File lib/hipflag/client.rb, line 44
def handle_errors
  yield.tap do |response|
    if response.unauthorized?
      raise Unauthorized, 'Unauthorized'
    elsif response.unprocessable?
      raise UnprocessableEntity, response.json
    elsif response.server_error?
      raise ServerError, 'Server is not available'
    elsif response.not_found?
      raise NotFound, 'Resource not found'
    end
http_client() click to toggle source
# File lib/hipflag/client.rb, line 27
def http_client
  @http_client ||= Patron::Session.new do |config|
    config.timeout = TIMEOUT
    config.headers = {
      'Content-Type' => 'application/json',
      'X-Auth-Public' => @public_key,
      'X-Auth-Secret' => @secret_key
    }
  end
end
perform_request(method, url, params = {}) click to toggle source
# File lib/hipflag/client.rb, line 38
def perform_request(method, url, params = {})
  handle_errors do
    Response.new(http_client.request(method, url, {}, params))
  end
end