Class: ISBNdb::ApiClient

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/isbndb/api_client.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key: nil) ⇒ ApiClient

Returns a new instance of ApiClient.

Raises:

  • (ArgumentError)


8
9
10
11
# 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

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



6
7
8
# File 'lib/isbndb/api_client.rb', line 6

def api_key
  @api_key
end

Class Method Details

.snakify(hash) ⇒ Object



43
44
45
46
47
48
49
# 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

Instance Method Details

#authorObject



27
28
29
# File 'lib/isbndb/api_client.rb', line 27

def author
  @author ||= ISBNdb::Api::Author.new(client: self)
end

#bookObject



31
32
33
# File 'lib/isbndb/api_client.rb', line 31

def book
  @book ||= ISBNdb::Api::Book.new(client: self)
end

#publisherObject



35
36
37
# File 'lib/isbndb/api_client.rb', line 35

def publisher
  @publisher ||= ISBNdb::Api::Publisher.new(client: self)
end

#request(page, params = {}) ⇒ Object



13
14
15
16
17
18
19
20
21
# File 'lib/isbndb/api_client.rb', line 13

def request(page, params = {})
  response = self.class.get(Addressable::URI.encode(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

#statsObject



23
24
25
# File 'lib/isbndb/api_client.rb', line 23

def stats
  request('/stats')
end

#subjectObject



39
40
41
# File 'lib/isbndb/api_client.rb', line 39

def subject
  @subject ||= ISBNdb::Api::Subject.new(client: self)
end