class RelatonCalconnect::HitCollection

Constants

DATADIR

ENDPOINT = “127.0.0.1:4000/relaton/index.yaml”.freeze

DATAFILE
ENDPOINT
ETAGFILE

Public Class Methods

new(ref, year = nil) click to toggle source

@param ref [Strig] @param year [String] @param opts [Hash]

Calls superclass method
# File lib/relaton_calconnect/hit_collection.rb, line 16
def initialize(ref, year = nil)
  super
  @array = from_yaml(ref).sort_by do |hit|
    hit.hit["revdate"] ? Date.parse(hit.hit["revdate"]) : Date.new
  end.reverse
end

Private Instance Methods

data() click to toggle source

Fetches YAML data

@return [Hash]

# File lib/relaton_calconnect/hit_collection.rb, line 39
def data
  FileUtils.mkdir_p DATADIR
  ctime = File.ctime DATAFILE if File.exist? DATAFILE
  fetch_data if !ctime || ctime.to_date < Date.today
  @data ||= YAML.safe_load File.read(DATAFILE, encoding: "UTF-8")
end
etag() click to toggle source

Read ETag from file

@return [String, NilClass]

# File lib/relaton_calconnect/hit_collection.rb, line 63
def etag
  @etag ||= if File.exist? ETAGFILE
              File.read ETAGFILE, encoding: "UTF-8"
            end
end
etag=(e_tag) click to toggle source

Save ETag to file

@param tag [String]

# File lib/relaton_calconnect/hit_collection.rb, line 73
def etag=(e_tag)
  File.write ETAGFILE, e_tag, encoding: "UTF-8"
end
fetch_data() click to toggle source

fetch data from server and save it to file.

# File lib/relaton_calconnect/hit_collection.rb, line 49
def fetch_data
  resp = Faraday.new(ENDPOINT, headers: { "If-None-Match" => etag }).get
  # return if there aren't any changes since last fetching
  return unless resp.status == 200

  self.etag = resp[:etag]
  @data = YAML.safe_load resp.body
  File.write DATAFILE, @data.to_yaml, encoding: "UTF-8"
end
from_yaml(docid, **_opts) click to toggle source

Fetch data from yaml

@param docid [String]

# File lib/relaton_calconnect/hit_collection.rb, line 29
def from_yaml(docid, **_opts)
  data["root"]["items"].select do |doc|
    doc["docid"] && doc["docid"]["id"].include?(docid)
  end.map { |h| Hit.new(h, self) }
end