class Shodan::Clients::Base
Constants
Attributes
key[R]
Public Class Methods
new(key: ENV["SHODAN_API_KEY"])
click to toggle source
# File lib/shodan/clients/base.rb, line 16 def initialize(key: ENV["SHODAN_API_KEY"]) @key = key warn 'No key has been found or provided!' unless key? end
Public Instance Methods
key?()
click to toggle source
# File lib/shodan/clients/base.rb, line 21 def key? !key.nil? end
Private Instance Methods
_host()
click to toggle source
# File lib/shodan/clients/base.rb, line 27 def _host self.class::HOST end
base_url()
click to toggle source
# File lib/shodan/clients/base.rb, line 31 def base_url self.class::BASE_URL end
delete(path, **params)
click to toggle source
get(path, **params)
click to toggle source
https_options()
click to toggle source
# File lib/shodan/clients/base.rb, line 79 def https_options if proxy = ENV["HTTPS_PROXY"] || ENV["https_proxy"] uri = URI(proxy) { proxy_address: uri.hostname, proxy_port: uri.port, proxy_from_env: false, use_ssl: true } else { use_ssl: true } end end
post(path, **params)
click to toggle source
request(req)
click to toggle source
# File lib/shodan/clients/base.rb, line 35 def request(req) Net::HTTP.start(_host, 443, https_options) do |http| response = http.request(req) if response.code.to_i != 200 json = JSON.parse(response.body) raise Error, json["error"] if json.key?("error") raise Error, response.body end JSON.parse response.body end end
turn_into_facets(facets)
click to toggle source
# File lib/shodan/clients/base.rb, line 101 def turn_into_facets(facets) { facets: facets.map { |k, v| "#{k}:#{v}" }.join(",") } end
turn_into_query(params)
click to toggle source
# File lib/shodan/clients/base.rb, line 93 def turn_into_query(params) query = params.dig(:query) options = params.reject { |k, _| k == :query }.map { |k, v| "#{k}:#{v}" } { query: [query, options].flatten.compact.join(" ") } end