summaryrefslogtreecommitdiff
path: root/lib/nickserver/wkd/url.rb
blob: 759222d6bc093c1d449219151a1e9966f17bfc02 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
require 'digest/sha1'
require 'zbase32'

module Nickserver::Wkd
  # The url to lookup the given email address in the web key directory.
  class Url
    def initialize(email)
      @domain = email.domain.downcase
      @local_part = email.local_part.downcase
    end

    def to_s
      "https://#{domain}/.well-known/openpgpkey/hu/#{encoded_digest}"
    end

    # needed to compare when used as an arg calling a test mock
    def ==(other)
      self.to_s == other.to_s
    end

    protected

    attr_reader :domain, :local_part

    def encoded_digest
      ZBase32.encode32(digest.to_i(16).to_s(2))
    end

    def digest
      Digest::SHA1.hexdigest local_part
    end
  end
end