Sha256: 07226eced900c18fa2fe01bccf85117268ceee52064f13f15df6a168a45327de

Contents?: true

Size: 1.71 KB

Versions: 3

Compression:

Stored size: 1.71 KB

Contents

# frozen_string_literal: true

require 'nistbib/hit'
require 'nistbib/workers_pool'
require "addressable/uri"
require 'open-uri'

module NistBib
  # Page of hit collection.
  class HitCollection < Array

    DOMAIN = 'https://csrc.nist.gov'

    # @return [TrueClass, FalseClass]
    attr_reader :fetched

    # @return [String]
    attr_reader :text

    # @return [String]
    attr_reader :year

    # @param ref_nbr [String]
    # @param year [String]
    def initialize(ref_nbr, year = nil) #(text, hit_pages = nil)
      @text = ref_nbr
      @year = year
      from, to = nil
      if year
        d = Date.strptime year, '%Y'
        from = d.strftime '%m/%d/%Y'
        to   = d.next_year.prev_day.strftime '%m/%d/%Y'
      end
      url  = "#{DOMAIN}/publications/search?keywords-lg=#{ref_nbr}&dateFrom-lg=#{from}&dateTo-lg=#{to}"
      doc  = Nokogiri::HTML OpenURI.open_uri(::Addressable::URI.parse(url).normalize)
      hits = doc.css('table.publications-table > tbody > tr').map do |h|
        link  = h.at('td/div/strong/a')
        code  = h.at('td[2]').text.strip
        title = link.text
        url   = DOMAIN + link[:href]
        Hit.new({ code: code, title: title, url: url }, self)
      end
      concat hits
      # concat(hits.map { |h| Hit.new(h, self) })
      @fetched = false
      # @hit_pages = hit_pages
    end

    # @return [Iecbib::HitCollection]
    def fetch
      workers = WorkersPool.new 4
      workers.worker(&:fetch)
      each do |hit|
        workers << hit
      end
      workers.end
      workers.result
      @fetched = true
      self
    end

    def to_s
      inspect
    end

    def inspect
      "<#{self.class}:#{format('%#.14x', object_id << 1)} @fetched=#{@fetched}>"
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
nistbib-0.1.2 lib/nistbib/hit_collection.rb
nistbib-0.1.1 lib/nistbib/hit_collection.rb
nistbib-0.1.0 lib/nistbib/hit_collection.rb