id_generator 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 37e5398140f12ae7caa2db2c35878c213471d05572324a1da557fce3be36df8a
4
- data.tar.gz: 386736c3d6ce69cd9b2755d5f01c0efe020652ade9cabf804e98ec8ce30d309b
3
+ metadata.gz: 6c2e271fb50e9a96d53b92c70fa45d55f8a8dc85aee47410868cb6ef33f239d4
4
+ data.tar.gz: 26ea59ebc7009adb5e2ca9fc5ac02af9d243e3a4b0a82efee6ad1b4f65296dc3
5
5
  SHA512:
6
- metadata.gz: e0e138ca63cfd6cbb86228d85be5942686b26d8117886e4ed07d6b99107fc72d2a1b980a76e7c4393ea6d09b37af48093ee11388d3ed3345541dc4bb2587ba6b
7
- data.tar.gz: fe98cb7ad9c71fcd0515a9da8f33f4914fd73986cefd657be08d028799624ed2953977df7807fa345258a1e9dda9dfa2311f09e6026db7fbbb46f7b94de40b17
6
+ metadata.gz: edc950a375068320a1a6e4d478fde583f4677263103d3f391f5e7a4bbee1db2eda2a78fd63d1b1f98401088d4faec278a8b9e9b833c444cf79d427536d1a143f
7
+ data.tar.gz: bf47f173ee92b65f56a799654e39fc096c15a0cad660620bbb8e72ccce8884b253cdd72eae4693c55f6a3ec46974b69a9133b52fb848d1e44618143567cba1d3
@@ -5,6 +5,9 @@ require: rubocop-rspec
5
5
 
6
6
  #Example: https://github.com/bbatsov/rubocop/blob/master/config/default.yml
7
7
 
8
+ Metrics/LineLength:
9
+ Max: 120
10
+
8
11
  Style/FrozenStringLiteralComment:
9
12
  Enabled: false
10
13
 
@@ -12,5 +15,6 @@ Style/Documentation:
12
15
  Enabled: false
13
16
 
14
17
  Metrics/BlockLength:
18
+ ExcludedMethods: ['describe']
15
19
  Exclude:
16
20
  - 'id_generator.gemspec'
@@ -7,9 +7,13 @@ GEM
7
7
  remote: https://rubygems.org/
8
8
  specs:
9
9
  ast (2.4.0)
10
+ codacy-coverage (2.1.0)
11
+ simplecov
10
12
  coderay (1.1.2)
11
13
  diff-lcs (1.3)
14
+ docile (1.3.1)
12
15
  jaro_winkler (1.5.1)
16
+ json (2.1.0)
13
17
  method_source (0.9.0)
14
18
  parallel (1.12.1)
15
19
  parser (2.5.1.2)
@@ -45,6 +49,11 @@ GEM
45
49
  rubocop-rspec (1.30.0)
46
50
  rubocop (>= 0.58.0)
47
51
  ruby-progressbar (1.10.0)
52
+ simplecov (0.16.1)
53
+ docile (~> 1.1)
54
+ json (>= 1.8, < 3)
55
+ simplecov-html (~> 0.10.0)
56
+ simplecov-html (0.10.2)
48
57
  unicode-display_width (1.4.0)
49
58
 
50
59
  PLATFORMS
@@ -52,6 +61,7 @@ PLATFORMS
52
61
 
53
62
  DEPENDENCIES
54
63
  bundler (~> 1.16)
64
+ codacy-coverage
55
65
  id_generator!
56
66
  pry
57
67
  rake (~> 10.0)
