Module: Alexandrite::GoogleAPI

Includes:
Helpers::Format, Helpers::Validation
Included in:
Google
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

Instance Method Summary collapse

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

#isbnObject (readonly)

Returns the value of attribute isbn.



18
19
20
# File 'lib/alexandrite_google.rb', line 18

def isbn
  @isbn
end

#resultObject (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

Parameters:

  • query (String)

Returns:

  • (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

Parameters:

  • key (Symbol)
  • query (String)
  • fields (Array<String>)

Returns:

  • (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