Skip to content

Commit

Permalink
fixup! Add CloudFlareProtectedEmail class
Browse files Browse the repository at this point in the history
  • Loading branch information
Oliver Denman committed Jun 5, 2017
1 parent 2d0f184 commit 1488b48
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions lib/cloudflare_protected_email_address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,31 @@ def initialize(obfuscated)
end

def human_readable
return CGI.unescape(escaped_email_address) if CGI.unescape(escaped_email_address).include?('@')
obfuscated
return obfuscated unless unescaped.include?('@')
unescaped
end

private

attr_accessor :obfuscated

def components
obfuscated.scan(/.{2}/).map(&:hex)
end

def obfuscated_characters
obfuscated.scan(/.{2}/).drop(1).map(&:hex)
components.drop(1)
end

def key
obfuscated[0..1].hex
components.first
end

def escaped
obfuscated_characters.map { |char| (char ^ key).to_s(16).prepend('%') }.join
end

def escaped_email_address
obfuscated_characters.reduce('') do |email, encoded_character|
email + (encoded_character ^ key).to_s(16).prepend('%')
end
def unescaped
CGI.unescape(escaped)
end
end

0 comments on commit 1488b48

Please sign in to comment.