Class: Censys::Search::Response

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/censys/search/response.rb

Constant Summary collapse

RESULTS =
{
  ipv4:         IPv4,
  websites:     Website,
  certificates: Certificate
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api, result_type, params, response) ⇒ Response

Returns a new instance of Response.

Parameters:

  • api (API)

    Parent API.

  • result_type (:ipv4, :website, :certificate)

    Result type.

  • params (Hash)

    Search parameters.

  • response (Hash{String => Object})

    Response JSON Hash.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/censys/search/response.rb', line 38

def initialize(api, result_type, params, response)
  @api         = api
  @result_type = result_type
  @params      = params

  @status   = response['status']
  @metadata = Metadata.new(response['metadata'])

  unless (result_class = RESULTS[@result_type])
    raise(ArgumentError, "invalid result type: #{@result_type}")
  end

  @results = response['results'].map do |result|
    result_class.new(result, @api)
  end
end

Instance Attribute Details

#metadataObject (readonly)

Returns the value of attribute metadata.



21
22
23
# File 'lib/censys/search/response.rb', line 21

def 
  @metadata
end

#resultsObject (readonly)

Returns the value of attribute results.



23
24
25
# File 'lib/censys/search/response.rb', line 23

def results
  @results
end

#statusObject (readonly)

Returns the value of attribute status.



19
20
21
# File 'lib/censys/search/response.rb', line 19

def status
  @status
end

Instance Method Details

#each {|result| ... } ⇒ Enumerator

Enumerates over all results in the response.

Yields:

  • (result)

    The given block will be passed each result.

Yield Parameters:

Returns:

  • (Enumerator)

    If no block is given, an Enumerator will be returned.



76
77
78
# File 'lib/censys/search/response.rb', line 76

def each(&block)
  @results.each(&block)
end

#each_page {|page| ... } ⇒ Enumerator

Enumerates through each page of results.

Yields:

  • (page)

    The given block will be passed each page.

Yield Parameters:

  • page (Response)

    The response containing the next page of results.

Returns:

  • (Enumerator)

    If no block was given, an enumerator will be returned.



104
105
106
107
108
109
110
111
112
113
114
# File 'lib/censys/search/response.rb', line 104

def each_page
  return enum_for(__method__) unless block_given?

  page = self

  while page
    yield page

    page = page.next_page
  end
end

#next_pageSearch::Response? Also known as: next

Queries the next page of results.

Returns:

  • (Search::Response, nil)

    The next page of results or nil if there are no more pages.



86
87
88
# File 'lib/censys/search/response.rb', line 86

def next_page
  @api.search(@result_type, @params.merge(page: @metadata.page + 1)) if @metadata.page < @metadata.pages
end

#ok?Boolean

Determines if the response has status of ok.

Returns:

  • (Boolean)


60
61
62
# File 'lib/censys/search/response.rb', line 60

def ok?
  @status == 'ok'
end

#pagesEnumerator

Provides access to additional pages.

Returns:

  • (Enumerator)


121
122
123
# File 'lib/censys/search/response.rb', line 121

def pages
  enum_for(:each_page)
end