uxid 0.1.0 → 0.2.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
  SHA256:
3
- metadata.gz: 1c5ebc6fc8f88263e841c870bb1191ac2769933134e68557bcf8c5472c4a9f3b
4
- data.tar.gz: 0bea747a87b5ad069325004ea48d9b38e3bd7935e8769f3056e5ceea550b684d
3
+ metadata.gz: 307443b03ac5b5b09a4d9f9da47fb1fe49c0756a003686e4dc91befd7826b46f
4
+ data.tar.gz: 4ccd6a17d53a456c353c4e3d227c8d49e5d3d46f41a5cf07ecd7de4b8c29da81
5
5
  SHA512:
6
- metadata.gz: ebd95c6ed10c5bd1272c598bb7d0538fb72888e7e11842ab4aa30cecccb8a92d839d278fba4d5ec62f512df8a5509ac1f93dc7d4102e8262c7b3f0241a4c145a
7
- data.tar.gz: 6e4bb57c8369218d3015cf81ec967625e33f51cf66e5641b4c63c4b32bba9dad424def094b86ba88464d906176c16d9c187fc4c89cb5caa85234be8d99c9d0f7
6
+ metadata.gz: 80f7234fa2c8497a2c5077b4a267ce9cfc2bbe9638891a539ec9471e63570a6be128cf26289a2453b8f5fd8555cee779bc1fb65737f100b4b7bb75585469ba35
7
+ data.tar.gz: 8ec73e770fed7334cbe650143032286a635ecabd8861417bc12b3340639b162aa23cc544cb980ebb825b1cb2ca6c9b5b4279a5e95ce992fb3955f1c4c1beb65d
@@ -1,3 +1,10 @@
1
+ ## 0.2.0 / 2020-11-24
2
+
3
+ * Deprecates :size option with integers
4
+ * Adds T-Shirt sizes (:size option)
5
+ * Adds :rand_size option (:rand_size option)
6
+
7
+
1
8
  ## 0.1.0 / 2020-11-08
2
9
 
