Module: Shodanz::API::Utils

Included in:
Exploits, REST, Streaming
Defined in:
lib/shodanz/apis/utils.rb

Overview

Utils provides simply get, post, and slurp_stream functionality to the client. Under the hood they support both async and non-async usage. You should basically never need to use these methods directly.

Author:

  • Kent ‘picat’ Gruber

Instance Method Summary collapse

Instance Method Details

#get(path, **params) ⇒ Object

Perform a direct GET HTTP request to the REST API.



16
17
18
19
20
# File 'lib/shodanz/apis/utils.rb', line 16

def get(path, **params)
  return sync_get(path, **params) unless Async::Task.current?

  async_get(path, **params)
end

#post(path, body: nil, **params) ⇒ Object

Perform a direct POST HTTP request to the REST API.



23
24
25
26
27
# File 'lib/shodanz/apis/utils.rb', line 23

def post(path, body: nil, **params)
  return sync_post(path, params: params, body: body) unless Async::Task.current?

  async_post(path, params: params, body: body)
end

#slurp_stream(path, **params) ⇒ Object

Perform the main function of consuming the streaming API.



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/shodanz/apis/utils.rb', line 30

def slurp_stream(path, **params)
  if Async::Task.current?
    async_slurp_stream(path, **params) do |result|
      yield result
    end
  else
    sync_slurp_stream(path, **params) do |result|
      yield result
    end
  end
end

#turn_into_facets(**facets) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/shodanz/apis/utils.rb', line 50

def turn_into_facets(**facets)
  return {} if facets.nil?

  filters = facets.reject { |key, _| key == :facets }
  facets[:facets] = []
  filters.each do |key, value|
    facets[:facets] << "#{key}:#{value}"
  end
  facets[:facets] = facets[:facets].join(',')
  facets.select { |key, _| key == :facets }
end

#turn_into_query(**params) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/shodanz/apis/utils.rb', line 42

def turn_into_query(**params)
  filters = params.reject { |key, _| key == :query }
  filters.each do |key, value|
    params[:query] << " #{key}:#{value}"
  end
  params.select { |key, _| key == :query }
end