class Reactio::APIClient

Public Class Methods

build(api_key, organization) click to toggle source
# File lib/reactio/api_client.rb, line 10
def build(api_key, organization)
  raise ArgumentError, 'api_key is required' unless api_key
  raise ArgumentError, 'organization is required' unless organization
  http_client = build_http_client(
    api_key,
    APIEndpoint.new(organization)
  )
  new(http_client)
end
build_http_client(api_key, api_endpoint) click to toggle source
# File lib/reactio/api_client.rb, line 20
def build_http_client(api_key, api_endpoint)
  Faraday.new(url: api_endpoint.base_url) do |faraday|
    faraday.use :reactio, api_key
    faraday.request :url_encoded
    faraday.adapter Faraday.default_adapter
  end
end
new(http_client) click to toggle source
# File lib/reactio/api_client.rb, line 29
def initialize(http_client)
  @http = http_client
end

Public Instance Methods

request(method, path, env = {}) click to toggle source
# File lib/reactio/api_client.rb, line 33
def request(method, path, env = {})
  response = @http.send(method, path, env[:body])
  handle_api_response(response)
end

Private Instance Methods

handle_api_response(res) click to toggle source
# File lib/reactio/api_client.rb, line 40
def handle_api_response(res)
  case res.status
  when 200..299
    res.body
  when 401
    raise Reactio::AuthenticationError, res.body.inspect
  when 400..499
    raise Reactio::BadRequest, res.body.inspect
  when 500..599
    raise Reactio::ServerError, res.body.inspect
  end
end