3
10
  * Adds basic UXID generation
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- uxid (0.0.1)
4
+ uxid (0.2.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -3,17 +3,18 @@
3
3
  ![MIT License][badge_license_url]
4
4
  ![Gem Version][badge_version_url]
5
5
 
6
- UXIDs are identifiers which:
6
+
7
+ **U**ser e**X**perience focused **ID**entifiers (UXIDs) are identifiers which:
7
8
 
8
9
  * Describe the resource (aid in debugging and investigation)
9
- * Prononounceable - easily and accurately transmit characters to another human using a telephone
10
10
  * Work well with copy and paste (double clicking selects the entire ID)
11
11
  * Can be shortened for low cardinality resources
12
- * Are very unlikely to collide (more likely with less randomness)
13
12
  * Are secure against enumeration attacks
14
13
  * Can be generated by application code (not tied to the datastore)
15
14
  * Are K-sortable (lexicographically sortable by time - works well with datastore indexing)
16
15
  * Do not require any coordination (human or automated) at startup, or generation
16
+ * Are very unlikely to collide (more likely with less randomness)
17
+ * Are easily and accurately transmitted to another human using a telephone
17
18
 
18
19
  Many of the concepts of Stripe IDs have been used in this library.
19
20
 
@@ -29,7 +30,8 @@ UXID.generate # "01EMDGJF0DQXQJ8FM78XE97Y3H"
29
30
  UXID.generate prefix: "cus" # "cus_01EMDGJF0DQXQJ8FM78XE97Y3H"
30
31
 
31
32
  # The amount of entropy can be decreased for smaller cardinality resources
32
- UXID.generate prefix: "cus", size: 4 # "cus_01EMDGN5QAGPWSKN2"
33
+ # T-Shirt sizes can be used (:xs, :s, :m, :l, :xl) or (:xsmall, :small, :medium, :large, :xlarge)
34
+ UXID.generate prefix: "cus", size: :small # "cus_01EQRH884AQYY1"
33
35
  ```
34
36
 
35
37
 
@@ -38,7 +40,7 @@ UXID.generate prefix: "cus", size: 4 # "cus_01EMDGN5QAGPWSKN2"
38
40
  Add this line to your application's Gemfile:
39
41
 
40
42
  ```ruby
41
- gem 'uxid'
43
+ gem "uxid"
42
44
  ```
43
45
 
44
46
  And then execute:
@@ -14,6 +14,12 @@ module UXID
14
14
  CROCKFORD_ENCODING = "0123456789ABCDEFGHJKMNPQRSTVWXYZ"
15
15
  DELIMITER = "_"
16
16
 
17
+ SIZE_WARNING_MESSAGE = <<-EOM
18
+ WARN: The UXID :size param is for Symbol T-Shirt sizes only.
19
+ Please use the :size param with Symbols (:xs, :s, :m, :l, :xl, or :xsmall, :small, :medium, :large, :xlarge),
20
+ or use the :rand_size param for specific size of random bytes.
21
+ EOM
22
+
17
23
  class Error < StandardError; end
18
24
 
19
25
  def self.generate attrs = {}
@@ -25,9 +31,39 @@ module UXID
25
31
  model = ::UXID::Model.new
26
32
 
27
33
  model.time = attrs[:time] || Time.now
28
- model.size = attrs[:size] || 10
29
34
  model.prefix = attrs[:prefix] || ""
30
35
 
36
+ if attrs[:rand_size]
37
+ model.rand_size = attrs[:rand_size]
38
+ else
39
+ model.size = attrs[:size]
40
+ case model.size
41
+ when Integer
42
+ puts SIZE_WARNING_MESSAGE
43
+ model.rand_size = model.size
44
+
45
+ when String
46
+ puts SIZE_WARNING_MESSAGE
47
+ model.rand_size = 10
48
+
49
+ when :xs, :xsmall
50
+ model.rand_size = 0
51
+
52
+ when :s, :small
53
+ model.rand_size = 2
54
+
55
+ when :m, :medium
56
+ model.rand_size = 5
57
+
58
+ when :l, :large
59
+ model.rand_size = 7
60
+
61
+ else
62
+ model.rand_size = 10
63
+ end
64
+
65
+ end
66
+
31
67
  model
32
68
  end
33
69
  end
@@ -10,7 +10,7 @@ module UXID
10
10
 
11
11
  def encode
12
12
  @model.time_encoded = encode_time
13
- @model.entropy_encoded = public_send "encode_entropy_#{@model.size}"
13
+ @model.entropy_encoded = public_send "encode_entropy_#{@model.rand_size}"
14
14
 
15
15
  @model.encoded
16
16
  end
@@ -35,17 +35,6 @@ module UXID
35
35
  UXID::CROCKFORD_ENCODING[((b1&1)<<4)]
36
36
  end
37
37
 
38
- def encode_entropy_2
39
- b0, b1 = @model.entropy_bytes
40
-
41
- UXID::CROCKFORD_ENCODING[(b0&248)>>3] +
42
- UXID::CROCKFORD_ENCODING[((b0&7)<<2)] +
43
- UXID::CROCKFORD_ENCODING[(b0&248)>>3] +
44
- UXID::CROCKFORD_ENCODING[((b0&7)<<2) | ((b1&192)>>6)] +
45
- UXID::CROCKFORD_ENCODING[(b1&62)>>1] +
46
- UXID::CROCKFORD_ENCODING[((b1&1)<<4)]
47
- end
48
-
49
38
  def encode_entropy_3
50
39
  b0, b1, b2 = @model.entropy_bytes
51
40
 
@@ -2,7 +2,7 @@ require "uxid/encoder"
2
2
 
3
3
  module UXID
4
4
  class Model
5
- attr_accessor :time, :time_encoded, :entropy, :entropy_encoded, :prefix, :size
5
+ attr_accessor :time, :time_encoded, :entropy, :entropy_encoded, :prefix, :size, :rand_size
6
6
 
7
7
  def encode
8
8
  encoder = ::UXID::Encoder.new self
@@ -22,7 +22,7 @@ module UXID
22
22
  def entropy_bytes
23
23
  return @entropy_bytes if @entropy_bytes
24
24
 
25
- bytes_string = SecureRandom.random_bytes @size
25
+ bytes_string = SecureRandom.random_bytes @rand_size
26
26
  @entropy_bytes = bytes_string.bytes
27
27
  return @entropy_bytes
28
28
  end
@@ -1,3 +1,3 @@
1
1
  module UXID
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: uxid
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - JohnnyT
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-11-08 00:00:00.000000000 Z
11
+ date: 2020-11-24 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: 'Generates IDs like: cus_01EPEY1JMKXVBT and txn_01EPEY2P06TR1RTV07XA82ZGJJ
14
14
  (similar to Stripe identifiers).'