Module: Shodan::HasPages

Includes:
Enumerable
Included in:
Query
Defined in:
lib/shodan/has_pages.rb

Instance Method Summary collapse

Instance Method Details

#[](index) ⇒ Page

The page at a given index.

Parameters:

  • index (Integer)

    The index to request a page for.

Returns:

  • (Page)

    The page at the specified index.



44
45
46
# File 'lib/shodan/has_pages.rb', line 44

def [](index)
  page_cache[index]
end

#each {|page| ... } ⇒ self

Iterates over every page, until an empty page is encountered.

Yields:

  • (page)

    The given block will receive every non-empty page.

Yield Parameters:

  • page (Page)

    A non-empty page.

Returns:

  • (self)


92
93
94
95
96
97
98
99
100
101
# File 'lib/shodan/has_pages.rb', line 92

def each(&block)
  index = 1

  until ((next_page = page_cache[index]).empty?) do
    block.call(next_page)
    index = index + 1
  end

  return self
end

#each_host {|host| ... } ⇒ self

Iterates over every host.

Yields:

  • (host)

    If a block is given, it will be passed every host from every page.

Yield Parameters:

  • host (Host)

    A host.

Returns:

  • (self)


114
115
116
# File 'lib/shodan/has_pages.rb', line 114

def each_host(&block)
  each { |page| page.each(&block) }
end

#each_on_page(index) {|host| ... } ⇒ Object

Iterates over the hosts in a page at a given index.

Parameters:

  • index (Integer)

    The index of the page to iterate over.

Yields:

  • (host)

    If a block is given, it will be passed every host on the page at the specified index.

Yield Parameters:

  • host (Host)

    A host on the page at the specified index.



131
132
133
# File 'lib/shodan/has_pages.rb', line 131

def each_on_page(index,&block)
  page_cache[index].each(&block)
end

#each_on_pages(indices) {|host| ... } ⇒ self

Iterates over each host on the pages at the given indices.

Parameters:

  • indices (Range, Array<Integer>)

    The indices of the pages.

Yields:

  • (host)

    If a block is given, it will be passed ever host on the pages at the specified indices.

Yield Parameters:

  • host (Host)

    A host on one of the pages at the specified indices.

Returns:

  • (self)


150
151
152
# File 'lib/shodan/has_pages.rb', line 150

def each_on_pages(indices,&block)
  each_page(indices) { |page| page.each(&block) }
end

#each_page(indices) {|page| ... } ⇒ self

Iterates over the pages at given indices.

Parameters:

  • The (Range, Array<Integer>)

    indices to request pages for.

Yields:

  • (page)

    The given block will be passed each page at one of the specified indices.

Yield Parameters:

  • page (Page)

    The page at one of the specified indices.

Returns:

  • (self)


76
77
78
79
# File 'lib/shodan/has_pages.rb', line 76

def each_page(indices,&block)
  indices.map { |index| block.call(page_cache[index]) }
  return self
end

#first_hostHost

The first host on the first page.

Returns:

  • (Host)

    The first host.



160
161
162
# File 'lib/shodan/has_pages.rb', line 160

def first_host
  first_page.first
end

#first_pagePage

The first page.

Returns:

  • (Page)

    The first page.



31
32
33
# File 'lib/shodan/has_pages.rb', line 31

def first_page
  page_cache[1]
end

#host_at(index) ⇒ Host

Returns the host at a given index.

Parameters:

  • index (Integer)

    The index to request the host at.

Returns:

  • (Host)

    The host at the given index.



173
174
175
# File 'lib/shodan/has_pages.rb', line 173

def host_at(index)
  page(page_index_of(index))[result_index_of(index)]
end

#pages(indices) ⇒ Array<Page>

The pages at the given indices.

Parameters:

  • The (Range, Array<Integer>)

    indices to request pages for.

Returns:

  • (Array<Page>)

    The pages at the specified indices.



57
58
59
# File 'lib/shodan/has_pages.rb', line 57

def pages(indices)
  indices.map { |index| page_cache[index] }
end