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

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

View File

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

View File

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