class Apullo::Fingerprint::Favicon

Attributes

uri[R]

Public Class Methods

new(url) click to toggle source
# File lib/apullo/fingerprints/favicon.rb, line 12
def initialize(url)
  @uri = URI(url)
end

Public Instance Methods

results() click to toggle source
# File lib/apullo/fingerprints/favicon.rb, line 16
def results
  data = b64_favicon_data
  return {} unless data

  hash = Hash.new(data.b)
  {
    md5: hash.md5,
    mmh3: hash.mmh3,
    sha1: hash.sha1,
    sha256: hash.sha256,
    meta: {
      url: uri.to_s,
    }
  }
end

Private Instance Methods

b64_favicon_data() click to toggle source
# File lib/apullo/fingerprints/favicon.rb, line 34
def b64_favicon_data
  @b64_favicon_data ||= [].tap do |out|
    data = get(uri.path)
    next unless data

    b64 = Base64.strict_encode64(data)
    out << b64.chars.each_slice(76).map(&:join).join("\n") + "\n"
  end.first
end
build_http() click to toggle source
# File lib/apullo/fingerprints/favicon.rb, line 55
def build_http
  if uri.scheme == "http"
    Net::HTTP.start(uri.host, uri.port)
  else
    Net::HTTP.start(uri.host, uri.port, use_ssl: true)
  end
end
get(path) click to toggle source
# File lib/apullo/fingerprints/favicon.rb, line 44
def get(path)
  http = build_http
  path = path.empty? ? "/" : path
  request = Net::HTTP::Get.new(path)
  response = http.request(request)

  response.code.to_i == 200 ? response.body : nil
rescue Errno::ECONNREFUSED, Net::HTTPError, OpenSSL::OpenSSLError, Timeout::Error => _e
  nil
end