sixarm_ruby_xid 3.3.0 → 3.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data/README.md +5 -1
- data/lib/sixarm_ruby_xid.rb +16 -3
- data/test/sixarm_ruby_xid_test.rb +65 -3
- data.tar.gz.sig +0 -0
- metadata +35 -22
- metadata.gz.sig +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 51f78d9f07ef6aef69becef2af659d95fe05f971
|
4
|
+
data.tar.gz: 35abf0348943973f1b6b962c9bcb2659e289cbd7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 87fe6ef0b1dd620118b4486f1ec2b090d720720873b5d521cd45d967791e9b261e5693a1bcc284fd68fce0e3b416120c0fdd4f223193896caabfbf2ed6c3d60d
|
7
|
+
data.tar.gz: 6001f77cdcbac8a470d0f4aa0d2d43311ce8d771d99051863887799fd159d1fd35b114f0dc8a429d27adae35280e9a043bcfe4ec3ff0300093687d9b8f107b87
|
checksums.yaml.gz.sig
CHANGED
Binary file
|
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
#
|
1
|
+
# Ruby » <br> XID excellent identifier
|
2
2
|
|
3
3
|
[![Code Climate](https://codeclimate.com/github/SixArm/xid.png)](https://codeclimate.com/github/SixArm/sixarm_ruby_xid)
|
4
4
|
[![Build Status](https://travis-ci.org/SixArm/xid.png)](https://travis-ci.org/SixArm/sixarm_ruby_xid)
|
@@ -65,8 +65,11 @@ Methods:
|
|
65
65
|
|
66
66
|
* `XID.new`: create an XID and initialize to a random string.
|
67
67
|
* `XID.new(s): create an XID and initialize to a given string.
|
68
|
+
* `XID.valid?`: is this XIDa valid, i.e. the correct format?
|
69
|
+
* `XID#valid?(s)`: is a string a valid XID, i.e. the correct format?
|
68
70
|
* `XID#digest`: return a SHA256 digest as a 64-character string.
|
69
71
|
* `XID.digest(s)`: return a SHA256 digest as a 64-character string.
|
72
|
+
* `XID.parse(s)`: parse any string with enough data to a new XID.
|
70
73
|
|
71
74
|
Notes:
|
72
75
|
|
@@ -123,6 +126,7 @@ Some databases have specialize fields for 128 bit values, such as PostgreSQL and
|
|
123
126
|
|
124
127
|
## Changes
|
125
128
|
|
129
|
+
* 2015-04-01 3.4.0 Add XID.parse and XID.valid?
|
126
130
|
* 2015-02-12 3.3.0 Add XID.digest(s)
|
127
131
|
* 2015-02-12 3.2.0 Add XID.new(s) to initialize with a given string.
|
128
132
|
* 2015-02-12 3.1.0 Add #digest.
|
data/lib/sixarm_ruby_xid.rb
CHANGED
@@ -8,16 +8,29 @@ require 'digest/sha2'
|
|
8
8
|
|
9
9
|
class XID < String
|
10
10
|
|
11
|
-
def initialize(s=nil)
|
12
|
-
|
11
|
+
def initialize(s = nil)
|
12
|
+
s and (XID.valid?(s) or raise ArgumentError)
|
13
|
+
super(s || SecureRandom.hex)
|
14
|
+
end
|
15
|
+
|
16
|
+
def valid?
|
17
|
+
XID.valid?(self)
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.valid?(s)
|
21
|
+
!!(s && s.is_a?(String) && s.size == 32 && s =~ /\A[0-9a-f]{32}\Z/)
|
13
22
|
end
|
14
23
|
|
15
24
|
def digest
|
16
|
-
|
25
|
+
XID.digest(self)
|
17
26
|
end
|
18
27
|
|
19
28
|
def self.digest(s)
|
20
29
|
Digest::SHA256.new.update(s).to_s
|
21
30
|
end
|
22
31
|
|
32
|
+
def self.parse(s)
|
33
|
+
XID.new(s.downcase.gsub(/[^0-9a-f]/,'')[0...32])
|
34
|
+
end
|
35
|
+
|
23
36
|
end
|
@@ -27,15 +27,57 @@ describe XID do
|
|
27
27
|
|
28
28
|
end
|
29
29
|
|
30
|
-
describe "with initialize string" do
|
30
|
+
describe "with valid initialize string" do
|
31
31
|
|
32
32
|
it "sets" do
|
33
|
-
|
34
|
-
xid.
|
33
|
+
s = "1cf2a839c0890bb9be7e9d56b7405a54"
|
34
|
+
xid = XID.new(s)
|
35
|
+
xid.must_equal(s)
|
35
36
|
end
|
36
37
|
|
37
38
|
end
|
38
39
|
|
40
|
+
describe "with invalid initialize string" do
|
41
|
+
|
42
|
+
it "raises" do
|
43
|
+
proc { XID.new("invalid") }.must_raise ArgumentError
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
49
|
+
|
50
|
+
describe ".valid" do
|
51
|
+
|
52
|
+
describe "with valid string" do
|
53
|
+
it "is true" do
|
54
|
+
xid = XID.new("c3d010bbfec046f59c7fe843d32dab32")
|
55
|
+
xid.valid?.must_equal true
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
describe "with invalid string" do
|
60
|
+
it "never gets called because it fails to create an xid" do
|
61
|
+
proc { XID.new("abc") }.must_raise ArgumentError
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
end
|
66
|
+
|
67
|
+
describe "#valid" do
|
68
|
+
|
69
|
+
describe "with valid string" do
|
70
|
+
it "is true" do
|
71
|
+
XID.valid?("c3d010bbfec046f59c7fe843d32dab32").must_be_same_as true
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
describe "with invalid string" do
|
76
|
+
it "is false" do
|
77
|
+
XID.valid?("abc").must_be_same_as false
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
39
81
|
end
|
40
82
|
|
41
83
|
describe "#digest" do
|
@@ -55,4 +97,24 @@ describe XID do
|
|
55
97
|
|
56
98
|
end
|
57
99
|
|
100
|
+
describe ".parse" do
|
101
|
+
|
102
|
+
it "converts to lower case" do
|
103
|
+
XID.parse("C3D010BBFEC046F59C7FE843D32DAB32").must_equal "c3d010bbfec046f59c7fe843d32dab32"
|
104
|
+
end
|
105
|
+
|
106
|
+
it "deletes non-hex characters" do
|
107
|
+
XID.parse("!!c3d010bb-fec0-46f59c7f-e843d32dab32!!").must_equal "c3d010bbfec046f59c7fe843d32dab32"
|
108
|
+
end
|
109
|
+
|
110
|
+
it "chops at 32 characters" do
|
111
|
+
XID.parse("c3d010bbfec046f59c7fe843d32dab32aaaaaaaaa").must_equal "c3d010bbfec046f59c7fe843d32dab32"
|
112
|
+
end
|
113
|
+
|
114
|
+
it "raises if the string is invalid" do
|
115
|
+
proc { XID.parse("abc") }.must_raise ArgumentError
|
116
|
+
end
|
117
|
+
|
118
|
+
end
|
119
|
+
|
58
120
|
end
|
data.tar.gz.sig
CHANGED
Binary file
|
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
|
+
version: 3.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- SixArm
|
@@ -10,27 +10,41 @@ bindir: bin
|
|
10
10
|
cert_chain:
|
11
11
|
- |
|
12
12
|
-----BEGIN CERTIFICATE-----
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
13
|
+
MIIGCTCCA/GgAwIBAgIJAK3igyLv2hNNMA0GCSqGSIb3DQEBBQUAMGAxCzAJBgNV
|
14
|
+
BAYTAlVTMRMwEQYDVQQIEwpDYWxpZm9ybmlhMRYwFAYDVQQHEw1TYW4gRnJhbmNp
|
15
|
+
c2NvMQ8wDQYDVQQKEwZTaXhBcm0xEzARBgNVBAMTCnNpeGFybS5jb20wHhcNMTUw
|
16
|
+
MzE0MjA0MTE5WhcNMTcxMjA4MjA0MTE5WjBgMQswCQYDVQQGEwJVUzETMBEGA1UE
|
17
|
+
CBMKQ2FsaWZvcm5pYTEWMBQGA1UEBxMN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wpDYWxpZm9ybmlhMRYwFAYDVQQHEw1TYW4gRnJhbmNpc2NvMQ8wDQYD
|
33
|
+
VQQKEwZTaXhBcm0xEzARBgNVBAMTCnNpeGFybS5jb22CCQCt4oMi79oTTTAMBgNV
|
34
|
+
HRMEBTADAQH/MA0GCSqGSIb3DQEBBQUAA4ICAQCYcCnvJpEhpo5mdVM8JDUuUZFt
|
35
|
+
qP2Kvj9J6tqugO+cuUF2S/ro4gdEQhl7Gv6+DCWHd0FQWJBSXMsZ9a6RhFGAcE5C
|
36
|
+
egK706Gh40yNeobd1aoUh+Pn17kYH2WSBRC+KsIvhZaAnra/1JPZItoge64GS+lM
|
37
|
+
PJJbVrtSati++s39wnss1QlMy9TXoesmR8vqsOU0XdCnK5hOun5RA8SYDWLffsfG
|
38
|
+
E3hvCg4C5viEkPY0YdC0KMSqs5kIA2nCUiqpkwIOa36rVEwiKiU7OCfE3u3baDpL
|
39
|
+
FlfMBznZKOdxDFAmNaxvXBe2XeTzrZPvJtnNLWL6K4LaBHhq3bBdh1Hge0iMkpQ7
|
40
|
+
RTIGlfhlIFkMV3wT0LTsNznUPsoo6e+IW/tDrk23mrNRY6QynTETb+QVIevuzD9m
|
41
|
+
Drcxp/zlVhud+a0ezdnyNvF520arJWvqA4GrOo8F+TT2vVrjscgYjiVGdSq+8wQv
|
42
|
+
Efa5jhe8QwG7R1rdpMMP5yBSAqWuFBczMveX5j4rp9Ifw5/XsZbgwcmdm26bjhzh
|
43
|
+
w2grAHIhvR9mztm6uXQlZhv1fu3P+RWHDSYhnZSCJSCdxPzQJ1mG5T5ahiL3HvCZ
|
44
|
+
2AC9FOGkybW6DJEFSFFMlNk0IILsa/gNp8ufGuTVLWF9FUUdMNK+TMbghnifT8/1
|
45
|
+
n+ES/gQPOnvmVkLDGw==
|
32
46
|
-----END CERTIFICATE-----
|
33
|
-
date: 2015-02
|
47
|
+
date: 2015-04-02 00:00:00.000000000 Z
|
34
48
|
dependencies: []
|
35
49
|
description: XID generates a new string suitable for security, authentication, tracking,
|
36
50
|
etc.
|
@@ -76,4 +90,3 @@ specification_version: 4
|
|
76
90
|
summary: SixArm.com » Ruby » XID excellent identifier
|
77
91
|
test_files:
|
78
92
|
- test/sixarm_ruby_xid_test.rb
|
79
|
-
has_rdoc: true
|
metadata.gz.sig
CHANGED
Binary file
|