order_id 0.1.1 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.gitignore +1 -1
- data/Gemfile +6 -0
- data/README.md +3 -3
- data/bin/console +3 -5
- data/lib/order_id/version.rb +3 -0
- data/lib/order_id.rb +27 -0
- data/{OrderId.gemspec → order_id.gemspec} +13 -12
- metadata +25 -12
- data/.idea/workspace.xml +0 -49
- data/lib/OrderId/version.rb +0 -3
- data/lib/OrderId.rb +0 -26
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 4411fb9039ddb6ce1173b1093eb5978ff8d0c04b12bdb51d3671448b2f16eba6
|
4
|
+
data.tar.gz: 517f9d854d8d469be93929b9fd778b8fd8765d39dfd9778bc271593aa37c5ef5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6e8c88d48610a1eb1688d440fb22e02648ab8e55d5f8b07323653b01203db07989d32e65e95b7472116973492cea7b3d39b9cba718089049df3ebe6699ae06d2
|
7
|
+
data.tar.gz: 3c3ea8008b0bb8b03bfa8a7ef200bd887f7fffffcfae3cf06fd8ad8a1974c58064951c7c5a433d13ea11d93ccdebc873663b998238252c7c402646cfdcf18e77
|
data/.gitignore
CHANGED
data/Gemfile
ADDED
data/README.md
CHANGED
@@ -13,7 +13,7 @@ of XX chars separated by a custom separator-char.
|
|
13
13
|
Add this line to your application's Gemfile:
|
14
14
|
|
15
15
|
```ruby
|
16
|
-
gem '
|
16
|
+
gem 'order_id'
|
17
17
|
```
|
18
18
|
|
19
19
|
And then execute:
|
@@ -32,7 +32,7 @@ OrderId.get_time('G3RRY-ZMIHR-CCZ3P-FGXM') # 2022-08-21 20:33:18 +0000
|
|
32
32
|
```
|
33
33
|
### Parameters
|
34
34
|
OrderId takes four optional parameters:
|
35
|
-
- `
|
35
|
+
- `decimal_places`: number of decimal places in a timestamp. Makes no sense if it's over 20, common sense is 10 or 12.
|
36
36
|
- `base`: base number system. Defaults to 36, but any arbitrary base is a good place to obfuscate your ids. E.g . `OrderId.generate(base: 12) # 12616-51312-6751B-A87B1-72235-444`
|
37
37
|
- `separator`: a separator char. Defaults to `-` but can be `'/'` or any non-digit and non-word character
|
38
38
|
- `group_length`: a number of chars in groups separated by a `separator`. Defaults to 4. `OrderId.generate(base: 12, length: 4, group_length: 8) # "1A434285-32526"`
|
@@ -41,7 +41,7 @@ OrderId takes four optional parameters:
|
|
41
41
|
If you know the parameters which the Id has been generated with you can restore a timestamp the Id was based on.
|
42
42
|
|
43
43
|
```ruby
|
44
|
-
OrderId.get_time('1A434285-32526', base: 12,
|
44
|
+
OrderId.get_time('1A434285-32526', base: 12, decimal_places: 4) # 2022-08-21 20:49:34 +0000
|
45
45
|
```
|
46
46
|
|
47
47
|
## Development
|
data/bin/console
CHANGED
@@ -1,14 +1,12 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
|
3
3
|
require "bundler/setup"
|
4
|
-
require "
|
4
|
+
require "order_id"
|
5
5
|
|
6
6
|
# You can add fixtures and/or initialization code here to make experimenting
|
7
7
|
# with your gem easier. You can also use a different console, if you like.
|
8
8
|
|
9
9
|
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
-
|
11
|
-
|
10
|
+
require "pry"
|
11
|
+
Pry.start
|
12
12
|
|
13
|
-
require "irb"
|
14
|
-
IRB.start(__FILE__)
|
data/lib/order_id.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'order_id/version'
|
2
|
+
require 'bigdecimal'
|
3
|
+
module OrderId
|
4
|
+
DECIMAL_PLACES = 20
|
5
|
+
BASE = 36
|
6
|
+
SEPARATOR = '-'
|
7
|
+
GROUP_LENGTH = 5
|
8
|
+
ALLOWED_SEPARATORS = ['-', '_', '|', ':', '@', '.', '/', '#', '!', '$', '%', '^', '&', '*', '(', ')', '[', ']', '{', '}'].freeze
|
9
|
+
|
10
|
+
class FormatError < StandardError; end
|
11
|
+
|
12
|
+
def self.generate(decimal_places: DECIMAL_PLACES, base: BASE, separator: SEPARATOR, group_length: GROUP_LENGTH)
|
13
|
+
raise FormatError, "Characters not allowed as separator: '#{separator}'" unless ALLOWED_SEPARATORS.include?(separator)
|
14
|
+
raise FormatError, 'Length should be positive' unless decimal_places.positive?
|
15
|
+
|
16
|
+
t = Time.now.to_f
|
17
|
+
ts = format("%.#{decimal_places}f", t).delete('.')
|
18
|
+
final = ts.to_i.to_s(base).upcase
|
19
|
+
final.scan(/.{1,#{group_length}}/).join(separator)
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.get_time(id, decimal_places: DECIMAL_PLACES, base: BASE, separator: SEPARATOR)
|
23
|
+
id.delete!(separator)
|
24
|
+
ts = id.to_i(base) / BigDecimal("1e+#{decimal_places}")
|
25
|
+
Time.at(ts)
|
26
|
+
end
|
27
|
+
end
|
@@ -1,18 +1,18 @@
|
|
1
1
|
|
2
|
-
lib = File.expand_path(
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
3
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
-
require
|
4
|
+
require 'order_id/version'
|
5
5
|
|
6
6
|
Gem::Specification.new do |spec|
|
7
|
-
spec.name =
|
7
|
+
spec.name = 'order_id'
|
8
8
|
spec.version = OrderId::VERSION
|
9
|
-
spec.authors = [
|
10
|
-
spec.email = [
|
9
|
+
spec.authors = ['Max Buslaev']
|
10
|
+
spec.email = ['max@buslaev.net']
|
11
11
|
|
12
12
|
spec.summary = %q{Timestamps-based ID strings.}
|
13
13
|
spec.description = %q{Generates an ID string best used for orders/invoices/etc. Timestamp-based, reverse parseable.}
|
14
|
-
spec.homepage =
|
15
|
-
spec.license =
|
14
|
+
spec.homepage = 'https://github.com/austerlitz/order_id'
|
15
|
+
spec.license = 'MIT'
|
16
16
|
|
17
17
|
|
18
18
|
# Specify which files should be added to the gem when it is released.
|
@@ -20,13 +20,14 @@ Gem::Specification.new do |spec|
|
|
20
20
|
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
21
21
|
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
22
22
|
end
|
23
|
-
spec.bindir =
|
23
|
+
spec.bindir = 'exe'
|
24
24
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
25
|
-
spec.require_paths = [
|
25
|
+
spec.require_paths = ['lib']
|
26
26
|
|
27
|
-
spec.add_development_dependency
|
28
|
-
spec.add_development_dependency
|
29
|
-
spec.add_development_dependency
|
27
|
+
spec.add_development_dependency 'bundler'
|
28
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
29
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
30
|
+
spec.add_development_dependency 'pry'
|
30
31
|
|
31
32
|
spec.add_dependency 'bigdecimal'
|
32
33
|
end
|
metadata
CHANGED
@@ -1,29 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: order_id
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Max Buslaev
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2023-07-31 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - "
|
17
|
+
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '0'
|
20
20
|
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- - "
|
24
|
+
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '
|
26
|
+
version: '0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rake
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -52,6 +52,20 @@ dependencies:
|
|
52
52
|
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: pry
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
55
69
|
- !ruby/object:Gem::Dependency
|
56
70
|
name: bigdecimal
|
57
71
|
requirement: !ruby/object:Gem::Requirement
|
@@ -75,18 +89,18 @@ extensions: []
|
|
75
89
|
extra_rdoc_files: []
|
76
90
|
files:
|
77
91
|
- ".gitignore"
|
78
|
-
- ".idea/workspace.xml"
|
79
92
|
- ".rspec"
|
80
93
|
- ".travis.yml"
|
81
94
|
- CODE_OF_CONDUCT.md
|
95
|
+
- Gemfile
|
82
96
|
- LICENSE.txt
|
83
|
-
- OrderId.gemspec
|
84
97
|
- README.md
|
85
98
|
- Rakefile
|
86
99
|
- bin/console
|
87
100
|
- bin/setup
|
88
|
-
- lib/
|
89
|
-
- lib/
|
101
|
+
- lib/order_id.rb
|
102
|
+
- lib/order_id/version.rb
|
103
|
+
- order_id.gemspec
|
90
104
|
homepage: https://github.com/austerlitz/order_id
|
91
105
|
licenses:
|
92
106
|
- MIT
|
@@ -106,8 +120,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
106
120
|
- !ruby/object:Gem::Version
|
107
121
|
version: '0'
|
108
122
|
requirements: []
|
109
|
-
|
110
|
-
rubygems_version: 2.5.2
|
123
|
+
rubygems_version: 3.4.13
|
111
124
|
signing_key:
|
112
125
|
specification_version: 4
|
113
126
|
summary: Timestamps-based ID strings.
|
data/.idea/workspace.xml
DELETED
@@ -1,49 +0,0 @@
|
|
1
|
-
<?xml version="1.0" encoding="UTF-8"?>
|
2
|
-
<project version="4">
|
3
|
-
<component name="ChangeListManager">
|
4
|
-
<list default="true" id="4b873eb2-62f8-4b0c-a8f7-c2e6e71629fe" name="Changes" comment="" />
|
5
|
-
<option name="SHOW_DIALOG" value="false" />
|
6
|
-
<option name="HIGHLIGHT_CONFLICTS" value="true" />
|
7
|
-
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
|
8
|
-
<option name="LAST_RESOLUTION" value="IGNORE" />
|
9
|
-
</component>
|
10
|
-
<component name="Git.Settings">
|
11
|
-
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" />
|
12
|
-
</component>
|
13
|
-
<component name="MarkdownSettingsMigration">
|
14
|
-
<option name="stateVersion" value="1" />
|
15
|
-
</component>
|
16
|
-
<component name="ProjectId" id="2DgD17KvwTjH9BGJd1Qsr1bJHzY" />
|
17
|
-
<component name="ProjectLevelVcsManager" settingsEditedManually="true" />
|
18
|
-
<component name="ProjectViewState">
|
19
|
-
<option name="hideEmptyMiddlePackages" value="true" />
|
20
|
-
<option name="showLibraryContents" value="true" />
|
21
|
-
</component>
|
22
|
-
<component name="PropertiesComponent">{
|
23
|
-
"keyToString": {
|
24
|
-
"RunOnceActivity.OpenProjectViewOnStart": "true",
|
25
|
-
"RunOnceActivity.ShowReadmeOnStart": "true",
|
26
|
-
"WebServerToolWindowFactoryState": "false",
|
27
|
-
"nodejs_package_manager_path": "npm",
|
28
|
-
"ruby.rails.projectView.checked": "true",
|
29
|
-
"vue.rearranger.settings.migration": "true"
|
30
|
-
}
|
31
|
-
}</component>
|
32
|
-
<component name="SpellCheckerSettings" RuntimeDictionaries="0" Folders="0" CustomDictionaries="0" DefaultDictionary="application-level" UseSingleDictionary="true" transferred="true" />
|
33
|
-
<component name="SpringUtil" SPRING_PRE_LOADER_OPTION="true" RAKE_SPRING_PRE_LOADER_OPTION="true" RAILS_SPRING_PRE_LOADER_OPTION="true" />
|
34
|
-
<component name="TaskManager">
|
35
|
-
<task active="true" id="Default" summary="Default task">
|
36
|
-
<changelist id="4b873eb2-62f8-4b0c-a8f7-c2e6e71629fe" name="Changes" comment="" />
|
37
|
-
<created>1661111615612</created>
|
38
|
-
<option name="number" value="Default" />
|
39
|
-
<option name="presentableId" value="Default" />
|
40
|
-
<updated>1661111615612</updated>
|
41
|
-
<workItem from="1661111617027" duration="4151000" />
|
42
|
-
<workItem from="1661115973104" duration="395000" />
|
43
|
-
</task>
|
44
|
-
<servers />
|
45
|
-
</component>
|
46
|
-
<component name="TypeScriptGeneratedFilesManager">
|
47
|
-
<option name="version" value="3" />
|
48
|
-
</component>
|
49
|
-
</project>
|
data/lib/OrderId/version.rb
DELETED
data/lib/OrderId.rb
DELETED
@@ -1,26 +0,0 @@
|
|
1
|
-
require "OrderId/version"
|
2
|
-
require 'bigdecimal'
|
3
|
-
module OrderId
|
4
|
-
DECIMAL_LENGTH = 20
|
5
|
-
BASE = 36
|
6
|
-
SEPARATOR = '-'
|
7
|
-
GROUP_LENGTH = 5
|
8
|
-
class FormatError < StandardError; end
|
9
|
-
|
10
|
-
def self.generate(length: DECIMAL_LENGTH, base: BASE, separator: SEPARATOR, group_length: GROUP_LENGTH)
|
11
|
-
if separator =~ (/[\w\d]/)
|
12
|
-
raise FormatError, "Characters not allowed as separator: `#{separator}`"
|
13
|
-
end
|
14
|
-
t = Time.now.to_f
|
15
|
-
ts = "%.#{length}f" % t
|
16
|
-
ts.delete!('.')
|
17
|
-
final = ts.to_i.to_s(base).upcase
|
18
|
-
final.scan(/.{1,#{group_length}}/).join(separator)
|
19
|
-
end
|
20
|
-
|
21
|
-
def self.get_time(id, length: DECIMAL_LENGTH, base: BASE, separator: SEPARATOR)
|
22
|
-
id.delete!(separator)
|
23
|
-
ts = id.to_i(base) / BigDecimal("1e+#{length}")
|
24
|
-
Time.at(ts)
|
25
|
-
end
|
26
|
-
end
|