class Freefeed::Client

Constants

HOST

Attributes

api_token[RW]
faraday_options[RW]
resources[RW]

Public Class Methods

new(api_token = nil, faraday_options = {}) click to toggle source
# File lib/freefeed/client.rb, line 7
def initialize(api_token = nil, faraday_options = {})
  @api_token = api_token
  @faraday_options = faraday_options
end

Public Instance Methods

authenticate(username:, password:) click to toggle source
# File lib/freefeed/client.rb, line 12
def authenticate(username:, password:)
  Session.create(self, username: username, password: password).tap do |re|
    @api_token = re.fetch('authToken')
  end
end
call!(verb, endpoint, params = {}, headers = {}) click to toggle source
# File lib/freefeed/client.rb, line 18
def call!(verb, endpoint, params = {}, headers = {})
  response = conn.send(
    verb, endpoint, params, auth_header.merge(headers)
  )
  handle_erros(response)
  prepare_response(response)
end

Private Instance Methods

auth_header() click to toggle source
# File lib/freefeed/client.rb, line 54
def auth_header
  api_token.nil? ? {} : { 'x-authentication-token': api_token }
end
conn() click to toggle source
# File lib/freefeed/client.rb, line 28
def conn
  @conn ||= Faraday.new(url: "https://#{HOST}") do |faraday|
    faraday.request :multipart
    faraday.response(
      :logger,
      (faraday_options[:logger] || Logger.new(STDOUT)),
      (faraday_options[:logger_options] || { bodies: true, headers: true })
    )
    faraday.request :json
    faraday.adapter faraday_options[:adapter] || Faraday.default_adapter
  end
end
handle_erros(response) click to toggle source
# File lib/freefeed/client.rb, line 41
def handle_erros(response)
  raise(
    Exceptions::ResponseError.new(response),
    'Freefeed API has returned the error'
  ) unless response.status == 200
end
prepare_response(response) click to toggle source
# File lib/freefeed/client.rb, line 48
def prepare_response(response)
  MultiJson.load(response.body.to_s)
rescue MultiJson::LoadError
  response.body
end