Sha256: fc042f74dc5aeb99f43037986d176051b00a65abad913214feea198e424c8191
Contents?: true
Size: 1.89 KB
Versions: 3
Compression:
Stored size: 1.89 KB
Contents
# frozen_string_literal: true require "net/https" require "json" module UrlScan module Clients class Base VERSION = 1 HOST = "urlscan.io" attr_reader :key def initialize(key = ENV["URLSCAN_API_KEY"]) @key = key end private def url @url ||= "https://#{self.class::HOST}/api/v#{self.class::VERSION}" end def url_for(path) URI(url + path) end 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 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 def auth_header { "API-KEY": key } end def get(path, &block) get = Net::HTTP::Get.new(url_for(path)) request(get, &block) end def post(path, json, &block) post = Net::HTTP::Post.new(url_for(path), auth_header) post.content_type = "application/json" post.body = json.to_json request(post, &block) end end end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
urlscan-0.5.0 | lib/urlscan/clients/base.rb |
urlscan-0.4.1 | lib/urlscan/clients/base.rb |
urlscan-0.4.0 | lib/urlscan/clients/base.rb |