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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0b8b78b48157b5d06b0b0bd0acd50ebb366d8e47
4
- data.tar.gz: 01121a252416e4d57b23da700657c3282cf69904
3
+ metadata.gz: 51f78d9f07ef6aef69becef2af659d95fe05f971
4
+ data.tar.gz: 35abf0348943973f1b6b962c9bcb2659e289cbd7
5
5
  SHA512:
6
- metadata.gz: cc7ddce6d4efcabe265120454474ca1d26127bb09cc4c5c291199b8d38880d392a5fdb111194913f8e10d25e1fb533813d6383578c045ae4ca300a221a6fbe92
7
- data.tar.gz: 15d570a1ccc39b129975c98087b52de59e81a622d785354b6b8c085577c46f1bfa7e8e1165ce3dfc99498d5863ae60d420284af0967286af564a7e702f12ed46
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
- # SixArm.com » Ruby » <br> XID excellent identifier
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.
@@ -8,16 +8,29 @@ require 'digest/sha2'
8
8
 
9
9
  class XID < String
10
10
 
11
- def initialize(s=nil)
12
- super(s || SecureRandom.hex(16))
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
- Digest::SHA256.new.update(self).to_s
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
- xid = XID.new("foo")
34
- xid.must_equal("foo")
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.3.0
4
+ version: 3.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - SixArm
@@ -10,27 +10,41 @@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0LTsNznUPsoo6e+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-12 00:00:00.000000000 Z
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