class Panoptes::Endpoints::BaseEndpoint

Attributes

auth[R]
params[R]
prefix[R]
url[R]

Public Class Methods

new(auth: {}, url: nil, prefix: nil, params: nil, &config) click to toggle source

@param auth [Hash<token: String, client_id: String, client_secret: String>] Authentication details

* either nothing,
* a hash with +:token+ (an existing OAuth user token),
* or a hash with +:client_id+ and +:client_secret+
  (a keypair for an OAuth Application).

@param url [String] API location to use. @param prefix [String] An optional API url prefix @yield Allows an optional block to configure the faraday connection @yieldparam faraday [Faraday::Connection] The faraday connection

# File lib/panoptes/endpoints/base_endpoint.rb, line 21
def initialize(auth: {}, url: nil, prefix: nil, params: nil, &config)
  @auth = auth
  @url = url
  @prefix = prefix
  @config = config
  @params = params
end

Public Instance Methods

connection() click to toggle source
# File lib/panoptes/endpoints/base_endpoint.rb, line 29
def connection
  @connection ||= Faraday.new(url) do |faraday|
    auth_request faraday, auth
    configure faraday
  end
end
delete(path, query = {}, etag: nil) click to toggle source
# File lib/panoptes/endpoints/base_endpoint.rb, line 52
def delete(path, query = {}, etag: nil)
  request :delete, path, query, etag_header(etag)
end
etag_header(etag) click to toggle source
# File lib/panoptes/endpoints/base_endpoint.rb, line 56
def etag_header(etag)
  {}.tap do |headers|
    headers['If-Match'] = etag if etag
  end
end
get(path, query = {}) click to toggle source
# File lib/panoptes/endpoints/base_endpoint.rb, line 36
def get(path, query = {})
  request :get, path, query
end
handle_response(response) click to toggle source
# File lib/panoptes/endpoints/base_endpoint.rb, line 70
def handle_response(response)
  case response.status
  when 404
    raise Panoptes::Client::ResourceNotFound, status: response.status, body: response.body
  when 400..600
    raise Panoptes::Client::ServerError.new, response.body
  else
    response.body
  end
end
patch(path, body = {}, etag: nil) click to toggle source
# File lib/panoptes/endpoints/base_endpoint.rb, line 48
def patch(path, body = {}, etag: nil)
  request :patch, path, body, etag_header(etag)
end
post(path, body = {}) click to toggle source
# File lib/panoptes/endpoints/base_endpoint.rb, line 40
def post(path, body = {})
  request :post, path, body
end
put(path, body = {}, etag: nil) click to toggle source
# File lib/panoptes/endpoints/base_endpoint.rb, line 44
def put(path, body = {}, etag: nil)
  request :put, path, body, etag_header(etag)
end
request(method, path, *args) click to toggle source
# File lib/panoptes/endpoints/base_endpoint.rb, line 62
def request(method, path, *args)
  if prefix
    sep = path[0] == '/' ? nil : '/'
    path = "#{prefix}#{sep}#{path}"
  end
  handle_response connection.send(method, path, *args)
end

Protected Instance Methods

auth_request(faraday, auth) click to toggle source
# File lib/panoptes/endpoints/base_endpoint.rb, line 95
def auth_request(faraday, auth)
  if auth[:token]
    faraday.request :panoptes_access_token,
                    url: url,
                    access_token: auth[:token]
  elsif auth[:client_id] && auth[:client_secret]
    faraday.request :panoptes_client_credentials,
                    url: url,
                    client_id: auth[:client_id],
                    client_secret: auth[:client_secret]
  end
end
configure(faraday) click to toggle source
# File lib/panoptes/endpoints/base_endpoint.rb, line 83
def configure(faraday)
  if @config
    @config.call faraday
  else
    faraday.request :panoptes_api_v1
    faraday.request :json
    faraday.response :json
    faraday.adapter Faraday.default_adapter
    faraday.params = @params if @params
  end
end