Remove debugging and fix test. Rubocop run

This commit is contained in:
Dan Milne
2020-04-22 14:12:39 +10:00
parent 4d91ad81cf
commit 20d670d6e9
6 changed files with 43 additions and 124 deletions

View File

@@ -9,6 +9,4 @@ module Openlib
class Error < StandardError; end class Error < StandardError; end
# Your code goes here... # Your code goes here...
end end

View File

@@ -1,8 +1,11 @@
# frozen_string_literal: true
require 'json' require 'json'
module Openlib module Openlib
class Book 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 = id
@id_kind = id_kind.to_sym @id_kind = id_kind.to_sym
@client = Openlib::Client.new(user_agent: user_agent) @client = Openlib::Client.new(user_agent: user_agent)
@@ -10,6 +13,10 @@ module Openlib
@data = nil @data = nil
end end
def cache_clear
@view = @data = nil
end
## ##
# Check that we got back the expected data # Check that we got back the expected data
def view def view
@@ -39,16 +46,15 @@ module Openlib
def method_missing(m, *args, &block) def method_missing(m, *args, &block)
case m case m
when :info_url, :preview, :preview_url, :thumbnail_url 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, when :url, :authors, :identifiers, :classifications, :subjects,
:subject_places, :subject_people, :subject_times, :publishers, :subject_places, :subject_people, :subject_times, :publishers,
:publish_places, :publish_date, :excerpts, :links, :cover, :ebooks, :publish_places, :publish_date, :excerpts, :links, :cover, :ebooks,
:number_of_pages, :weight, :title :number_of_pages, :weight, :title
then self.send('data_data', req: m) send('data_data', req: m)
else else
super super
end end
end end
end end
end end

View File

@@ -10,14 +10,17 @@ module Openlib
end end
def get(id:, format: 'data', id_kind: :isbn) 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) JSON.parse(resp.read)
end end
end end
end end

View File

@@ -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"
}
}
]

View File

@@ -1,4 +1,6 @@
require "test_helper" # frozen_string_literal: true
require 'test_helper'
require 'byebug' require 'byebug'
class OpenlibTest < Minitest::Test class OpenlibTest < Minitest::Test
@@ -7,21 +9,19 @@ class OpenlibTest < Minitest::Test
end end
def test_it_does_something_useful def test_it_does_something_useful
TESTCASES.each do |tc| TESTCASES.each do |tc|
b = Openlib::Book.new(id: tc.dig(:isbn)) b = Openlib::Book.new(id: tc.dig(:isbn))
b.view = tc.dig(:view) b.view = tc.dig(:view)
b.data = tc.dig(:data) 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('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('title'), b.title
end end
end end
TESTCASES = [ TESTCASES = [
isbn: '9780316030571', 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"}}, 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"}]}} 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 end

View File

@@ -1,4 +1,6 @@
$LOAD_PATH.unshift File.expand_path("../lib", __dir__) # frozen_string_literal: true
require "openlib"
require "minitest/autorun" $LOAD_PATH.unshift File.expand_path('../lib', __dir__)
require 'openlib'
require 'minitest/autorun'