mirror of
https://github.com/dkam/openlib.git
synced 2025-12-28 03:04:53 +00:00
Get the data from the array just once
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user