Class: ItuBib::HitCollection

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

Overview

Page of hit collection.

Constant Summary collapse

DOMAIN =
'https://www.itu.int'

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)


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
# File 'lib/itubib/hit_collection.rb', line 24

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"=>"163",
    "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)


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

def fetched
  @fetched
end

#textString (readonly)

Returns:

  • (String)


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

def text
  @text
end

#yearString (readonly)

Returns:

  • (String)


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

def year
  @year
end

Instance Method Details

#fetchItuBib::HitCollection



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/itubib/hit_collection.rb', line 72

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

#inspectObject



88
89
90
# File 'lib/itubib/hit_collection.rb', line 88

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

#to_sObject



84
85
86
# File 'lib/itubib/hit_collection.rb', line 84

def to_s
  inspect
end