class FavroApi::Request

Constants

API_URL
ENDPOINTS

Attributes

connection[RW]
endpoint[RW]
last_response[RW]
method[RW]
page[RW]
params[RW]
url[RW]

Public Class Methods

fetch(endpoint, page: nil, last_response: nil, params: {}) click to toggle source
# File lib/favro_api/request.rb, line 51
def fetch(endpoint, page: nil, last_response: nil, params: {})
  new(
    endpoint:      endpoint,
    last_response: last_response,
    page:          page,
    method:        :get,
    params:        params
  ).fetch
end
new(options = {}) click to toggle source
# File lib/favro_api/request.rb, line 23
def initialize(options = {})
  self.url           = options[:url]
  self.endpoint      = options[:endpoint]
  self.method        = options[:method] || :get
  self.params        = options[:params] || {}
  self.last_response = options[:last_response]
  self.page          = options[:page]
end

Public Instance Methods

fetch() click to toggle source
# File lib/favro_api/request.rb, line 32
def fetch
  self.connection = Faraday.new("#{uri.scheme}://#{uri.hostname}") do |faraday|
    faraday.adapter :net_http
    faraday.basic_auth(FavroApi.auth.email, FavroApi.auth.token)
    faraday.headers['organizationId'] = params.delete(:organization_id)
    faraday.params['page']      = page || (last_response ? last_response.page + 1 : 0)
    faraday.params['requestId'] = last_response&.request_id
  end

  response = Response.new(response: connection.send(method, uri.request_uri, params))

  if response.error?
    raise ApiError, "Got API error. Code: #{response.status}, message: #{response.body}"
  end

  response
end

Private Instance Methods

api_url_for(endpoint) click to toggle source
# File lib/favro_api/request.rb, line 68
def api_url_for(endpoint)
  path = ENDPOINTS[endpoint] || raise(ApiError, "Unknown endpoint #{endpoint}")
  "#{API_URL}#{path}"
end
uri() click to toggle source
# File lib/favro_api/request.rb, line 73
def uri
  URI(url)
end