slothful-code-generator 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.editorconfig +3 -0
- data/.gitignore +10 -0
- data/.travis.yml +7 -0
- data/Gemfile +4 -0
- data/LICENSE +25 -0
- data/README.md +46 -0
- data/Rakefile +10 -0
- data/Steepfile +7 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/slothful-code-generator.rb +1 -0
- data/lib/slothful_code.rb +134 -0
- data/lib/slothful_code/version.rb +3 -0
- data/sig/slothful_code.rbs +27 -0
- data/slothful-code-generator.gemspec +40 -0
- metadata +187 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: f8dbd14d94387b3989432616c6fac8ad9d6dbd95b51bc665db1bf612672376ed
|
4
|
+
data.tar.gz: c0eb253a2e8d63f85e844d7df91b6593b1048c9024e8564a9e824f7e44af4788
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 695fa543111485dd49ea0daf207c39bde52f1f387f67a77beaabce4d5935f21719efd4d6f7aa8f967b1ff97e18e12b070abea05caffa5f3f51b93cc3b3fb2939
|
7
|
+
data.tar.gz: 0b67467a85a6729a261bbd76e4d94f6eb563e10bea0f374a1e9b23c2c938e64bd62d2dfff56e063873f6c0d3a1b321438a1f1d3fd2953a02a251639cc380a8e3
|
data/.editorconfig
ADDED
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
BSD 2-Clause License
|
2
|
+
|
3
|
+
Copyright (c) 2021, Colorful Company,Inc.
|
4
|
+
All rights reserved.
|
5
|
+
|
6
|
+
Redistribution and use in source and binary forms, with or without
|
7
|
+
modification, are permitted provided that the following conditions are met:
|
8
|
+
|
9
|
+
1. Redistributions of source code must retain the above copyright notice, this
|
10
|
+
list of conditions and the following disclaimer.
|
11
|
+
|
12
|
+
2. Redistributions in binary form must reproduce the above copyright notice,
|
13
|
+
this list of conditions and the following disclaimer in the documentation
|
14
|
+
and/or other materials provided with the distribution.
|
15
|
+
|
16
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
17
|
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
18
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
19
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
20
|
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
21
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
22
|
+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
23
|
+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
24
|
+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
25
|
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
data/README.md
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
# SlothfulCode
|
2
|
+
|
3
|
+
ある程度人間にとって扱いやすい長さ、複雑さで、類推しにくく、重複しないコードを生成、検証する
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'slothful-code-generator'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install slothful-code-generator
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
require 'slothful_code'
|
25
|
+
```
|
26
|
+
|
27
|
+
### ganerate
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
sc = SlothfulCode.new(<salt>)
|
31
|
+
sc.generate(type_id: '1') # => '989JV7WDCN6Q-1'
|
32
|
+
```
|
33
|
+
|
34
|
+
### decode
|
35
|
+
|
36
|
+
```ruby
|
37
|
+
sc = SlothfulCode.new(<salt>)
|
38
|
+
sc.decode(<code>) # => {:type_id=>'1', :time=>[1612769809, 113]}
|
39
|
+
```
|
40
|
+
|
41
|
+
### valid?
|
42
|
+
|
43
|
+
```ruby
|
44
|
+
sc = SlothfulCode.new(<salt>)
|
45
|
+
sc.valid?(<code>) # => true / false
|
46
|
+
```
|
data/Rakefile
ADDED
data/Steepfile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "slothful_code"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require_relative './slothful_code'
|
@@ -0,0 +1,134 @@
|
|
1
|
+
require "hashids"
|
2
|
+
require "slothful_code/version"
|
3
|
+
|
4
|
+
#
|
5
|
+
# 識別コードを生成、検証する
|
6
|
+
#
|
7
|
+
# hashids を利用して長過ぎず短過ぎない、人間に読み書きしやすくユニー
|
8
|
+
# クな文字列を生成し、またその文字列が適切にこのシステムが生成したも
|
9
|
+
# のかどうかを検証できる仕組み
|
10
|
+
#
|
11
|
+
# 数字と英大文字のうち数字と紛らわしくない文字を利用して11, 12桁程度に
|
12
|
+
# 収まるだろう文字列を生成する
|
13
|
+
#
|
14
|
+
# sc = SlothfulCode.new
|
15
|
+
# sc.generate(type_id: '1') # => 'R6B7ZRDT6EY-1'
|
16
|
+
#
|
17
|
+
# see https://hashids.org/
|
18
|
+
#
|
19
|
+
class SlothfulCode
|
20
|
+
class Error < StandardError; end
|
21
|
+
class WrongTypeId < Error; end
|
22
|
+
|
23
|
+
HASHIDS_INCLUDE_CHARS = '1234567890ABCDEFGHJKLMNPQRSTUVWXYZ'.freeze
|
24
|
+
TYPE_CHARS = /\A[0-9A-Z]+\z/
|
25
|
+
|
26
|
+
#
|
27
|
+
# [param] String salt
|
28
|
+
#
|
29
|
+
def initialize(salt = '')
|
30
|
+
@id_processor = Hashids.new(salt, 6, HASHIDS_INCLUDE_CHARS)
|
31
|
+
end
|
32
|
+
attr_reader :resources
|
33
|
+
|
34
|
+
#
|
35
|
+
# コードを生成する
|
36
|
+
#
|
37
|
+
# [param] String type_id
|
38
|
+
# [return] String
|
39
|
+
#
|
40
|
+
def generate(type_id:)
|
41
|
+
t = time_array
|
42
|
+
|
43
|
+
if type_id.to_s =~ TYPE_CHARS
|
44
|
+
hash = [
|
45
|
+
@id_processor.encode(t),
|
46
|
+
type_id.to_s
|
47
|
+
].join('-')
|
48
|
+
|
49
|
+
@resources = { type_id: type_id.to_s, time: t }
|
50
|
+
hash
|
51
|
+
else
|
52
|
+
raise WrongTypeId
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
#
|
57
|
+
# 与えられたコードが hashids で decode できるかどうか
|
58
|
+
#
|
59
|
+
# [param] String code
|
60
|
+
# [return] Boolean
|
61
|
+
#
|
62
|
+
def valid?(code)
|
63
|
+
decode(code).size > 0
|
64
|
+
end
|
65
|
+
|
66
|
+
#
|
67
|
+
# [param] String code
|
68
|
+
# [return] Hash
|
69
|
+
#
|
70
|
+
# :reek:TooManyStatement
|
71
|
+
def decode(code)
|
72
|
+
begin
|
73
|
+
id_str, type_id = code.split('-')
|
74
|
+
id = @id_processor.decode(id_str)
|
75
|
+
|
76
|
+
if id && type_id
|
77
|
+
@resources = { type_id: type_id, time: id }
|
78
|
+
else
|
79
|
+
{}
|
80
|
+
end
|
81
|
+
rescue Hashids::InputError => e
|
82
|
+
if e.message == 'unable to unhash'
|
83
|
+
{}
|
84
|
+
else
|
85
|
+
raise
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
#
|
91
|
+
# [return] Time
|
92
|
+
#
|
93
|
+
def now
|
94
|
+
Time.now
|
95
|
+
end
|
96
|
+
|
97
|
+
#
|
98
|
+
# 現在時刻を2つの整数の配列にして返す
|
99
|
+
#
|
100
|
+
# 少数部を 1 msec にまで丸めて2つの整数の配列にして返している。この
|
101
|
+
# 部分は完全に hashids での利用のために特化している。この際、0.1 と
|
102
|
+
# 0.001 が同じ整数 1 にならないように 0 を埋めている。
|
103
|
+
#
|
104
|
+
# ex)
|
105
|
+
# [12345, 123] = time_array
|
106
|
+
#
|
107
|
+
# [return] Array
|
108
|
+
#
|
109
|
+
# :reek:UncommunicativeVariableName
|
110
|
+
def time_array
|
111
|
+
t = now.to_f.round(3)
|
112
|
+
sleep 0.001
|
113
|
+
|
114
|
+
ht, lt = t.to_s.split('.')
|
115
|
+
[ht.to_i, (lt.to_s + '00')[0, 3].to_i]
|
116
|
+
end
|
117
|
+
|
118
|
+
#
|
119
|
+
# decode 済みのコードについて確認用の日付を取得する
|
120
|
+
#
|
121
|
+
# [return] String
|
122
|
+
#
|
123
|
+
# :reek:UncommunicativeVariableName, :reek:FeatureEnvy
|
124
|
+
def date
|
125
|
+
time = resources[:time]
|
126
|
+
|
127
|
+
if time
|
128
|
+
t = Time.at(time.first)
|
129
|
+
{ month: t.month, mon: t.mon, day: t.day }
|
130
|
+
else
|
131
|
+
nil
|
132
|
+
end
|
133
|
+
end
|
134
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# -*- mode: ruby -*-
|
2
|
+
|
3
|
+
type time_array = [Integer, Integer]
|
4
|
+
type resource_record = { type_id: String?, time: time_array? }
|
5
|
+
type date_record = { month: Integer, mon: Integer, day: Integer }
|
6
|
+
|
7
|
+
class SlothfulCode
|
8
|
+
VERSION: String
|
9
|
+
HASHIDS_INCLUDE_CHARS: String
|
10
|
+
TYPE_CHARS: untyped
|
11
|
+
|
12
|
+
attr_reader resources: resource_record
|
13
|
+
|
14
|
+
def initialize: (?String) -> untyped
|
15
|
+
|
16
|
+
def generate: (type_id: String) -> String
|
17
|
+
|
18
|
+
def valid?: (String) -> bool
|
19
|
+
|
20
|
+
def decode: (String) -> resource_record
|
21
|
+
|
22
|
+
def now: () -> Time
|
23
|
+
|
24
|
+
def time_array: () -> time_array
|
25
|
+
|
26
|
+
def date: () -> (date_record | nil)
|
27
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
lib = File.expand_path("lib", __dir__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require "slothful_code/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "slothful-code-generator"
|
7
|
+
spec.version = SlothfulCode::VERSION
|
8
|
+
spec.authors = [""]
|
9
|
+
spec.email = [""]
|
10
|
+
|
11
|
+
spec.summary = %q{generate time-based unique id with loose precision}
|
12
|
+
spec.description = # %q{TODO: Write a longer description or delete this line.}
|
13
|
+
spec.homepage = "https://github.com/colorfulcompany/slothful-code-generator"
|
14
|
+
|
15
|
+
spec.metadata["allowed_push_host"] = "https://rubygems.org"
|
16
|
+
|
17
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
18
|
+
spec.metadata["source_code_uri"] = spec.homepage
|
19
|
+
# spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
|
20
|
+
|
21
|
+
# Specify which files should be added to the gem when it is released.
|
22
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
23
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
24
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
25
|
+
end
|
26
|
+
spec.bindir = "exe"
|
27
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
28
|
+
spec.require_paths = ["lib"]
|
29
|
+
|
30
|
+
spec.add_runtime_dependency "hashids", "~> 1"
|
31
|
+
|
32
|
+
spec.add_development_dependency "bundler", "~> 2.0"
|
33
|
+
spec.add_development_dependency "rake", "~> 12"
|
34
|
+
spec.add_development_dependency "minitest", "~> 5.0"
|
35
|
+
spec.add_development_dependency "minitest-skip"
|
36
|
+
spec.add_development_dependency "minitest-power_assert", "~> 0.3"
|
37
|
+
spec.add_development_dependency "power_assert", "~> 2.0"
|
38
|
+
spec.add_development_dependency "minitest-reporters", "~> 1"
|
39
|
+
spec.add_development_dependency "steep"
|
40
|
+
end
|
metadata
ADDED
@@ -0,0 +1,187 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: slothful-code-generator
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- ''
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2021-03-18 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: hashids
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '2.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '2.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '12'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '12'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: minitest
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '5.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '5.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: minitest-skip
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: minitest-power_assert
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0.3'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0.3'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: power_assert
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '2.0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '2.0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: minitest-reporters
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - "~>"
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '1'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - "~>"
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '1'
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: steep
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - ">="
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0'
|
132
|
+
type: :development
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - ">="
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '0'
|
139
|
+
description: https://github.com/colorfulcompany/slothful-code-generator
|
140
|
+
email:
|
141
|
+
- ''
|
142
|
+
executables: []
|
143
|
+
extensions: []
|
144
|
+
extra_rdoc_files: []
|
145
|
+
files:
|
146
|
+
- ".editorconfig"
|
147
|
+
- ".gitignore"
|
148
|
+
- ".travis.yml"
|
149
|
+
- Gemfile
|
150
|
+
- LICENSE
|
151
|
+
- README.md
|
152
|
+
- Rakefile
|
153
|
+
- Steepfile
|
154
|
+
- bin/console
|
155
|
+
- bin/setup
|
156
|
+
- lib/slothful-code-generator.rb
|
157
|
+
- lib/slothful_code.rb
|
158
|
+
- lib/slothful_code/version.rb
|
159
|
+
- sig/slothful_code.rbs
|
160
|
+
- slothful-code-generator.gemspec
|
161
|
+
homepage: https://github.com/colorfulcompany/slothful-code-generator
|
162
|
+
licenses: []
|
163
|
+
metadata:
|
164
|
+
allowed_push_host: https://rubygems.org
|
165
|
+
homepage_uri: https://github.com/colorfulcompany/slothful-code-generator
|
166
|
+
source_code_uri: https://github.com/colorfulcompany/slothful-code-generator
|
167
|
+
post_install_message:
|
168
|
+
rdoc_options: []
|
169
|
+
require_paths:
|
170
|
+
- lib
|
171
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
172
|
+
requirements:
|
173
|
+
- - ">="
|
174
|
+
- !ruby/object:Gem::Version
|
175
|
+
version: '0'
|
176
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
177
|
+
requirements:
|
178
|
+
- - ">="
|
179
|
+
- !ruby/object:Gem::Version
|
180
|
+
version: '0'
|
181
|
+
requirements: []
|
182
|
+
rubyforge_project:
|
183
|
+
rubygems_version: 2.7.6.2
|
184
|
+
signing_key:
|
185
|
+
specification_version: 4
|
186
|
+
summary: generate time-based unique id with loose precision
|
187
|
+
test_files: []
|