class Rbvore::API

Constants

BASE_ENDPOINT
DEFAULT_API_KEY
DEFAULT_HOST

Public Class Methods

endpoint(*items) click to toggle source
# File lib/rbvore/api.rb, line 88
def self.endpoint(*items)
  ([BASE_ENDPOINT] + items.map { |item|
    case
    when Rbvore.resource_subclass?(item)
      item.pluralize
    when item.nil?
      nil
    else
      item.to_s
    end
  }.compact).join("/")
end
new(host = DEFAULT_HOST) click to toggle source
# File lib/rbvore/api.rb, line 50
def initialize(host = DEFAULT_HOST)
  @host = host
end

Public Instance Methods

fetch_all(endpoint, resource_class, params: {}, api_key: nil) click to toggle source
# File lib/rbvore/api.rb, line 64
def fetch_all(endpoint, resource_class, params: {}, api_key: nil)
  response = request(
    :get,
    endpoint,
    params: params,
    api_key: api_key,
  )
  raise response.error unless response.success?

  Resource.parse_collection(response.json_body, resource_class)
end
fetch_one(endpoint, resource_class, params: {}, api_key: nil) click to toggle source
# File lib/rbvore/api.rb, line 76
def fetch_one(endpoint, resource_class, params: {}, api_key: nil)
  response = request(
    :get,
    endpoint,
    params: params,
    api_key: api_key,
  )
  raise response.error unless response.success?

  Resource.parse_object(response.json_body, resource_class)
end
request(method, endpoint, body: nil, params: {}, headers: {}, api_key: nil) click to toggle source
# File lib/rbvore/api.rb, line 54
def request(method, endpoint, body: nil, params: {}, headers: {}, api_key: nil) # rubocop:disable Metrics/ParameterLists
  api_key ||= DEFAULT_API_KEY
  uri = build_uri(endpoint, params)
  http_start(uri) { |http|
    http.request(
      build_request(method, uri, body: body, headers: headers, api_key: api_key),
    )
  }
end

Private Instance Methods

build_request(method, uri, body: nil, headers: {}, api_key: nil) click to toggle source
# File lib/rbvore/api.rb, line 121
def build_request(method, uri, body: nil, headers: {}, api_key: nil)
  method_class(method).new(uri.request_uri).tap { |r|
    headers.each do |key, value|
      r.add_field(key, value)
    end
    r.add_field "Accept", "application/json"
    r.add_field "Api-Key", api_key unless api_key.nil?
    set_body(r, body)
  }
end
build_uri(endpoint, params = nil) click to toggle source
# File lib/rbvore/api.rb, line 103
def build_uri(endpoint, params = nil)
  url = if endpoint.start_with? "http"
          endpoint
        else
          "https://#{@host}#{endpoint}"
        end

  URI.parse(url).tap { |uri|
    next if params.nil? || params.empty?

    uri.query = encode_form_data(params)
  }
end
encode_form_data(body) click to toggle source
# File lib/rbvore/api.rb, line 117
def encode_form_data(body)
  body.map { |key, value| "#{key}=#{CGI.escape(value)}" }.join("&")
end
http_opts() click to toggle source
# File lib/rbvore/api.rb, line 136
def http_opts
  { use_ssl: true }
end
http_start(uri, &block) click to toggle source
# File lib/rbvore/api.rb, line 132
def http_start(uri, &block)
  Response.new(Net::HTTP.start(uri.host, uri.port, http_opts, &block))
end
method_class(name) click to toggle source
# File lib/rbvore/api.rb, line 149
def method_class(name)
  case name.to_sym
  when :get
    Net::HTTP::Get
  when :post
    Net::HTTP::Post
  end
end
set_body(request, body) click to toggle source
# File lib/rbvore/api.rb, line 140
def set_body(request, body)
  if body.is_a? String
    request.body = body
  elsif !body.nil?
    request.add_field "Content-Type", "application/json"
    request.body = body.to_json
  end
end