class ISBNdb::ApiClient

Attributes

api_key[RW]

Public Class Methods

new(api_key: nil) click to toggle source
# File lib/isbndb/api_client.rb, line 8
def initialize(api_key: nil)
  raise ArgumentError, 'API key is required.' unless api_key.is_a?(String)
  @api_key = api_key
end
snakify(hash) click to toggle source
# File lib/isbndb/api_client.rb, line 43
def self.snakify(hash)
  if hash.is_a? Array
    hash.map{ |item| symbolize(item.to_snake_keys) }
  else
    symbolize(hash.to_snake_keys)
  end
end

Private Class Methods

symbolize(obj) click to toggle source
# File lib/isbndb/api_client.rb, line 61
def self.symbolize(obj)
  return obj.reduce({}) do |memo, (k, v)|
    memo.tap { |m| m[k.to_sym] = symbolize(v) }
  end if obj.is_a? Hash
  return obj.reduce([]) do |memo, v|
    memo << symbolize(v); memo
  end if obj.is_a? Array
  obj
end

Public Instance Methods

author() click to toggle source
# File lib/isbndb/api_client.rb, line 27
def author
  @author ||= ISBNdb::Api::Author.new(client: self)
end
book() click to toggle source
# File lib/isbndb/api_client.rb, line 31
def book
  @book ||= ISBNdb::Api::Book.new(client: self)
end
publisher() click to toggle source
# File lib/isbndb/api_client.rb, line 35
def publisher
  @publisher ||= ISBNdb::Api::Publisher.new(client: self)
end
request(page, params = {}) click to toggle source
# File lib/isbndb/api_client.rb, line 13
def request(page, params = {})
  response = self.class.get(URI.escape(page), query: params, headers: headers, timeout: 60)
  raise ISBNdb::RequestError.new "HTTP Response: #{response.code}" if response.code != 200
  begin
    self.class.snakify(response.parsed_response)
  rescue JSON::ParserError
    nil
  end
end
stats() click to toggle source
# File lib/isbndb/api_client.rb, line 23
def stats
  request('/stats')
end
subject() click to toggle source
# File lib/isbndb/api_client.rb, line 39
def subject
  @subject ||= ISBNdb::Api::Subject.new(client: self)
end

Private Instance Methods

headers() click to toggle source
# File lib/isbndb/api_client.rb, line 53
def headers
  {
    'Authorization' => api_key,
    'Content-Type' => 'application/json',
    'Accept' => '*/*'
  }
end