Class: ISBNdb::ApiClient
- Inherits:
-
Object
- Object
- ISBNdb::ApiClient
- Includes:
- HTTParty
- Defined in:
- lib/isbndb/api_client.rb
Instance Attribute Summary collapse
-
#api_key ⇒ Object
Returns the value of attribute api_key.
Class Method Summary collapse
Instance Method Summary collapse
- #author ⇒ Object
- #book ⇒ Object
-
#initialize(api_key: nil) ⇒ ApiClient
constructor
A new instance of ApiClient.
- #publisher ⇒ Object
- #request(page, params = {}) ⇒ Object
- #stats ⇒ Object
- #subject ⇒ Object
Constructor Details
#initialize(api_key: nil) ⇒ ApiClient
Returns a new instance of ApiClient.
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_key ⇒ Object
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
#author ⇒ Object
27 28 29 |
# File 'lib/isbndb/api_client.rb', line 27 def @author ||= ISBNdb::Api::Author.new(client: self) end |
#book ⇒ Object
31 32 33 |
# File 'lib/isbndb/api_client.rb', line 31 def book @book ||= ISBNdb::Api::Book.new(client: self) end |
#publisher ⇒ Object
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 |
#stats ⇒ Object
23 24 25 |
# File 'lib/isbndb/api_client.rb', line 23 def stats request('/stats') end |