Class: RelatonNist::HitCollection

Inherits:
Array
  • Object
show all
Defined in:
lib/relaton_nist/hit_collection.rb

Overview

Page of hit collection.

Constant Summary collapse

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ref_nbr, year = nil, opts = {}) ⇒ HitCollection

Returns a new instance of HitCollection.

Parameters:

  • ref_nbr (String)
  • year (String) (defaults to: nil)
  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • :stage (String)


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/relaton_nist/hit_collection.rb', line 28

def initialize(ref_nbr, year = nil, opts = {})
  @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}"
  url += "&dateFrom-lg=#{from}" if from
  url += "&dateTo-lg=#{to}" if to
  url += if /PD/ =~ opts[:stage]
           "&status-lg=Draft,Retired Draft,Withdrawn"
         else
           "&status-lg=Final,Withdrawn"
         end

  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")
    serie = h.at("td[1]").text.strip
    code  = h.at("td[2]").text.strip
    title = link.text
    url   = DOMAIN + link[:href]
    status = h.at("td[4]").text.strip.downcase
    release_date = Date.strptime h.at("td[5]").text.strip, "%m/%d/%Y"
    Hit.new(
      {
        code: code, serie: serie, title: title, url: url, status: status,
        release_date: release_date
      }, self
    )
  end
  concat(hits.sort { |a, b| a.sort_value - b.sort_value })
  # concat(hits.map { |h| Hit.new(h, self) })
  @fetched = false
  # @hit_pages = hit_pages
end

Instance Attribute Details

#fetchedTrueClass, FalseClass (readonly)

Returns:

  • (TrueClass, FalseClass)


14
15
16
# File 'lib/relaton_nist/hit_collection.rb', line 14

def fetched
  @fetched
end

#textString (readonly)

Returns:

  • (String)


17
18
19
# File 'lib/relaton_nist/hit_collection.rb', line 17

def text
  @text
end

#yearString (readonly)

Returns:

  • (String)


20
21
22
# File 'lib/relaton_nist/hit_collection.rb', line 20

def year
  @year
end

Instance Method Details

#fetchIecbib::HitCollection

Returns:

  • (Iecbib::HitCollection)


70
71
72
73
74
75
76
77
78
79
80
# File 'lib/relaton_nist/hit_collection.rb', line 70

def fetch
  workers = RelatonBib::WorkersPool.new 4
  workers.worker(&:fetch)
  each do |hit|
    workers << hit
  end
  workers.end
  workers.result
  @fetched = true
  self
end

#inspectObject



86
87
88
# File 'lib/relaton_nist/hit_collection.rb', line 86

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

#to_sObject



82
83
84
# File 'lib/relaton_nist/hit_collection.rb', line 82

def to_s
  inspect
end