Module: Alexandrite::GoogleAPI
- Includes:
- Helpers::Format, Helpers::Validation
- Included in:
- Defined in:
- lib/alexandrite_google.rb
Overview
Methods for consuming Google API
Constant Summary collapse
- BASE_URL =
'https://www.googleapis.com/books/v1/volumes?q='
- FILTERS =
{ author: 'inauthor:', isbn: 'isbn:', lccn: 'lccn:', oclc: 'oclc:', publisher: 'inpublisher:', subject: 'subject:', title: 'intitle:' }.freeze
- FIELDS =
{ title: 'volumeInfo/title', authors: 'volumeInfo/authors' }.freeze
Instance Attribute Summary collapse
-
#isbn ⇒ Object
readonly
Returns the value of attribute isbn.
-
#result ⇒ Object
readonly
Returns the value of attribute result.
Instance Method Summary collapse
- #search(query, projection: 'lite') ⇒ Faraday::Response
- #search_by(key, query, projection: 'lite', **fields) ⇒ Faraday::Response
Methods included from Helpers::Validation
#define_isbn_type, #valid_isbn_length?, #validate_isbn
Methods included from Helpers::Format
#add_fields, #remove_non_digits
Instance Attribute Details
#isbn ⇒ Object (readonly)
Returns the value of attribute isbn.
18 19 20 |
# File 'lib/alexandrite_google.rb', line 18 def isbn @isbn end |
#result ⇒ Object (readonly)
Returns the value of attribute result.
18 19 20 |
# File 'lib/alexandrite_google.rb', line 18 def result @result end |
Instance Method Details
#search(query, projection: 'lite') ⇒ Faraday::Response
39 40 41 42 43 44 45 |
# File 'lib/alexandrite_google.rb', line 39 def search(query, projection: 'lite') conn = Faraday.new("#{BASE_URL}#{query}&projection=#{projection}") do |r| r.response :json end conn.get.body end |
#search_by(key, query, projection: 'lite', **fields) ⇒ Faraday::Response
51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/alexandrite_google.rb', line 51 def search_by(key, query, projection: 'lite', **fields) projection = "&projection=#{projection}" url = fields.any? ? "#{BASE_URL}#{FILTERS[key]}#{query}#{add_fields(fields)}#{projection}" : "#{BASE_URL}#{FILTERS[key]}#{query}" conn = Faraday.new(url) do |r| r.response :json end response = conn.get.body process_response(response) end |