Sha256: b08f2c3374a97c6df51308450c6c440efc5a753e42fdb39b55628843a2e127f2

Contents?: true

Size: 960 Bytes

Versions: 2

Compression:

Stored size: 960 Bytes

Contents

module RelatonIsbn
  #
  # Search ISBN in Openlibrary.
  #
  module OpenLibrary
    extend self

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

    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

    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
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
relaton-isbn-1.20.0 lib/relaton_isbn/open_library.rb
relaton-isbn-1.19.0 lib/relaton_isbn/open_library.rb