Skip to content

Commit

Permalink
Merge pull request #81 from djberg96/digest_uuid
Browse files Browse the repository at this point in the history
Extend the Digest::UUID module with the clean method
  • Loading branch information
Fryguy authored Jul 9, 2020
2 parents 079d3c9 + d252b90 commit 057042e
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/more_core_extensions/all.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require 'more_core_extensions/core_ext/array'
require 'more_core_extensions/core_ext/benchmark'
require 'more_core_extensions/core_ext/class'
require 'more_core_extensions/core_ext/digest'
require 'more_core_extensions/core_ext/hash'
require 'more_core_extensions/core_ext/math'
require 'more_core_extensions/core_ext/module'
Expand Down
1 change: 1 addition & 0 deletions lib/more_core_extensions/core_ext/digest.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require 'more_core_extensions/core_ext/digest/uuid'
26 changes: 26 additions & 0 deletions lib/more_core_extensions/core_ext/digest/uuid.rb
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
34 changes: 34 additions & 0 deletions spec/core_ext/digest/uuid_spec.rb
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

0 comments on commit 057042e

Please sign in to comment.