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

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

View File

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

View File

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