mirror of
https://github.com/dkam/openlib.git
synced 2025-12-28 19:24:53 +00:00
Get the data from the array just once
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
PATH
|
PATH
|
||||||
remote: .
|
remote: .
|
||||||
specs:
|
specs:
|
||||||
openlib (0.1.0)
|
openlib (0.1.1)
|
||||||
|
|
||||||
GEM
|
GEM
|
||||||
remote: https://rubygems.org/
|
remote: https://rubygems.org/
|
||||||
|
|||||||
@@ -27,15 +27,19 @@ module Openlib
|
|||||||
@data ||= @client.get(id: @id, id_kind: @id_kind, format: 'data')
|
@data ||= @client.get(id: @id, id_kind: @id_kind, format: 'data')
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def web
|
||||||
|
@web ||= @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)
|
view.first.last.dig(req.to_s)
|
||||||
end
|
end
|
||||||
|
|
||||||
def data_data(req:)
|
def data_data(req:)
|
||||||
case 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
|
else
|
||||||
data.first.last.dig(req.to_s)
|
data.dig(req.to_s)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -44,11 +48,14 @@ module Openlib
|
|||||||
end
|
end
|
||||||
|
|
||||||
def to_h
|
def to_h
|
||||||
|
return {} if data.empty?
|
||||||
|
|
||||||
%i[url authors identifiers classifications subjects
|
%i[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].each_with_object({}) do |obj, memo|
|
number_of_pages weight title].each_with_object({}) do |obj, memo|
|
||||||
memo[obj] = send(obj) unless send(obj).nil?
|
memo[obj] = send(obj) unless send(obj).nil?
|
||||||
|
memo
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -20,7 +20,11 @@ module Openlib
|
|||||||
|
|
||||||
puts resp.status.first.to_s unless resp.status.first == '200'
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user