-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add CloudflareProtectedEmailAddress #6
base: master
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'cgi' | ||
|
||
class CloudflareProtectedEmailAddress | ||
def initialize(obfuscated) | ||
@obfuscated = obfuscated | ||
end | ||
|
||
def human_readable | ||
return obfuscated unless unescaped.include?('@') | ||
unescaped | ||
end | ||
|
||
private | ||
|
||
attr_accessor :obfuscated | ||
|
||
def components | ||
obfuscated.scan(/.{2}/).map(&:hex) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm curious here why you're using |
||
end | ||
|
||
def obfuscated_characters | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is actually quite a misleading name too, as it returns integers, not characters |
||
components.drop(1) | ||
end | ||
|
||
def key | ||
components.first | ||
end | ||
|
||
def escaped | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this would be easier to follow if it actually mentioned percent encoding (or hex encoding) somewhere. Ideally that would be obvious from the name of the method, though if that's not sensible, then a comment might be called for. |
||
obfuscated_characters.map { |char| (char ^ key).to_s(16).prepend('%') }.join | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Now that I've worked out what's actually going on here, I'm not entirely sure that
|
||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
(not sure if |
||
|
||
def unescaped | ||
CGI.unescape(escaped) | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative './test_helper' | ||
require_relative '../lib/cloudflare_protected_email_address' | ||
|
||
describe CloudflareProtectedEmailAddress do | ||
it 'will make an obfuscated email address human readable' do | ||
CloudflareProtectedEmailAddress.new('b3d2dfd7dcc5d6c1d2f3d7dac3c6c7d2d7dcc09dd4dcc59dc3ca') | ||
.human_readable | ||
.must_equal '[email protected]' | ||
end | ||
|
||
it 'returns the initialization email when it is not obfuscated' do | ||
CloudflareProtectedEmailAddress.new('[email protected]') | ||
.human_readable | ||
.must_equal '[email protected]' | ||
end | ||
|
||
it 'returns the original string when the initialization string is not an obfuscated email' do | ||
CloudflareProtectedEmailAddress.new('xyz789') | ||
.human_readable | ||
.must_equal 'xyz789' | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
components
is a bit too overloaded a term for me to be entirely comfortable with it here — I'm not sure it really conveys what's actually going on, and could be misleading. Is there a better name you can think of?