Class: RelatonOgc::HitCollection

Inherits:
RelatonBib::HitCollection
  • Object
show all
Defined in:
lib/relaton_ogc/hit_collection.rb

Constant Summary collapse

ENDPOINT =
"https://raw.githubusercontent.com/relaton/relaton-data-ogc/main/".freeze
INDEX_FILE =
"index-v1.yaml".freeze

Instance Method Summary collapse

Constructor Details

#initialize(code, year = nil) ⇒ HitCollection

Returns a new instance of HitCollection.

Parameters:

  • code (Strig)
  • year (String) (defaults to: nil)
  • opts (Hash)


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/relaton_ogc/hit_collection.rb', line 13

def initialize(code, year = nil) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
  super
  @array = []
  return if code.nil? || code.empty?

  index = Relaton::Index.find_or_create :ogc, url: "#{ENDPOINT}index-v1.zip", file: INDEX_FILE
  row = index.search(code).min_by { |r| r[:id] }
  return unless row

  url = "#{ENDPOINT}#{row[:file]}"
  resp = Faraday.get(url) { |req| req.options.timeout = 10 }
  return unless resp.status == 200

  hash = YAML.safe_load(resp.body)
  hash["fetched"] = Date.today.to_s
  bib = OgcBibliographicItem.from_hash hash
  @array = [Hit.new(bib, self)]
end