mirror of
https://github.com/dkam/openlib.git
synced 2025-12-28 03:04:53 +00:00
Remove debugging and fix test. Rubocop run
This commit is contained in:
@@ -9,6 +9,4 @@ module Openlib
|
||||
|
||||
class Error < StandardError; end
|
||||
# Your code goes here...
|
||||
|
||||
|
||||
end
|
||||
|
||||
@@ -1,15 +1,22 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'json'
|
||||
|
||||
module Openlib
|
||||
class Book
|
||||
def initialize(id, id_kind: :isbn, user_agent: nil)
|
||||
attr_writer :view, :data
|
||||
def initialize(id:, id_kind: :isbn, user_agent: nil)
|
||||
@id = id
|
||||
@id_kind= id_kind.to_sym
|
||||
@id_kind = id_kind.to_sym
|
||||
@client = Openlib::Client.new(user_agent: user_agent)
|
||||
@view = nil
|
||||
@data = nil
|
||||
end
|
||||
|
||||
def cache_clear
|
||||
@view = @data = nil
|
||||
end
|
||||
|
||||
##
|
||||
# Check that we got back the expected data
|
||||
def view
|
||||
@@ -20,13 +27,13 @@ module Openlib
|
||||
@data ||= @client.get(id: @id, id_kind: @id_kind, format: 'data')
|
||||
end
|
||||
|
||||
def view_data(req: )
|
||||
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') }
|
||||
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
|
||||
@@ -39,16 +46,15 @@ module Openlib
|
||||
def method_missing(m, *args, &block)
|
||||
case m
|
||||
when :info_url, :preview, :preview_url, :thumbnail_url
|
||||
then self.send('view_data', req: m)
|
||||
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)
|
||||
send('data_data', req: m)
|
||||
else
|
||||
super
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
@@ -10,14 +10,17 @@ module Openlib
|
||||
end
|
||||
|
||||
def get(id:, format: 'data', id_kind: :isbn)
|
||||
raise ArgumetError, "Kind must be one of #{ID_KINDS}" unless ID_KINDS.include?(id_kind)
|
||||
unless ID_KINDS.include?(id_kind)
|
||||
raise ArgumetError, "Kind must be one of #{ID_KINDS}"
|
||||
end
|
||||
|
||||
resp = URI.open( "https://openlibrary.org/api/books?jscmd=#{format}&format=json&bibkeys=#{id_kind.to_s.upcase}#{id}", 'User-Agent' => @user_agent )
|
||||
url = "https://openlibrary.org/api/books?jscmd=#{format}&format=json&bibkeys=#{id_kind.to_s.upcase}:#{id}"
|
||||
|
||||
byebug unless resp.status.first == '200'
|
||||
resp = URI.open(url, 'User-Agent' => @user_agent)
|
||||
|
||||
puts resp.status.first.to_s unless resp.status.first == '200'
|
||||
|
||||
JSON.parse(resp.read)
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,90 +0,0 @@
|
||||
TESTCASES= [
|
||||
isbn: '9780316030571',
|
||||
data: {
|
||||
"ISBN:9780316030571": {
|
||||
"authors": [
|
||||
{
|
||||
"name": "Iain M. Banks",
|
||||
"url": "https://openlibrary.org/authors/OL6924809A/Iain_M._Banks"
|
||||
}
|
||||
],
|
||||
"cover": {
|
||||
"large": "https://covers.openlibrary.org/b/id/2379038-L.jpg",
|
||||
"medium": "https://covers.openlibrary.org/b/id/2379038-M.jpg",
|
||||
"small": "https://covers.openlibrary.org/b/id/2379038-S.jpg"
|
||||
},
|
||||
"ebooks": [
|
||||
{
|
||||
"availability": "borrow",
|
||||
"borrow_url": "https://openlibrary.org/books/OL10426208M/Use_of_Weapons/borrow",
|
||||
"checkedout": false,
|
||||
"formats": {},
|
||||
"preview_url": "https://archive.org/details/useofweapons00bank_0"
|
||||
}
|
||||
],
|
||||
"identifiers": {
|
||||
"goodreads": [
|
||||
"3395563"
|
||||
],
|
||||
"isbn_10": [
|
||||
"0316030570"
|
||||
],
|
||||
"isbn_13": [
|
||||
"9780316030571"
|
||||
],
|
||||
"librarything": [
|
||||
"141"
|
||||
],
|
||||
"openlibrary": [
|
||||
"OL10426208M"
|
||||
]
|
||||
},
|
||||
"key": "/books/OL10426208M",
|
||||
"number_of_pages": 480,
|
||||
"publish_date": "July 28, 2008",
|
||||
"publishers": [
|
||||
{
|
||||
"name": "Orbit"
|
||||
}
|
||||
],
|
||||
"subject_people": [
|
||||
{
|
||||
"name": "Cheradenine Zakalwe",
|
||||
"url": "https://openlibrary.org/subjects/person:cheradenine_zakalwe"
|
||||
},
|
||||
{
|
||||
"name": "Diziet Sma",
|
||||
"url": "https://openlibrary.org/subjects/person:diziet_sma"
|
||||
},
|
||||
{
|
||||
"name": "Skaffen-Amtiskaw",
|
||||
"url": "https://openlibrary.org/subjects/person:skaffen-amtiskaw"
|
||||
}
|
||||
],
|
||||
"subjects": [
|
||||
{
|
||||
"name": "Fiction",
|
||||
"url": "https://openlibrary.org/subjects/fiction"
|
||||
},
|
||||
{
|
||||
"name": "Science Fiction",
|
||||
"url": "https://openlibrary.org/subjects/science_fiction"
|
||||
},
|
||||
{
|
||||
"name": "Science Fiction & Fantasy",
|
||||
"url": "https://openlibrary.org/subjects/science_fiction_&_fantasy"
|
||||
},
|
||||
{
|
||||
"name": "Long Now Manual for Civilization",
|
||||
"url": "https://openlibrary.org/subjects/long_now_manual_for_civilization"
|
||||
},
|
||||
{
|
||||
"name": "Space warfare",
|
||||
"url": "https://openlibrary.org/subjects/space_warfare"
|
||||
}
|
||||
],
|
||||
"title": "Use of Weapons",
|
||||
"url": "https://openlibrary.org/books/OL10426208M/Use_of_Weapons"
|
||||
}
|
||||
}
|
||||
]
|
||||
@@ -1,4 +1,6 @@
|
||||
require "test_helper"
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'test_helper'
|
||||
require 'byebug'
|
||||
|
||||
class OpenlibTest < Minitest::Test
|
||||
@@ -7,21 +9,19 @@ class OpenlibTest < Minitest::Test
|
||||
end
|
||||
|
||||
def test_it_does_something_useful
|
||||
|
||||
TESTCASES.each do |tc|
|
||||
b = Openlib::Book.new(id: tc.dig(:isbn))
|
||||
b.view = tc.dig(:view)
|
||||
b.data = tc.dig(:data)
|
||||
|
||||
assert_equal tc.dig(:data).first.last.dig('authors').map {|e| e.dig('name') }, b.authors
|
||||
assert_equal tc.dig(:data).first.last.dig('title').map {|e| e.dig('name') }, b.title
|
||||
assert_equal tc.dig(:data).first.last.dig('authors').map { |e| e.dig('name') }, b.authors
|
||||
assert_equal tc.dig(:data).first.last.dig('title'), b.title
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
TESTCASES= [
|
||||
TESTCASES = [
|
||||
isbn: '9780316030571',
|
||||
view: {"ISBN:9780316030571"=>{"bib_key"=>"ISBN:9780316030571", "preview"=>"borrow", "thumbnail_url"=>"https://covers.openlibrary.org/b/id/2379038-S.jpg", "preview_url"=>"https://archive.org/details/useofweapons00bank_0", "info_url"=>"https://openlibrary.org/books/OL10426208M/Use_of_Weapons"}},
|
||||
data: {"ISBN:9780316030571"=>{"publishers"=>[{"name"=>"Orbit"}], "identifiers"=>{"isbn_13"=>["9780316030571"], "openlibrary"=>["OL10426208M"], "isbn_10"=>["0316030570"], "librarything"=>["141"], "goodreads"=>["3395563"]}, "title"=>"Use of Weapons", "url"=>"https://openlibrary.org/books/OL10426208M/Use_of_Weapons", "number_of_pages"=>480, "cover"=>{"small"=>"https://covers.openlibrary.org/b/id/2379038-S.jpg", "large"=>"https://covers.openlibrary.org/b/id/2379038-L.jpg", "medium"=>"https://covers.openlibrary.org/b/id/2379038-M.jpg"}, "subjects"=>[{"url"=>"https://openlibrary.org/subjects/fiction", "name"=>"Fiction"}, {"url"=>"https://openlibrary.org/subjects/science_fiction", "name"=>"Science Fiction"}, {"url"=>"https://openlibrary.org/subjects/science_fiction_&_fantasy", "name"=>"Science Fiction & Fantasy"}, {"url"=>"https://openlibrary.org/subjects/long_now_manual_for_civilization", "name"=>"Long Now Manual for Civilization"}, {"url"=>"https://openlibrary.org/subjects/space_warfare", "name"=>"Space warfare"}], "subject_people"=>[{"url"=>"https://openlibrary.org/subjects/person:cheradenine_zakalwe", "name"=>"Cheradenine Zakalwe"}, {"url"=>"https://openlibrary.org/subjects/person:diziet_sma", "name"=>"Diziet Sma"}, {"url"=>"https://openlibrary.org/subjects/person:skaffen-amtiskaw", "name"=>"Skaffen-Amtiskaw"}], "key"=>"/books/OL10426208M", "authors"=>[{"url"=>"https://openlibrary.org/authors/OL6924809A/Iain_M._Banks", "name"=>"Iain M. Banks"}], "publish_date"=>"July 28, 2008", "ebooks"=>[{"checkedout"=>false, "formats"=>{}, "preview_url"=>"https://archive.org/details/useofweapons00bank_0", "borrow_url"=>"https://openlibrary.org/books/OL10426208M/Use_of_Weapons/borrow", "availability"=>"borrow"}]}}
|
||||
]
|
||||
view: { 'ISBN:9780316030571' => { 'bib_key' => 'ISBN:9780316030571', 'preview' => 'borrow', 'thumbnail_url' => 'https://covers.openlibrary.org/b/id/2379038-S.jpg', 'preview_url' => 'https://archive.org/details/useofweapons00bank_0', 'info_url' => 'https://openlibrary.org/books/OL10426208M/Use_of_Weapons' } },
|
||||
data: { 'ISBN:9780316030571' => { 'publishers' => [{ 'name' => 'Orbit' }], 'identifiers' => { 'isbn_13' => ['9780316030571'], 'openlibrary' => ['OL10426208M'], 'isbn_10' => ['0316030570'], 'librarything' => ['141'], 'goodreads' => ['3395563'] }, 'title' => 'Use of Weapons', 'url' => 'https://openlibrary.org/books/OL10426208M/Use_of_Weapons', 'number_of_pages' => 480, 'cover' => { 'small' => 'https://covers.openlibrary.org/b/id/2379038-S.jpg', 'large' => 'https://covers.openlibrary.org/b/id/2379038-L.jpg', 'medium' => 'https://covers.openlibrary.org/b/id/2379038-M.jpg' }, 'subjects' => [{ 'url' => 'https://openlibrary.org/subjects/fiction', 'name' => 'Fiction' }, { 'url' => 'https://openlibrary.org/subjects/science_fiction', 'name' => 'Science Fiction' }, { 'url' => 'https://openlibrary.org/subjects/science_fiction_&_fantasy', 'name' => 'Science Fiction & Fantasy' }, { 'url' => 'https://openlibrary.org/subjects/long_now_manual_for_civilization', 'name' => 'Long Now Manual for Civilization' }, { 'url' => 'https://openlibrary.org/subjects/space_warfare', 'name' => 'Space warfare' }], 'subject_people' => [{ 'url' => 'https://openlibrary.org/subjects/person:cheradenine_zakalwe', 'name' => 'Cheradenine Zakalwe' }, { 'url' => 'https://openlibrary.org/subjects/person:diziet_sma', 'name' => 'Diziet Sma' }, { 'url' => 'https://openlibrary.org/subjects/person:skaffen-amtiskaw', 'name' => 'Skaffen-Amtiskaw' }], 'key' => '/books/OL10426208M', 'authors' => [{ 'url' => 'https://openlibrary.org/authors/OL6924809A/Iain_M._Banks', 'name' => 'Iain M. Banks' }], 'publish_date' => 'July 28, 2008', 'ebooks' => [{ 'checkedout' => false, 'formats' => {}, 'preview_url' => 'https://archive.org/details/useofweapons00bank_0', 'borrow_url' => 'https://openlibrary.org/books/OL10426208M/Use_of_Weapons/borrow', 'availability' => 'borrow' }] } }
|
||||
].freeze
|
||||
end
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
$LOAD_PATH.unshift File.expand_path("../lib", __dir__)
|
||||
require "openlib"
|
||||
# frozen_string_literal: true
|
||||
|
||||
require "minitest/autorun"
|
||||
$LOAD_PATH.unshift File.expand_path('../lib', __dir__)
|
||||
require 'openlib'
|
||||
|
||||
require 'minitest/autorun'
|
||||
|
||||
Reference in New Issue
Block a user