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



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

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
  # if year
  #   search << "stageDateStart=#{Date.new(year.to_i).strftime("%Y-%m-%d")}"
  #   search << "stageDateEnd=#{Date.new(year.to_i, 12, 31).strftime("%Y-%m-%d")}"
  # end
  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, NilClass (readonly)



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

def text
  @text
end

Instance Method Details

#inspectObject



80
81
82
# File 'lib/relaton_iso/hit_collection.rb', line 80

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

#reduce!(sum, &block) ⇒ Object



51
52
53
54
# File 'lib/relaton_iso/hit_collection.rb', line 51

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

#select(&block) ⇒ Object



45
46
47
48
49
# File 'lib/relaton_iso/hit_collection.rb', line 45

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

#to_all_parts(lang = nil) ⇒ RelatonIsoBib::IsoBibliographicItem



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/relaton_iso/hit_collection.rb', line 58

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

  bibitem = hit.fetch lang
  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



76
77
78
# File 'lib/relaton_iso/hit_collection.rb', line 76

def to_s
  inspect
end

#to_xml(**opts) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/relaton_iso/hit_collection.rb', line 84

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