Get the data from the array just once

This commit is contained in:
Dan Milne
2020-04-22 17:29:07 +10:00
parent d71c58a047
commit fd69f9e04d
3 changed files with 15 additions and 4 deletions

View File

@@ -27,15 +27,19 @@ module Openlib
@data ||= @client.get(id: @id, id_kind: @id_kind, format: 'data')
end
def web
@web ||= @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') }
when :authors, :publishers, :subjects then data.dig(req.to_s).map { |p| p.dig('name') }
else
data.first.last.dig(req.to_s)
data.dig(req.to_s)
end
end
@@ -44,11 +48,14 @@ module Openlib
end
def to_h
return {} if data.empty?
%i[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].each_with_object({}) do |obj, memo|
memo[obj] = send(obj) unless send(obj).nil?
memo
end
end

View File

@@ -20,7 +20,11 @@ module Openlib
puts resp.status.first.to_s unless resp.status.first == '200'
JSON.parse(resp.read)
data = JSON.parse(resp.read)
return data if data.empty?
data.find { |k, _v| k.include?(id) }&.last
end
end
end