class UrlScan::Clients::Base
Constants
- HOST
- VERSION
Attributes
key[R]
Public Class Methods
new(key = ENV["URLSCAN_API_KEY"])
click to toggle source
# File lib/urlscan/clients/base.rb, line 14 def initialize(key = ENV["URLSCAN_API_KEY"]) @key = key end
Private Instance Methods
default_headers()
click to toggle source
# File lib/urlscan/clients/base.rb, line 64 def default_headers @default_headers ||= { "API-KEY": key }.compact end
get(path, params = {}, &block)
click to toggle source
# File lib/urlscan/clients/base.rb, line 68 def get(path, params = {}, &block) uri = url_for(path) uri.query = URI.encode_www_form(params) get = Net::HTTP::Get.new(uri, default_headers) request(get, &block) end
https_options()
click to toggle source
# File lib/urlscan/clients/base.rb, line 28 def https_options if 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, json, &block)
click to toggle source
# File lib/urlscan/clients/base.rb, line 76 def post(path, json, &block) post = Net::HTTP::Post.new(url_for(path), default_headers) post.content_type = "application/json" post.body = json.to_json request(post, &block) end
request(req) { |parse| ... }
click to toggle source
# File lib/urlscan/clients/base.rb, line 42 def request(req) Net::HTTP.start(HOST, 443, https_options) do |http| response = http.request(req) case response.code when "200" if response["Content-Type"].to_s.include? "application/json" yield JSON.parse(response.body) else yield response.body end when "400" then raise ProcessingError, response.body when "401" then raise AuthenticationError, response.body when "404" then raise NotFound, response.body when "429" then raise RateLimited, response.body when "500" then raise InternalServerError, response.body else raise ResponseError, response.body end end end
url()
click to toggle source
# File lib/urlscan/clients/base.rb, line 20 def url @url ||= "https://#{HOST}/api/v#{VERSION}" end
url_for(path)
click to toggle source
# File lib/urlscan/clients/base.rb, line 24 def url_for(path) URI(url + path) end