Class: RelatonIso::HitCollection

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/relaton_iso/hit_collection.rb

Overview

Page of hit collection.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text) ⇒ HitCollection

Returns a new instance of HitCollection.

Parameters:

  • hits (Array<Hash>)


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/relaton_iso/hit_collection.rb', line 23

def initialize(text)
  @array = []
  @text = text
  %r{\s(?<num>\d+)(-(?<part>\d+))?} =~ text
  http = Net::HTTP.new "www.iso.org", 443
  http.use_ssl = true
  search = ["status=ENT_ACTIVE,ENT_PROGRESS,ENT_INACTIVE,ENT_DELETED"]
  search << "docNumber=#{num}"
  search << "docPartNo=#{part}" if part
  q = search.join "&"
  resp = http.get("/cms/render/live/en/sites/isoorg.advancedSearch.do?#{q}",
                  "Accept" => "application/json, text/plain, */*")
  return if resp.body.empty?

  json = JSON.parse resp.body
  @array = json["standards"].map { |h| Hit.new h, self }.sort! do |a, b|
    if a.sort_weight == b.sort_weight
      (parse_date(b.hit) - parse_date(a.hit)).to_i
    else
      a.sort_weight - b.sort_weight
    end
  end
end

Instance Attribute Details

#textString (readonly)

Returns:

  • (String)


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

def text
  @text
end

Instance Method Details

#inspectObject



95
96
97
# File 'lib/relaton_iso/hit_collection.rb', line 95

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

#reduce!(sum, &block) ⇒ Object



53
54
55
56
# File 'lib/relaton_iso/hit_collection.rb', line 53

def reduce!(sum, &block)
  @array = @array.reduce sum, &block
  self
end

#select(&block) ⇒ Object



47
48
49
50
51
# File 'lib/relaton_iso/hit_collection.rb', line 47

def select(&block)
  me = DeepClone.clone self
  me.instance_variable_get(:@array).select!(&block)
  me
end

#to_all_partsObject

workers = RelatonBib::WorkersPool.new 4

workers.worker(&:fetch)
@array.each do |hit|
  workers << hit
end
workers.end
workers.result
@fetched = true
self

end



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/relaton_iso/hit_collection.rb', line 73

def to_all_parts
  parts = @array.select { |h| !h.hit["docPart"].empty? }
  hit = parts.min_by { |h| h.hit["docPart"].to_i }
  return @array.first.fetch unless hit

  bibitem = hit.fetch
  bibitem.to_all_parts
  parts.reject { |h| h.hit["docRef"] == hit.hit["docRef"] }.each do |hi|
    isobib = RelatonIsoBib::IsoBibliographicItem.new(
      formattedref: RelatonBib::FormattedRef.new(content: hi.hit["docRef"]),
    )
    bibitem.relation << RelatonBib::DocumentRelation.new(
      type: "instance", bibitem: isobib,
    )
  end
  bibitem
end

#to_sObject



91
92
93
# File 'lib/relaton_iso/hit_collection.rb', line 91

def to_s
  inspect
end

#to_xml(**opts) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
# File 'lib/relaton_iso/hit_collection.rb', line 99

def to_xml(**opts)
  builder = Nokogiri::XML::Builder.new(encoding: "UTF-8") do |xml|
    xml.documents do
      @array.each do |hit|
        hit.fetch
        hit.to_xml xml, **opts
      end
    end
  end
  builder.to_xml
end