Class: RelatonItu::HitCollection

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

Overview

Page of hit collection.

Constant Summary collapse

DOMAIN =
"https://www.itu.int".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ref_nbr, year = nil) ⇒ HitCollection

Returns a new instance of HitCollection.

Parameters:

  • ref_nbr (String)
  • year (String) (defaults to: nil)


23
24
25
26
27
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/relaton_itu/hit_collection.rb', line 23

def initialize(ref_nbr, year = nil) #(text, hit_pages = nil)
  @text = ref_nbr
  @year = year
  # from, to = nil
  # if year
  #   from = Date.strptime year, "%Y"
  #   to   = from.next_year.prev_day
  # end
  url = "#{DOMAIN}/net4/ITU-T/search/GlobalSearch/Search"
  params = {
    "Input" => ref_nbr,
    "Start" => 0,
    "Rows" => 10,
    "SortBy" => "RELEVANCE",
    "ExactPhrase" => false,
    "CollectionName" => "General",
    "CollectionGroup" => "Recommendations",
    "Sector" => "t",
    "Criterias" => [{
      "Name" => "Search in",
      "Criterias" => [
        {
          "Selected" => false,
          "Value" => "",
          "Label" => "Name",
          "Target" => "/name_s",
          "TypeName" => "CHECKBOX",
          "GetCriteriaType" => 0,
        },
        {
          "Selected" => false,
          "Value" => "",
          "Label" => "Short description",
          "Target" => "/short_description_s",
          "TypeName" => "CHECKBOX",
          "GetCriteriaType" => 0,
        },
        {
          "Selected" => false,
          "Value" => "",
          "Label" => "File content",
          "Target" => "/file",
          "TypeName" => "CHECKBOX",
          "GetCriteriaType" => 0,
        },
      ],
      "ShowCheckbox" => true,
      "Selected" => false,
    }],
    "Topics" => "",
    "ClientData" => { "ip" => "" },
    "Language" => "en",
    "IP" => "",
    "SearchType" => "All",
  }
  data = { json: params.to_json }
  resp  = Net::HTTP.post(URI(url), data.to_json, "Content-Type" => "application/json")
  doc = JSON.parse resp.body
  hits = doc["results"].map do |h|
    code  = h["Media"]["Name"]
    title = h["Title"]
    url   = h["Redirection"]
    Hit.new({ code: code, title: title, url: url }, self)
  end
  concat hits
  @fetched = false
end

Instance Attribute Details

#fetchedTrueClass, FalseClass (readonly)

Returns:

  • (TrueClass, FalseClass)


13
14
15
# File 'lib/relaton_itu/hit_collection.rb', line 13

def fetched
  @fetched
end

#textString (readonly)

Returns:

  • (String)


16
17
18
# File 'lib/relaton_itu/hit_collection.rb', line 16

def text
  @text
end

#yearString (readonly)

Returns:

  • (String)


19
20
21
# File 'lib/relaton_itu/hit_collection.rb', line 19

def year
  @year
end

Instance Method Details

#fetchRelatonItu::HitCollection



92
93
94
95
96
97
98
99
100
101
102
# File 'lib/relaton_itu/hit_collection.rb', line 92

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



108
109
110
# File 'lib/relaton_itu/hit_collection.rb', line 108

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

#to_sObject



104
105
106
# File 'lib/relaton_itu/hit_collection.rb', line 104

def to_s
  inspect
end