class Opdb::Client

Client wrapping the Open Pinball Database API

Constants

API_BASE_URI

Attributes

api_token[R]

Public Class Methods

new(api_token: nil) click to toggle source
# File lib/opdb/client.rb, line 18
def initialize(api_token: nil)
  @api_token = api_token
end

Public Instance Methods

changelog() click to toggle source

Public Requests

# File lib/opdb/client.rb, line 23
def changelog
  request(
    http_method: :get,
    endpoint: "changelog"
  )
end
export_machine_groups() click to toggle source
# File lib/opdb/client.rb, line 78
def export_machine_groups
  private_request(
    http_method: :get,
    endpoint: "export/groups"
  )
end
export_machines() click to toggle source
# File lib/opdb/client.rb, line 71
def export_machines
  private_request(
    http_method: :get,
    endpoint: "export"
  )
end
get_machine_info(opdb_id:) click to toggle source
# File lib/opdb/client.rb, line 57
def get_machine_info(opdb_id:)
  private_request(
    http_method: :get,
    endpoint: "machines/#{opdb_id}"
  )
end
get_machine_info_by_ipdb_id(ipdb_id:) click to toggle source
# File lib/opdb/client.rb, line 64
def get_machine_info_by_ipdb_id(ipdb_id:)
  private_request(
    http_method: :get,
    endpoint: "machines/ipdb/#{ipdb_id}"
  )
end
search_machines(q:, require_opdb: "1", include_groups: "0", include_aliases: "1", include_grouping_entries: "0") click to toggle source

Requests that require a valid api_token

# File lib/opdb/client.rb, line 43
def search_machines(q:, require_opdb: "1", include_groups: "0", include_aliases: "1", include_grouping_entries: "0") # rubocop:disable Naming/MethodParameterName, Metrics/MethodLength
  private_request(
    http_method: :get,
    endpoint: "search",
    params: {
      q: q,
      require_opdb: require_opdb,
      include_groups: include_groups,
      include_aliases: include_aliases,
      include_grouping_entries: include_grouping_entries
    }
  )
end

Private Instance Methods

client() click to toggle source
# File lib/opdb/client.rb, line 87
def client
  @client ||= Faraday.new(API_BASE_URI) do |client|
    client.response :json
    client.headers["Accept"] = "application/json"
    client.headers["User-Agent"] = "opdb ruby client #{Opdb::VERSION}"
  end
end
error_class(response_status) click to toggle source
# File lib/opdb/client.rb, line 116
def error_class(response_status) # rubocop:disable Metrics/MethodLength
  case response_status
  when HTTP_BAD_REQUEST_CODE
    BadRequestError
  when HTTP_UNAUTHORIZED_CODE
    UnauthorizedError
  when HTTP_FORBIDDEN_CODE
    ForbiddenError
  when HTTP_NOT_FOUND_CODE
    NotFoundError
  when HTTP_UNPROCESSABLE_ENTITY_CODE
    UnprocessableEntityError
  else
    ApiError
  end
end
private_request(http_method:, endpoint:, params: {}) click to toggle source
# File lib/opdb/client.rb, line 103
def private_request(http_method:, endpoint:, params: {})
  raise "Api key not set" unless @api_token

  params.merge!(
    { api_token: api_token }
  )
  request(
    http_method: http_method,
    endpoint: endpoint,
    params: params
  )
end
request(http_method:, endpoint:, params: {}) click to toggle source
# File lib/opdb/client.rb, line 95
def request(http_method:, endpoint:, params: {})
  response = client.public_send(http_method, endpoint, params)

  return response.body if response.status == HTTP_OK_CODE

  raise error_class(response.status), response.body.to_json
end