From 20d670d6e90cc070e2df4197646a3616b07d16b7 Mon Sep 17 00:00:00 2001 From: Dan Milne Date: Wed, 22 Apr 2020 14:12:39 +1000 Subject: [PATCH] Remove debugging and fix test. Rubocop run --- lib/openlib.rb | 4 +- lib/openlib/book.rb | 30 +++++++++------ lib/openlib/client.rb | 17 ++++---- test/fixtures.rb | 90 ------------------------------------------- test/openlib_test.rb | 18 ++++----- test/test_helper.rb | 8 ++-- 6 files changed, 43 insertions(+), 124 deletions(-) delete mode 100644 test/fixtures.rb diff --git a/lib/openlib.rb b/lib/openlib.rb index d4929bf..29b8a1f 100644 --- a/lib/openlib.rb +++ b/lib/openlib.rb @@ -6,9 +6,7 @@ require 'openlib/book' module Openlib ID_KINDS = %i[isbn oclc lccn olid].freeze - + class Error < StandardError; end # Your code goes here... - - end diff --git a/lib/openlib/book.rb b/lib/openlib/book.rb index fdd2bb3..ab4195b 100644 --- a/lib/openlib/book.rb +++ b/lib/openlib/book.rb @@ -1,15 +1,22 @@ +# frozen_string_literal: true + require 'json' module Openlib - class Book - def initialize(id, id_kind: :isbn, user_agent: nil) + class Book + 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,35 +27,34 @@ 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 end - + def author authors end - def method_missing(m, *args, &block) + def method_missing(m, *args, &block) case m - when :info_url, :preview, :preview_url, :thumbnail_url - then self.send('view_data', req: m) + when :info_url, :preview, :preview_url, :thumbnail_url + 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 \ No newline at end of file +end diff --git a/lib/openlib/client.rb b/lib/openlib/client.rb index 32062d6..8e5d1e5 100644 --- a/lib/openlib/client.rb +++ b/lib/openlib/client.rb @@ -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) - - 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' - + unless ID_KINDS.include?(id_kind) + raise ArgumetError, "Kind must be one of #{ID_KINDS}" + end + + url = "https://openlibrary.org/api/books?jscmd=#{format}&format=json&bibkeys=#{id_kind.to_s.upcase}:#{id}" + + 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 diff --git a/test/fixtures.rb b/test/fixtures.rb deleted file mode 100644 index 92b2085..0000000 --- a/test/fixtures.rb +++ /dev/null @@ -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" - } - } -] diff --git a/test/openlib_test.rb b/test/openlib_test.rb index 821c922..20950b6 100644 --- a/test/openlib_test.rb +++ b/test/openlib_test.rb @@ -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 - 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 diff --git a/test/test_helper.rb b/test/test_helper.rb index f4fa690..0ec549f 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -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'