Module: RelatonIsbn::OpenLibrary

Extended by:
OpenLibrary
Included in:
OpenLibrary
Defined in:
lib/relaton_isbn/open_library.rb

Overview

Search ISBN in Openlibrary.

Constant Summary collapse

ENDPOINT =
"http://openlibrary.org/api/volumes/brief/isbn/".freeze

Instance Method Summary collapse

Instance Method Details

#get(ref, _date = nil, _opts = {}) ⇒ Object

rubocop:disable Metrics/MethodLength



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/relaton_isbn/open_library.rb', line 10

def get(ref, _date = nil, _opts = {}) # rubocop:disable Metrics/MethodLength
  Util.info "Fetching from OpenLibrary ...", key: ref

  isbn = Isbn.new(ref).parse
  unless isbn
    Util.info "Incorrect ISBN.", key: ref
    return
  end

  resp = request_api isbn
  unless resp
    Util.info "Not found.", key: ref
    return
  end

  bib = Parser.parse resp
  Util.info "Found: `#{bib.docidentifier.first.id}`", key: ref
  bib
end

#request_api(isbn) ⇒ Object



30
31
32
33
34
35
36
37
38
39
# File 'lib/relaton_isbn/open_library.rb', line 30

def request_api(isbn)
  uri = URI "#{ENDPOINT}#{isbn}.json"
  response = Net::HTTP.get_response uri
  return unless response.is_a? Net::HTTPSuccess

  data = JSON.parse response.body
  return unless data["records"]&.any?

  data["records"].first.last
end