sixarm_ruby_xid 3.4.2 → 4.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2f5b6519a9ac4fad507d584b4e79e3a8acd07052
4
- data.tar.gz: ae6ae65995d3091262ce3444951ede3437335a4e
3
+ metadata.gz: 0f6d3accebde3354b4b649117469e7e7fd67a7aa
4
+ data.tar.gz: 05feb7ba4b25e7f485425a37fab9a803c0c0099d
5
5
  SHA512:
6
- metadata.gz: ddce1beb9a306f2b3f5c95a836eff8aba4aa12a723f91102f6cde20cd353990dc028b6d06b3a5515f0738195dffa7fa1449a997080f0954b4deae3fe354203bf
7
- data.tar.gz: 10012f4d5b4a0f1a51e16a35ab2a1c0f98dd80903cfff4cac3709c68bc13b5187f866e8f2015da018c85cdb639d764cce4f141c36aaa4f11ae39fc6eb616bb6a
6
+ metadata.gz: d1af7e6bd0d539cd3a2a5214140d56774183b7b6fd63f273a9664f47eafeee62af87210340e5a54d0fae7c1dfa0ef7ec281bdbf06f7d9657fbefd297be68e547
7
+ data.tar.gz: e1a67f1e6d6c06e49761cf25b84f2b2bb3e94ee095121d06b6c9bfa0f1a3549aa93462c8605fda30bc779943ae188992d63e25830c176d8b4cd44c5be6f8e18a
checksums.yaml.gz.sig CHANGED
Binary file
data.tar.gz.sig CHANGED
Binary file
data/Rakefile CHANGED
@@ -9,9 +9,3 @@ end
9
9
 
10
10
  task :default => [:test]
11
11
  task :default => [:test]
12
- task :default => [:test]
13
- task :default => [:test]
14
- task :default => [:test]
15
- task :default => [:test]
16
- task :default => [:test]
17
- task :default => [:test]
@@ -6,34 +6,19 @@ XID class.
6
6
  require "securerandom"
7
7
  require "digest/sha2"
8
8
 
9
- class XID < String
9
+ class XID
10
10
 
11
- # Initialize a new XID, optionally with a string.
11
+ # Generate a new XID string.
12
12
  #
13
13
  # Example:
14
14
  #
15
- # xid = XID.new
15
+ # string = XID.generate
16
+ # #=> "4bb88af57d4ddc224fecad688442423d"
16
17
  #
17
- # xid = XID.new("4bb88af57d4ddc224fecad688442423d")
18
+ # Return: [String] A new XID string
18
19
  #
19
- # Return: [XID] A new XID
20
- #
21
- def initialize(s = nil)
22
- s and (XID.valid?(s) or raise ArgumentError)
23
- super(s || SecureRandom.hex)
24
- end
25
-
26
- # Is this XID valid?
27
- #
28
- # Example:
29
- #
30
- # xid = XID.new
31
- # xid.valid? #=> true
32
- #
33
- # Return: [true/false]
34
- #
35
- def valid?
36
- XID.valid?(self)
20
+ def self.generate
21
+ SecureRandom.hex
37
22
  end
38
23
 
39
24
  # Is a given XID string valid?
@@ -45,57 +30,30 @@ class XID < String
45
30
  #
46
31
  # Return: [true/false]
47
32
  #
48
- def self.valid?(s)
49
- !!(s && s.is_a?(String) && s.size == 32 && s =~ /\A[0-9a-f]{32}\Z/)
50
- end
51
-
52
- # Compute the XID digest of this XID.
53
- #
54
- # This is useful for proving knowledge of the XID,
55
- # without needed to expose the XID.
56
- #
57
- # Example:
58
- #
59
- # xid = XID.new("4bb88af57d4ddc224fecad688442423d")
60
- # xid.digest
61
- # #=> 4d648be0ca9dbb9e3a1b6b30b9d13c79ecbc0818dfca4125cc7961e32d61112c
62
- #
63
- def digest
64
- XID.digest(self)
65
- end
66
-
67
- # Compute the XID digest of a given string.
68
- #
69
- # This is useful for proving knowledge of the XID,
70
- # without needed to expose the XID.
71
- #
72
- # Example:
73
- #
74
- # xid = XID.new("4bb88af57d4ddc224fecad688442423d")
75
- # xid.digest
76
- # #=> 4d648be0ca9dbb9e3a1b6b30b9d13c79ecbc0818dfca4125cc7961e32d61112c
77
- #
78
- def self.digest(s)
79
- Digest::SHA256.new.update(s).to_s
33
+ def self.valid?(string)
34
+ !!(string && string.is_a?(String) && string.size == 32 && string =~ /\A[0-9a-f]{32}\z/)
80
35
  end
81
36
 
82
- # Parse a string to an XID.
37
+ # Parse any object to an XID string.
83
38
  #
