mirror of
https://github.com/dkam/openlib.git
synced 2025-12-28 03:04:53 +00:00
Initial import
This commit is contained in:
14
lib/openlib.rb
Normal file
14
lib/openlib.rb
Normal file
@@ -0,0 +1,14 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'openlib/version'
|
||||
require 'openlib/client'
|
||||
require 'openlib/book'
|
||||
|
||||
module Openlib
|
||||
ID_KINDS = %i[isbn oclc lccn olid].freeze
|
||||
|
||||
class Error < StandardError; end
|
||||
# Your code goes here...
|
||||
|
||||
|
||||
end
|
||||
54
lib/openlib/book.rb
Normal file
54
lib/openlib/book.rb
Normal file
@@ -0,0 +1,54 @@
|
||||
require 'json'
|
||||
|
||||
module Openlib
|
||||
class Book
|
||||
def initialize(id, id_kind: :isbn, user_agent: nil)
|
||||
@id = id
|
||||
@id_kind= id_kind.to_sym
|
||||
@client = Openlib::Client.new(user_agent: user_agent)
|
||||
@view = nil
|
||||
@data = nil
|
||||
end
|
||||
|
||||
##
|
||||
# Check that we got back the expected data
|
||||
def view
|
||||
@view ||= @client.get(id: @id, id_kind: @id_kind, format: 'viewapi')
|
||||
end
|
||||
|
||||
def data
|
||||
@data ||= @client.get(id: @id, id_kind: @id_kind, format: 'data')
|
||||
end
|
||||
|
||||
def view_data(req: )
|
||||
view.first.last.dig(req.to_s)
|
||||
end
|
||||
|
||||
def data_data(req:)
|
||||
case req
|
||||
when :authors, :publishers, :subjects then data.first.last.dig(req.to_s).map {|p| p.dig('name') }
|
||||
else
|
||||
data.first.last.dig(req.to_s)
|
||||
end
|
||||
end
|
||||
|
||||
def author
|
||||
authors
|
||||
end
|
||||
|
||||
def method_missing(m, *args, &block)
|
||||
case m
|
||||
when :info_url, :preview, :preview_url, :thumbnail_url
|
||||
then self.send('view_data', req: m)
|
||||
when :url, :authors, :identifiers, :classifications, :subjects,
|
||||
:subject_places, :subject_people, :subject_times, :publishers,
|
||||
:publish_places, :publish_date, :excerpts, :links, :cover, :ebooks,
|
||||
:number_of_pages, :weight, :title
|
||||
then self.send('data_data', req: m)
|
||||
else
|
||||
super
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
23
lib/openlib/client.rb
Normal file
23
lib/openlib/client.rb
Normal file
@@ -0,0 +1,23 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Openlib
|
||||
require 'open-uri'
|
||||
USER_AGENT = "Ruby OpenLib/#{Openlib::VERSION}"
|
||||
|
||||
class Client
|
||||
def initialize(user_agent: nil)
|
||||
@user_agent = user_agent || USER_AGENT
|
||||
end
|
||||
|
||||
def get(id:, format: 'data', id_kind: :isbn)
|
||||
raise ArgumetError, "Kind must be one of #{ID_KINDS}" unless ID_KINDS.include?(id_kind)
|
||||
|
||||
resp = URI.open( "https://openlibrary.org/api/books?jscmd=#{format}&format=json&bibkeys=#{id_kind.to_s.upcase}#{id}", 'User-Agent' => @user_agent )
|
||||
|
||||
byebug unless resp.status.first == '200'
|
||||
|
||||
JSON.parse(resp.read)
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
3
lib/openlib/version.rb
Normal file
3
lib/openlib/version.rb
Normal file
@@ -0,0 +1,3 @@
|
||||
module Openlib
|
||||
VERSION = "0.1.0"
|
||||
end
|
||||
Reference in New Issue
Block a user