class URLhaus::API

Constants

BASE_URL
HOST

The path to the REST API endpoint.

VERSION

Public Instance Methods

download(sha256) click to toggle source
# File lib/urlhaus/api.rb, line 44
def download(sha256)
  get("/download/#{sha256}")
end
host(host) click to toggle source
# File lib/urlhaus/api.rb, line 18
def host(host)
  post("/host/", host: host)
end
payload(hash) click to toggle source
# File lib/urlhaus/api.rb, line 22
def payload(hash)
  len = hash.length
  case len
  when 32
    params = { md5_hash: hash }
  when 64
    params = { sha256_hash: hash }
  else
    raise ArgumentError("Hash should be MD5 or SHA256")
  end

  post("/payload/", params)
end
signature(signature) click to toggle source
# File lib/urlhaus/api.rb, line 40
def signature(signature)
  post("/signature/", signature: signature)
end
tag(tag) click to toggle source
# File lib/urlhaus/api.rb, line 36
def tag(tag)
  post("/tag/", tag: tag)
end
url(url) click to toggle source
# File lib/urlhaus/api.rb, line 14
def url(url)
  post("/url/", url: url)
end

Private Instance Methods

_host() click to toggle source
# File lib/urlhaus/api.rb, line 50
def _host
  self.class::HOST
end
base_url() click to toggle source
# File lib/urlhaus/api.rb, line 54
def base_url
  self.class::BASE_URL
end
get(path, **params) click to toggle source

Perform a direct GET HTTP request to the REST API.

# File lib/urlhaus/api.rb, line 71
def get(path, **params)
  uri = URI("#{base_url}#{path}")
  uri.query = URI.encode_www_form(params) if params
  req = Net::HTTP::Get.new(uri)
  request req
end
https_options() click to toggle source
# File lib/urlhaus/api.rb, line 87
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

Perform a direct POST HTTP request to the REST API.

# File lib/urlhaus/api.rb, line 79
def post(path, **params)
  uri = URI("#{base_url}#{path}")
  req = Net::HTTP::Post.new(uri)
  req.set_form_data(params) if params

  request req
end
request(req) click to toggle source
# File lib/urlhaus/api.rb, line 58
def request(req)
  Net::HTTP.start(_host, 443, https_options) do |http|
    response = http.request(req)

    if response.code.to_i != 200
      raise Error, "#{response.code}: #{response.body}"
    end

    JSON.parse response.body
  end
end