ruby_xid 1.0.1 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/ruby_xid.rb +18 -21
  3. data/lib/xid/base32.rb +2 -22
  4. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 52284a496e3475218d579654a6d484b307dceec8e6aae766ef954aa466681f43
4
- data.tar.gz: 6f324b726dbf0bc3bcf8181586115df1aa32c35f405ef1da79f60991f87ca5bd
3
+ metadata.gz: dc05ca0b9b3c67ac29d68ea4b957b089ee503a2090b95747cfe9514c101940ed
4
+ data.tar.gz: 53518ae32bc8ecffe4e0f32b6d02284f447df4905d4cce72d84375a601a4950b
5
5
  SHA512:
6
- metadata.gz: 7f874017acb11d16aeb1edce7eb1786ff7baf5b2f43dd79369d0d9c91017d5d38fd6feb755ada4c763ecd20d7be8975f9e9ece4883000aa05b0f9c4d0549ec11
7
- data.tar.gz: b2e8f5b3a8b895598e811a08112525bdd6b1f79e7eac439dd13c8e884fa891c4bc4bbe1c270c45e44a0b6b5e1bee494c2dc67f8be1a8fae0342e1b21dfbb9283
6
+ metadata.gz: 24fb58cbe29a05bb81fdfb8db83a61d5c90dde101aa11a6e357331e9b23135201128b27a7fd2849760d71d00dca99d67002988788299633dae3ec696513e725a
7
+ data.tar.gz: 691f01a19ad332bf78a64a384be0a583e729a0bc058caefa5deffc599f6f8f6b1c4439cfe526cd026a6738ee3cd01e3efd7dacd2883cf62194e3fa2bdf5c3c84
data/lib/ruby_xid.rb CHANGED
@@ -19,7 +19,7 @@ class Xid
19
19
  # Decoded array
20
20
  @value = id
21
21
  else
22
- @value = generate_xid
22
+ generate_xid
23
23
  end
24
24
  end
25
25
 
@@ -55,7 +55,7 @@ class Xid
55
55
  def string
56
56
  # type: () -> str
57
57
  byte_value = bytes
58
- Base32.b32encode(byte_value).downcase[0..TRIM_LEN - 1]
58
+ Base32.b32encode(byte_value)[0..TRIM_LEN - 1]
59
59
  end
60
60
 
61
61
  def bytes
@@ -104,10 +104,8 @@ class Xid
104
104
  def real_machine_id
105
105
  # type: () -> List[int]
106
106
  hostname = Socket.gethostname.encode('utf-8')
107
- md5 = Digest::MD5.new
108
- md5 << hostname
109
- val = md5.digest[0..3]
110
- val.scan(/.{1}/m).map(&:ord)
107
+ val = Digest::MD5.digest(hostname)[0..3]
108
+ val.chars.map(&:ord)
111
109
  rescue
112
110
  SecureRandom.hex(3).scan(/.{2}/m).map(&:hex)
113
111
  end
@@ -115,30 +113,29 @@ class Xid
115
113
  def generate_xid
116
114
  # type: () -> List[int]
117
115
  now = Time.now.to_i
118
- id = [0] * RAW_LEN
116
+ @value = Array.new(RAW_LEN, 0)
119
117
 
120
- id[0] = (now >> 24) & 0xff
121
- id[1] = (now >> 16) & 0xff
122
- id[2] = (now >> 8) & 0xff
123
- id[3] = now & 0xff
118
+ @value[0] = (now >> 24) & 0xff
119
+ @value[1] = (now >> 16) & 0xff
120
+ @value[2] = (now >> 8) & 0xff
121
+ @value[3] = now & 0xff
124
122
 
125
- id[4] = machine_id[0]
126
- id[5] = machine_id[1]
127
- id[6] = machine_id[2]
123
+ @value[4] = machine_id[0]
124
+ @value[5] = machine_id[1]
125
+ @value[6] = machine_id[2]
128
126
 
129
- id[7] = (@pid >> 8) & 0xff
130
- id[8] = @pid & 0xff
127
+ @value[7] = (@pid >> 8) & 0xff
128
+ @value[8] = @pid & 0xff
131
129
 
132
130
  @mutex.synchronize do
133
131
  @rand_int += 1
134
132
  end
135
- i = @rand_int
136
133
 
137
- id[9] = (i >> 16) & 0xff
138
- id[10] = (i >> 8) & 0xff
139
- id[11] = i & 0xff
134
+ @value[9] = (@rand_int >> 16) & 0xff
135
+ @value[10] = (@rand_int >> 8) & 0xff
136
+ @value[11] = @rand_int & 0xff
140
137
 
141
- id
138
+ @value
142
139
  end
143
140
  end
144
141
 
data/lib/xid/base32.rb CHANGED
@@ -12,7 +12,7 @@ class Xid::Base32
12
12
  end
13
13
 
14
14
  def b32encode(src)
15
- src = src.scan(/.{1}/m).map(&:ord)
15
+ src = src.chars.map(&:ord)
16
16
  encode(src, ENCODE_HEX)
17
17
  end
18
18
 
@@ -24,7 +24,6 @@ class Xid::Base32
24
24
  return '' if src_str.empty?
25
25
 
26
26
  dst = []
27
- src_len = 0
28
27
  while src_str && !src_str.empty?
29
28
  src_len = src_str.length
30
29
  next_byte = [0] * 8
@@ -58,19 +57,6 @@ class Xid::Base32
58
57
  src_str = src_str[5..src_str.length]
59
58
  end
60
59
 
61
- dst[-1] = '=' if src_len < 5
62
- if src_len < 4
63
- dst[-2] = '='
64
- dst[-3] = '='
65
- end
66
- if src_len < 3
67
- dst[-4] = '='
68
- end
69
- if src_len < 2
70
- dst[-5] = '='
71
- dst[-6] = '='
72
- end
73
-
74
60
  dst.join('')
75
61
  end
76
62
 
@@ -91,13 +77,7 @@ class Xid::Base32
91
77
  break
92
78
  end
93
79
  char = src[i]
94
- if char == PAD_CHAR
95
- end_loop = true
96
- src_len = i
97
- break
98
- else
99
- dbuf[i] = decode_hex_map[char]
100
- end
80
+ dbuf[i] = decode_hex_map[char]
101
81
  end
102
82
 
103
83
  if src_len >= 8
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby_xid
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Valarpiraichandran