-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #81 from djberg96/digest_uuid
Extend the Digest::UUID module with the clean method
- Loading branch information
Showing
4 changed files
with
62 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
require 'more_core_extensions/core_ext/digest/uuid' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
require 'active_support/core_ext/digest/uuid' | ||
|
||
module Digest | ||
module UUID | ||
UUID_REGEX_FORMAT = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/.freeze | ||
|
||
# Takes a UUID string of varying formats and cleans it. It will strip invalid characters, | ||
# such as leading and trailing brackets as well as whitespace. The result is a lowercased, | ||
# canonical UUID string. | ||
# | ||
# If the +guid+ argument is nil or blank, then nil is returned. If the +guid+ is already | ||
# clean, then no additional cleaning occurs, and it is returned as-is. | ||
# | ||
# @param guid [String] A string that should more or less represent a UUID. | ||
# @return [String] A lowercase v4 UUID string stripped of any extraneous characters. | ||
# | ||
def self.clean(guid) | ||
return nil if guid.nil? | ||
g = guid.to_s.downcase | ||
return nil if g.strip.empty? | ||
return g if g.length == 36 && g =~ UUID_REGEX_FORMAT | ||
g.delete!('^0-9a-f') | ||
g.sub!(/^([0-9a-f]{8})([0-9a-f]{4})([0-9a-f]{4})([0-9a-f]{4})([0-9a-f]{12})$/, '\1-\2-\3-\4-\5') | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
describe Digest::UUID do | ||
context "clean" do | ||
let(:expected) { '01234567-89ab-cdef-abcd-ef0123456789' } | ||
|
||
it "handles a valid lowercase guid" do | ||
expect(described_class.clean('01234567-89ab-cdef-abcd-ef0123456789')).to eq(expected) | ||
end | ||
|
||
it "handles a Microsoft-style guid" do | ||
expect(described_class.clean('{01234567-89ab-cdef-abcd-ef0123456789}')).to eq(expected) | ||
end | ||
|
||
it "handles a valid uppercase guid" do | ||
expect(described_class.clean('01234567-89AB-CDEF-abcd-ef0123456789')).to eq(expected) | ||
end | ||
|
||
it "handles a valid guid with an invalid structure" do | ||
expect(described_class.clean('01 23 45 67 89 AB CD EF-AB CD EF 01 23 45 67 89')).to eq(expected) | ||
end | ||
|
||
it "handles an explicit nil or blank string" do | ||
expect(described_class.clean(nil)).to be_nil | ||
expect(described_class.clean('')).to be_nil | ||
end | ||
|
||
it "handles invalid guids" do | ||
expect(described_class.clean('sdkjfLSDLK')).to be_nil | ||
expect(described_class.clean('sdkjfLSD-LKJF-8367-41df-FLKD209alkfd')).to be_nil | ||
expect(described_class.clean('01234567-89AB-CDEF-abcd-efgggggggggg')).to be_nil | ||
expect(described_class.clean('01234567-89AB-CDEF-abcd-ef0123456789a')).to be_nil | ||
expect(described_class.clean('01234567-89AB-CDEF-abcd-ef012345678')).to be_nil | ||
end | ||
end | ||
end |