data/README.md CHANGED
@@ -1,38 +1,68 @@
1
- # Id::Generator
1
+ # IdGenerator
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/id/generator`. To experiment with that code, run `bin/console` for an interactive prompt.
3
+ [![Build Status](https://travis-ci.org/matic-insurance/id_generator.svg?branch=master)](https://travis-ci.org/matic-insurance/id_generator)
4
+ [![Codacy Badge](https://api.codacy.com/project/badge/Grade/ff827f62fb034d3b8ff71d69a9f0e233)](https://www.codacy.com/app/Matic/id_generator?utm_source=github.com&amp;utm_medium=referral&amp;utm_content=matic-insurance/id_generator&amp;utm_campaign=Badge_Grade)
4
5
 
5
- TODO: Delete this and the text above, and describe your gem
6
+ This gem provides uniq and random ids generation in distributed services
7
+ but at the same time gives some ordering (timestamp) and debug (context) information
6
8
 
7
- ## Installation
9
+ The gem is inspired by [Twitter Snowflake](https://github.com/twitter-archive/snowflake/tree/snowflake-2010) and other algorithms.
8
10
 
9
- Add this line to your application's Gemfile:
11
+ The main difference is that ids generated by gem are not fully sequenced.
12
+ We want to have large random part that can be used as a simple guard against enumeration attack.
10
13
 
11
- ```ruby
12
- gem 'id_generator'
13
- ```
14
+ ## Goals & Assumptions
14
15
 
15
- And then execute:
16
+ While we designed format and gem we used these as requirements:
16
17
 
17
- $ bundle
18
+ - Every system should be able to independently (without coordination) generate id that will never (with reasonable chances) be duplicated
19
+ - ID allows partial time ordering (timestamp in seconds)
20
+ - ID has easy system identifier (context)
21
+ - ID is human readable
22
+ - ID size is not a concern
23
+ - ID format should not be used for business/application logic - thus it remains flexible for future changes
18
24
 
19
- Or install it yourself as:
25
+ ## ID Format
20
26
 
21
- $ gem install id_generator
27
+ Suggested algorithm is based on the Twitter snowflake and other similar alghorithms:
22
28
 
23
- ## Usage
29
+ `TTTTTTTT-II-RRRRRRRRRRRRRRRRRRRRRR`
24
30
 
25
- TODO: Write usage instructions here
31
+ - `TTTTTTTT` - time in seconds since 2000 (just cool number) represented as hex - Gives us ~150 years of uniq sequences
32
+ - `II` - 1 byte for System Identification in hex
33
+ - `RRRRRRRRRRRRRRRRRRRRRR` - 11 bytes secure random number as hex
26
34
 
27
- ## Development
35
+ Code to generate is pretty straightforward:
36
+
37
+ ```ruby
38
+ time = format('%08x', Time.now.to_i - Time.new(2000).to_i)
39
+ id = '6a'
40
+ random = SecureRandom.hex(11)
41
+ id = "#{time}-#{id}-#{random}" # "21dc3680-6a-910df0665e5e29b9f89e21"
42
+ ```
43
+
44
+ ## Installation
28
45
 
29
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
46
+ Add gem to your application's Gemfile:
30
47
 
31
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
48
+ ```ruby
49
+ gem 'id_generator'
50
+ ```
51
+
52
+ ## Usage
53
+
54
+ ```ruby
55
+ # Somewhere during project start
56
+ context_id = 165 # value from 0 to 255
57
+ ID_GENERATOR = IdGenerator.new(context_id)
58
+
59
+ #Inside of the actual code
60
+ ID_GENERATOR.generate
61
+ ```
32
62
 
33
63
  ## Contributing
34
64
 
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/id_generator.
65
+ Bug reports and pull requests are welcome on GitHub at [https://github.com/matic-insurance/id_generator]().
36
66
 
37
67
  ## License
38
68
 
@@ -5,11 +5,12 @@ require 'id_generator'
5
5
  Gem::Specification.new do |spec|
6
6
  spec.name = 'id_generator'
7
7
  spec.version = IdGenerator::VERSION
8
- spec.authors = ['vnovitskyi']
8
+ spec.authors = ['Viktor Novitskyi']
9
9
  spec.email = ['vnovitskyi@gmail.com']
10
10
 
11
- spec.summary = 'Generate unique id'
12
- spec.description = 'Generate unique id'
11
+ spec.summary = 'Generator of random but at the same time timestamped and system identifiable ids'
12
+ spec.description = 'Ruby Gem that allow to generate random ids in distributed systems (micro-services)
13
+ and include debug information like timestamp and system id'
13
14
  spec.homepage = 'https://github.com/matic-insurance/id_generator'
14
15
  spec.license = 'MIT'
15
16
 
@@ -36,6 +37,7 @@ Gem::Specification.new do |spec|
36
37
  spec.require_paths = ['lib']
37
38
 
38
39
  spec.add_development_dependency 'bundler', '~> 1.16'
40
+ spec.add_development_dependency 'codacy-coverage'
39
41
  spec.add_development_dependency 'pry'
40
42
  spec.add_development_dependency 'rake', '~> 10.0'
41
43
  spec.add_development_dependency 'rspec', '~> 3.0'
@@ -1,38 +1,46 @@
1
1
  require 'securerandom'
2
2
 
3
3
  class IdGenerator
4
- VERSION = '0.1.1'.freeze
4
+ VERSION = '0.1.2'.freeze
5
+
6
+ COUNTER_PART_SIZE = 8
7
+ CONTEXT_PART_SIZE = 2
8
+
9
+ RANDOM_PART_BYTES = 11
10
+ COUNTER_START = Time.new(2000).to_i
5
11
 
6
12
  class Error < StandardError
7
13
  end
8
14
 
9
15
  def initialize(context_id)
10
- @context_id = context_id.to_i
16
+ raise(IdGenerator::Error, 'Invalid project id') unless context_id_valid?(context_id)
17
+
18
+ @context_id = value_to_hex(context_id, CONTEXT_PART_SIZE)
11
19
  end
12
20
 
13
21
  def generate
14
- validate_context_id
15
-
16
22
  "#{time}-#{@context_id}-#{random_number}"
17
23
  end
18
24
 
19
25
  private
20
26
 
21
- def validate_context_id
22
- return if context_id_valid?
27
+ def context_id_valid?(context_id)
28
+ return false unless context_id.is_a?(Integer)
29
+ return false unless context_id.between?(0, 255)
23
30
 
24
- raise(IdGenerator::Error, 'Invalid project id')
25
- end
26
-
27
- def context_id_valid?
28
- @context_id.positive?
31
+ true
29
32
  end
30
33
 
31
34
  def time
32
- (Time.now.to_i - Time.new(2014).to_i).to_s(16)
35
+ timestamp = Time.now.to_i - COUNTER_START
36
+ value_to_hex(timestamp, COUNTER_PART_SIZE)
33
37
  end
34
38
 
35
39
  def random_number
36
- SecureRandom.hex(11)
40
+ SecureRandom.hex(RANDOM_PART_BYTES)
41
+ end
42
+
43
+ def value_to_hex(value, size)
44
+ format("%0#{size}x", value)
37
45
  end
38
46
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: id_generator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
- - vnovitskyi
7
+ - Viktor Novitskyi
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-10-18 00:00:00.000000000 Z
11
+ date: 2018-10-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: codacy-coverage
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: pry
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -108,7 +122,9 @@ dependencies:
108
122
  - - ">="
109
123
  - !ruby/object:Gem::Version
110
124
  version: '0'
111
- description: Generate unique id
125
+ description: |-
126
+ Ruby Gem that allow to generate random ids in distributed systems (micro-services)
127
+ and include debug information like timestamp and system id
112
128
  email:
113
129
  - vnovitskyi@gmail.com
114
130
  executables: []
@@ -149,8 +165,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
149
165
  version: '0'
150
166
  requirements: []
151
167
  rubyforge_project:
152
- rubygems_version: 2.7.6
168
+ rubygems_version: 2.7.7
153
169
  signing_key:
154
170
  specification_version: 4
155
- summary: Generate unique id
171
+ summary: Generator of random but at the same time timestamped and system identifiable
172
+ ids
156
173
  test_files: []