84
- # This does three steps:
39
+ # This does these steps:
85
40
  #
86
- # * downcase
87
- # * delete any non-hex characters
88
- # * take the first 32 characters
41
+ # * Convert the object to a string by calling `#to_s`.
42
+ # * Convert the string to lower case by calling `#downcase`.
43
+ # * Delete any non-hex characters.
44
+ # * Take the first 32 characters.
89
45
  #
90
46
  # Example:
91
47
  #
92
- # xid = XID.parse("***FFAD30A1-BE5E-B511-9ED8-976CAB0281B6***")
48
+ # string = XID.parse("***FFAD30A1-BE5E-B511-9ED8-976CAB0281B6***")
93
49
  # #=> "ffad30a1be5eb5119ed8976cab0281b6"
94
50
  #
95
51
  # Return: [XID] A new XID
96
52
  #
97
- def self.parse(s)
98
- XID.new(s.downcase.gsub(/[^0-9a-f]/,'')[0...32])
53
+ def self.parse(object)
54
+ xid = object.to_s.downcase.gsub(/[^0-9a-f]/,'')[0...32]
55
+ raise ArgumentError if !XID.valid?(xid)
56
+ xid
99
57
  end
100
58
 
101
59
  end
@@ -8,14 +8,14 @@ describe XID do
8
8
  describe "with initialize defaults" do
9
9
 
10
10
  it "is a string with the correct length and characters" do
11
- xid = XID.new
11
+ xid = XID.generate
12
12
  xid.must_match /\A[0-9a-f]{32}\z/
13
13
  end
14
14
 
15
15
  it "is always different" do
16
16
  seen = Set.new
17
17
  99999.times.each{
18
- xid = XID.new
18
+ xid = XID.generate
19
19
  seen.include?(xid).must_equal false
20
20
  seen.add(xid)
21
21
  }
@@ -23,41 +23,6 @@ describe XID do
23
23
 
24
24
  end
25
25
 
26
- describe "with valid initialize string" do
27
-
28
- it "sets" do
29
- s = "1cf2a839c0890bb9be7e9d56b7405a54"
30
- xid = XID.new(s)
31
- xid.must_equal(s)
32
- end
33
-
34
- end
35
-
36
- describe "with invalid initialize string" do
37
-
38
- it "raises" do
39
- proc { XID.new("invalid") }.must_raise ArgumentError
40
- end
41
-
42
- end
43
-
44
- end
45
-
46
- describe ".valid" do
47
-
48
- describe "with valid string" do
49
- it "is true" do
50
- xid = XID.new("c3d010bbfec046f59c7fe843d32dab32")
51
- xid.valid?.must_equal true
52
- end
53
- end
54
-
55
- describe "with invalid string" do
56
- it "never gets called because it fails to create an xid" do
57
- proc { XID.new("abc") }.must_raise ArgumentError
58
- end
59
- end
60
-
61
26
  end
62
27
 
63
28
  describe "#valid" do
@@ -76,25 +41,15 @@ describe XID do
76
41
 
77
42
  end
78
43
 
79
- describe "#digest" do
80
-
81
- it "digests" do
82
- xid = XID.new
83
- xid.digest.must_match /\A[0-9a-f]{64}\z/
84
- end
85
-
86
- end
87
-
88
- describe ".digest" do
44
+ describe ".parse" do
89
45
 
90
- it "digests" do
91
- XID.digest("foo").must_match /\A[0-9a-f]{64}\z/
46
+ it "converts objects to strings" do
47
+ obj = Minitest::Mock.new
48
+ obj.expect(:to_s, "!!C3D010BBFEC046F59C7FE843D32DAB32!!")
49
+ XID.parse(obj).must_equal "c3d010bbfec046f59c7fe843d32dab32"
50
+ obj.verify
92
51
  end
93
52
 
94
- end
95
-
96
- describe ".parse" do
97
-
98
53
  it "converts to lower case" do
99
54
  XID.parse("C3D010BBFEC046F59C7FE843D32DAB32").must_equal "c3d010bbfec046f59c7fe843d32dab32"
100
55
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sixarm_ruby_xid
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.4.2
4
+ version: 4.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - SixArm
@@ -44,7 +44,7 @@ cert_chain:
44
44
  2AC9FOGkybW6DJEFSFFMlNk0IILsa/gNp8ufGuTVLWF9FUUdMNK+TMbghnifT8/1
45
45
  n+ES/gQPOnvmVkLDGw==
46
46
  -----END CERTIFICATE-----
47
- date: 2015-07-19 00:00:00.000000000 Z
47
+ date: 2015-08-06 00:00:00.000000000 Z
48
48
  dependencies:
49
49
  - !ruby/object:Gem::Dependency
50
50
  name: minitest
metadata.gz.sig CHANGED
Binary file