class Isobib::HitPages

Pages of hits.

Attributes

text[R]

@return [String]

Public Class Methods

new(text) click to toggle source

@param text [String]

# 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

Public Instance Methods

[](idx) click to toggle source

@param i [Integer] @return [Isobib::HitCollection]

Calls superclass method
# File lib/isobib/hit_pages.rb, line 31
def [](idx)
  # collection i
  return if idx + 1 > @nb_pages
  collection idx
  super
end
each() { |self| ... } click to toggle source
# File lib/isobib/hit_pages.rb, line 47
def each(&block)
  @nb_pages.times do |n|
    yield self[n] if block
  end
end
inspect() click to toggle source
# File lib/isobib/hit_pages.rb, line 57
def inspect
  "<#{self.class}:#{format('%#.14x', object_id << 1)} @text=#{@text} "\
  "@pages=#{@nb_pages}>"
end
last() click to toggle source

@return [Isobib::HitCollection]

# File lib/isobib/hit_pages.rb, line 25
def last
  collection(@nb_pages - 1)
end
map() { |self| ... } click to toggle source

@return [Array]

# 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
size() click to toggle source

@return [Integer]

# File lib/isobib/hit_pages.rb, line 63
def size
  @nb_pages
end
to_s() click to toggle source
# File lib/isobib/hit_pages.rb, line 53
def to_s
  inspect
end
to_xml() click to toggle source
# 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

Private Instance Methods

collection(idx) click to toggle source

@param i [Integer] @return [Isobib::HitCollection]

# File lib/isobib/hit_pages.rb, line 83
def collection(idx)
  return if idx + 1 > @nb_pages
  while Array.instance_method(:size).bind(self).call < idx + 1
    resp = @index.search(@text,
                         facetFilters: ['category:standard'],
                         page:         idx)
    self << HitCollection.new(resp['hits'], self)
  end
  Array.instance_method(:[]).bind(self).call idx
end