ruby_xid 1.0.5 → 1.0.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/ruby_xid.rb +24 -24
- data/lib/xid/base32.rb +9 -9
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 57563aac1f71f9bc79cbaf1431aee6a660499b4ccab42f87570701e585017822
|
4
|
+
data.tar.gz: c04e68b3bb859fdff627fbda865ab3290ba03d197eadcd11724c0e807850f8cc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 33d37db8dd796f96005337236f2552d5a896c390e7a92378a4cc174832ddb7cf75542627ded93d8f2e51d81f5651fc982d02adcecdae7db2fc40d0995eb440e6
|
7
|
+
data.tar.gz: f64ca53e6f4b0f0ca2a43827e6c8f2c5f1b6cd0dcba94b87d2c26f1c44e040f0c63e7d8ed90e9f1cf9b96571f6e313fd93bbf12ae49c1db693ad315f13fb5311
|
data/lib/ruby_xid.rb
CHANGED
@@ -7,23 +7,22 @@ require 'date'
|
|
7
7
|
class Xid
|
8
8
|
|
9
9
|
RAW_LEN = 12
|
10
|
-
TRIM_LEN = 20
|
11
10
|
|
12
11
|
@@generator = nil
|
13
12
|
|
14
13
|
def initialize(id = nil)
|
15
14
|
@@generator ||= Generator.new(init_rand_int, real_machine_id)
|
16
|
-
@
|
15
|
+
@value = id ? id : @@generator.generate_data.unpack('C12')
|
17
16
|
end
|
18
17
|
|
19
18
|
def next
|
20
|
-
@string =
|
21
|
-
@
|
19
|
+
@string = nil
|
20
|
+
@value = @@generator.generate_data.unpack('C12')
|
22
21
|
string
|
23
22
|
end
|
24
23
|
|
25
24
|
def value
|
26
|
-
@value
|
25
|
+
@value
|
27
26
|
end
|
28
27
|
|
29
28
|
def pid
|
@@ -58,26 +57,9 @@ class Xid
|
|
58
57
|
string
|
59
58
|
end
|
60
59
|
|
61
|
-
def string
|
62
|
-
# type: () -> str
|
63
|
-
@string ||= Base32.b32encode(value)[0..TRIM_LEN - 1]
|
64
|
-
end
|
65
|
-
|
66
60
|
def bytes
|
67
61
|
# type: () -> str
|
68
|
-
@
|
69
|
-
end
|
70
|
-
|
71
|
-
def init_rand_int
|
72
|
-
# type: () -> int
|
73
|
-
SecureRandom.random_number(16_777_215)
|
74
|
-
end
|
75
|
-
|
76
|
-
def real_machine_id
|
77
|
-
# type: () -> int
|
78
|
-
Digest::MD5.digest(Socket.gethostname).unpack('N')[0]
|
79
|
-
rescue
|
80
|
-
init_rand_int
|
62
|
+
@value.map(&:chr).join('')
|
81
63
|
end
|
82
64
|
|
83
65
|
def ==(other_xid)
|
@@ -108,6 +90,24 @@ class Xid
|
|
108
90
|
Object.const_get(name).new(val)
|
109
91
|
end
|
110
92
|
|
93
|
+
private
|
94
|
+
def string
|
95
|
+
# type: () -> str
|
96
|
+
@string ||= Base32.b32encode(value)
|
97
|
+
end
|
98
|
+
|
99
|
+
def init_rand_int
|
100
|
+
# type: () -> int
|
101
|
+
SecureRandom.random_number(16_777_215)
|
102
|
+
end
|
103
|
+
|
104
|
+
def real_machine_id
|
105
|
+
# type: () -> int
|
106
|
+
Digest::MD5.digest(Socket.gethostname).unpack('N')[0]
|
107
|
+
rescue
|
108
|
+
init_rand_int
|
109
|
+
end
|
110
|
+
|
111
111
|
# Xid Generator
|
112
112
|
class Generator
|
113
113
|
attr_accessor :value
|
@@ -119,7 +119,7 @@ class Xid
|
|
119
119
|
@machine_id = machine_id
|
120
120
|
end
|
121
121
|
|
122
|
-
def
|
122
|
+
def generate_data
|
123
123
|
# () -> str
|
124
124
|
@mutex.synchronize do
|
125
125
|
@rand_int += 1
|
data/lib/xid/base32.rb
CHANGED
@@ -3,6 +3,7 @@ class Xid::Base32
|
|
3
3
|
|
4
4
|
# 0123456789abcdefghijklmnopqrstuv - Used for encoding
|
5
5
|
ENCODE_HEX = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v"].freeze
|
6
|
+
TRIM_LEN = 20
|
6
7
|
|
7
8
|
# Start class methods
|
8
9
|
class << self
|
@@ -12,18 +13,16 @@ class Xid::Base32
|
|
12
13
|
end
|
13
14
|
|
14
15
|
def b32encode(src)
|
15
|
-
encode(src
|
16
|
+
encode(src)
|
16
17
|
end
|
17
18
|
|
18
19
|
def b32decode(src)
|
19
20
|
decode(src, ENCODE_HEX)
|
20
21
|
end
|
21
22
|
|
22
|
-
def encode(src_str
|
23
|
-
|
24
|
-
|
25
|
-
dst = []
|
26
|
-
while src_str && !src_str.empty?
|
23
|
+
def encode(src_str)
|
24
|
+
dst = ''
|
25
|
+
3.times do |i|
|
27
26
|
src_len = src_str.length
|
28
27
|
next_byte = Array.new(8, 0)
|
29
28
|
|
@@ -50,13 +49,14 @@ class Xid::Base32
|
|
50
49
|
next_byte[0] = src_str[0] >> 3
|
51
50
|
end
|
52
51
|
|
52
|
+
src_str = src_str[5..src_str.length]
|
53
|
+
next_byte = next_byte[0..3] if i == 2
|
53
54
|
next_byte.each do |nb|
|
54
|
-
dst
|
55
|
+
dst += ENCODE_HEX[nb]
|
55
56
|
end
|
56
|
-
src_str = src_str[5..src_str.length]
|
57
57
|
end
|
58
58
|
|
59
|
-
dst
|
59
|
+
dst
|
60
60
|
end
|
61
61
|
|
62
62
|
def decode(src, str_map)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruby_xid
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Valarpiraichandran
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-12-
|
11
|
+
date: 2019-12-17 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description:
|
14
14
|
email: a.valarpirai@gmail.com
|