Class: Isobib::HitPages

Inherits:
Array
  • Object
show all
Defined in:
lib/isobib/hit_pages.rb

Overview

Pages of hits.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text) ⇒ HitPages

Returns a new instance of HitPages.

Parameters:

  • text (String)


16
17
18
19
20
21
22
# File 'lib/isobib/hit_pages.rb', line 16

def initialize(text)
  @text = text
  @index = Algolia::Index.new 'all_en'
  resp = @index.search(text, facetFilters: ['category:standard'])
  @nb_pages = resp['nbPages']
  self << HitCollection.new(resp['hits'], self)
end

Instance Attribute Details

#textString (readonly)

Returns:

  • (String)


13
14
15
# File 'lib/isobib/hit_pages.rb', line 13

def text
  @text
end

Instance Method Details

#[](idx) ⇒ Isobib::HitCollection

Parameters:

  • i (Integer)

Returns:



31
32
33
34
35
36
# File 'lib/isobib/hit_pages.rb', line 31

def [](idx)
  # collection i
  return if idx + 1 > @nb_pages
  collection idx
  super
end

#each(&block) ⇒ Object



47
48
49
50
51
# File 'lib/isobib/hit_pages.rb', line 47

def each(&block)
  @nb_pages.times do |n|
    yield self[n] if block
  end
end

#inspectObject



57
58
59
60
# File 'lib/isobib/hit_pages.rb', line 57

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

#lastIsobib::HitCollection



25
26
27
# File 'lib/isobib/hit_pages.rb', line 25

def last
  collection(@nb_pages - 1)
end

#map(&block) ⇒ Array

Returns:

  • (Array)


39
40
41
42
43
44
45
# File 'lib/isobib/hit_pages.rb', line 39

def map(&block)
  m = []
  @nb_pages.times do |n|
    m << yield(self[n]) if block
  end
  m
end

#sizeInteger

Returns:

  • (Integer)


63
64
65
# File 'lib/isobib/hit_pages.rb', line 63

def size
  @nb_pages
end

#to_sObject



53
54
55
# File 'lib/isobib/hit_pages.rb', line 53

def to_s
  inspect
end

#to_xmlObject



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/isobib/hit_pages.rb', line 67

def to_xml
  builder = Nokogiri::XML::Builder.new(encoding: 'UTF-8') do |xml|
    xml.documents do
      each do |page|
        page.fetch
        page.each { |hit| hit.to_xml xml }
      end
    end
  end
  builder.to_xml
end