class RelatonNist::HitCollection
Page of hit collection.
Constants
- DATAFILE
- DATAFILEDIR
- DOMAIN
- GHNISTDATA
- PUBS_EXPORT
Public Class Methods
search(text, year = nil, opts = {})
click to toggle source
# File lib/relaton_nist/hit_collection.rb, line 18 def self.search(text, year = nil, opts = {}) new(text, year).search(opts) end
Public Instance Methods
search(opts)
click to toggle source
# File lib/relaton_nist/hit_collection.rb, line 22 def search(opts) @array = from_json(**opts) @array = from_ga unless @array.any? sort_hits! end
Private Instance Methods
data()
click to toggle source
Fetches json data form server @return [Hash]
# File lib/relaton_nist/hit_collection.rb, line 138 def data ctime = File.ctime DATAFILE if File.exist? DATAFILE if !ctime || ctime.to_date < Date.today fetch_data(ctime) end unzip end
fetch_data(ctime)
click to toggle source
Fetch data form server and save it to file
@prarm ctime [Time, NilClass]
# File lib/relaton_nist/hit_collection.rb, line 149 def fetch_data(ctime) # resp = OpenURI.open_uri("#{PUBS_EXPORT}.meta") if !ctime || ctime < OpenURI.open_uri("#{PUBS_EXPORT}.meta").last_modified @data = nil uri_open = URI.method(:open) || Kernel.method(:open) FileUtils.mkdir_p DATAFILEDIR unless Dir.exist? DATAFILEDIR IO.copy_stream(uri_open.call("#{PUBS_EXPORT}.zip"), DATAFILE) end end
from_ga()
click to toggle source
# File lib/relaton_nist/hit_collection.rb, line 41 def from_ga # rubocop:disable Metrics/AbcSize fn = text.gsub(%r{[/\s:.]}, "_").upcase yaml = OpenURI.open_uri "#{GHNISTDATA}#{fn}.yaml" hash = YAML.safe_load yaml bib = RelatonNist::NistBibliographicItem.from_hash hash hit = Hit.new({ code: text }, self) hit.fetch = bib [hit] rescue OpenURI::HTTPError => e return [] if e.io.status[0] == "404" raise e end
from_json(**opts)
click to toggle source
Fetches data form json @param stage [String] @return [Array<RelatonNist::Hit>]
# File lib/relaton_nist/hit_collection.rb, line 98 def from_json(**opts) select_data(**opts).map do |h| /(?<serie>(?<=-)\w+$)/ =~ h["series"] title = [h["title-main"], h["title-sub"]].compact.join " - " release_date = RelatonBib.parse_date h["published-date"], false Hit.new({ code: h["docidentifier"], serie: serie.upcase, title: title, url: h["uri"], status: h["status"], release_date: release_date, json: h }, self) end end
match_year?(doc, date)
click to toggle source
@param doc [Hash] @param date [Date] first day of year @return [TrueClass, FalseClass]
# File lib/relaton_nist/hit_collection.rb, line 129 def match_year?(doc, date) return true unless year idate = RelatonBib.parse_date doc["issued-date"], false idate.between? date, date.next_year.prev_day end
select_data(**opts)
click to toggle source
@param stage [String] @return [Array<Hach>]
# File lib/relaton_nist/hit_collection.rb, line 111 def select_data(**opts) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength,Metrics/PerceivedComplexity d = Date.strptime year, "%Y" if year statuses = %w[draft-public draft-prelim] data.select do |doc| next unless match_year?(doc, d) if /PD/.match? opts[:stage] next unless statuses.include? doc["status"] else next unless doc["status"] == "final" end doc["docidentifier"].include? text end end
sort_hits!()
click to toggle source
# File lib/relaton_nist/hit_collection.rb, line 30 def sort_hits! @array.sort! do |a, b| if a.sort_value == b.sort_value (b.hit[:release_date] - a.hit[:release_date]).to_i else b.sort_value - a.sort_value end end self end
unzip()
click to toggle source
upack zip file
@return [Hash]
# File lib/relaton_nist/hit_collection.rb, line 162 def unzip return @data if @data Zip::File.open(DATAFILE) do |zf| zf.each do |f| @data = JSON.parse f.get_input_stream.read break end end @data end