lite-uxid 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 3951cf83ce51ac1a9dc727b8a25ade7073e5cff6016bf03954b0605cc5ba3610
4
+ data.tar.gz: 93178efa462329aabbce59deac878a431c5f8949e3cbbb8a4ba28ac19b37016f
5
+ SHA512:
6
+ metadata.gz: e4c1ef9780039a2d35cf7c5b03edd224174fe9bab6b650fad3d7d740166435416ce6ef76dd15cde84b503c3610c600ae8d95389895c700d5ab96682f2adfba68
7
+ data.tar.gz: abeb96b3997ba18821c0ef0fc24e66e876217b6fca21cbdbd6c92d99f55dbcbe47e342c22989b190bca0655806ec2eaa7509a374980f996d0ab41b96943168b2
data/.fasterer.yml ADDED
@@ -0,0 +1,19 @@
1
+ speedups:
2
+ block_vs_symbol_to_proc: true
3
+ each_with_index_vs_while: false
4
+ fetch_with_argument_vs_block: true
5
+ for_loop_vs_each: true
6
+ getter_vs_attr_reader: true
7
+ gsub_vs_tr: true
8
+ hash_merge_bang_vs_hash_brackets: true
9
+ keys_each_vs_each_key: true
10
+ map_flatten_vs_flat_map: true
11
+ module_eval: true
12
+ proc_call_vs_yield: true
13
+ rescue_vs_respond_to: true
14
+ reverse_each_vs_reverse_each: true
15
+ select_first_vs_detect: true
16
+ select_last_vs_reverse_detect: true
17
+ setter_vs_attr_writer: true
18
+ shuffle_first_vs_sample: true
19
+ sort_vs_sort_by: true
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,4 @@
1
+ --backtrace
2
+ --color
3
+ --format progress
4
+ --order random
data/.rubocop.yml ADDED
@@ -0,0 +1,24 @@
1
+ require:
2
+ - rubocop-performance
3
+ - rubocop-rspec
4
+ AllCops:
5
+ TargetRubyVersion: 2.6
6
+ DisplayCopNames: true
7
+ DisplayStyleGuide: true
8
+ LineLength:
9
+ Max: 100
10
+ Layout/EmptyLinesAroundBlockBody:
11
+ Exclude:
12
+ - 'spec/**/**/*'
13
+ Layout/EmptyLinesAroundClassBody:
14
+ EnforcedStyle: empty_lines_except_namespace
15
+ Layout/EmptyLinesAroundModuleBody:
16
+ EnforcedStyle: empty_lines_except_namespace
17
+ Metrics/BlockLength:
18
+ Exclude:
19
+ - 'spec/**/**/*'
20
+ - '*.gemspec'
21
+ Style/Documentation:
22
+ Enabled: false
23
+ Style/ExpandPathArguments:
24
+ Enabled: false
data/.travis.yml ADDED
@@ -0,0 +1,24 @@
1
+ sudo: false
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.5
6
+ - 2.6
7
+ - ruby-head
8
+ matrix:
9
+ fast_finish: true
10
+ allow_failures:
11
+ - rvm: ruby-head
12
+ before_install:
13
+ - gem update --system
14
+ - gem install bundler
15
+ install:
16
+ - bundle install --jobs=3 --retry=3
17
+ script:
18
+ - bundle exec rspec
19
+ - bundle exec rubocop
20
+ - bundle exec fasterer
21
+ notifications:
22
+ email: false
23
+ slack:
24
+ secure: tGjUBos/5ypoNmEc2L05h0qTKpizE/g/Mx5McqJo390y3NniU9Quc7ci8JqTzPJq3kvZugMKo33SMvOFBN6drwcNN6I3BQyfGv77ZIt9coJOnz3ASr55fLtnO5gPcDxYdRAV5sOXBtaudY39mXRby/YEJx7PX6AE2NHpdhqwco3gsTWp77VxQ0cHxyPUvW0v+vEBaozzsduUKh1pH6+OckzH9/7lBQSGdljRAlhOM4KBnYD0bttw9bZk1QTyoGnmjAReTpbIPaIPFG28d9uVWD2WgQvyMGjpjdEzM8vTa7htNyfT71Hu7gToUDU9vknHQINuDPsBfpMZqKDtEUbGQwgfRAsdEzoBX/QRh9ArWDQ+ycmX+UuDT7+E5lt73Y6CX59EECMYQKM9LVNd08utDkhfYPH5r0MUs5kNblFi4NIUtgbHKFw2b4lmzNYuhdSUwVoUgdpW8NHtg4I4LzdTgFMI5or8r8Pbn2djGiGYq9kMC8eu5BksiAlZq8w0UcMlGz+uRtbpkf9IJNB+VuaeXohqIT8NKGfcC1j7kbbxrvQEbOW36TA09n34IGNF7iS/FWL4Rb2OhrsE16VPZ397GbSDXZsH7JzB1E3BW2lhONrw3U332RW+QSBxCdQZ9ig+xsoxfOfJVoqho6jHoNxJxDYvo7WWTY2Ashb7yu62RNo=
data/CHANGELOG.md ADDED
@@ -0,0 +1,11 @@
1
+ # Changelog
2
+ All notable changes to this project will be documented in this file.
3
+
4
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
5
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6
+
7
+ ## [Unreleased]
8
+
9
+ ## [1.0.0] - 2019-06-25
10
+ ### Added
11
+ - Initial project version
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at j.gomez@drexed.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in lite-uxid.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,132 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ lite-uxid (1.0.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ actionpack (5.2.3)
10
+ actionview (= 5.2.3)
11
+ activesupport (= 5.2.3)
12
+ rack (~> 2.0)
13
+ rack-test (>= 0.6.3)
14
+ rails-dom-testing (~> 2.0)
15
+ rails-html-sanitizer (~> 1.0, >= 1.0.2)
16
+ actionview (5.2.3)
17
+ activesupport (= 5.2.3)
18
+ builder (~> 3.1)
19
+ erubi (~> 1.4)
20
+ rails-dom-testing (~> 2.0)
21
+ rails-html-sanitizer (~> 1.0, >= 1.0.3)
22
+ activemodel (5.2.3)
23
+ activesupport (= 5.2.3)
24
+ activerecord (5.2.3)
25
+ activemodel (= 5.2.3)
26
+ activesupport (= 5.2.3)
27
+ arel (>= 9.0)
28
+ activesupport (5.2.3)
29
+ concurrent-ruby (~> 1.0, >= 1.0.2)
30
+ i18n (>= 0.7, < 2)
31
+ minitest (~> 5.1)
32
+ tzinfo (~> 1.1)
33
+ arel (9.0.0)
34
+ ast (2.4.0)
35
+ builder (3.2.3)
36
+ colorize (0.8.1)
37
+ concurrent-ruby (1.1.5)
38
+ crass (1.0.4)
39
+ database_cleaner (1.7.0)
40
+ diff-lcs (1.3)
41
+ erubi (1.8.0)
42
+ fasterer (0.5.1)
43
+ colorize (~> 0.7)
44
+ ruby_parser (>= 3.13.0)
45
+ generator_spec (0.9.4)
46
+ activesupport (>= 3.0.0)
47
+ railties (>= 3.0.0)
48
+ i18n (1.6.0)
49
+ concurrent-ruby (~> 1.0)
50
+ jaro_winkler (1.5.3)
51
+ loofah (2.2.3)
52
+ crass (~> 1.0.2)
53
+ nokogiri (>= 1.5.9)
54
+ method_source (0.9.2)
55
+ mini_portile2 (2.4.0)
56
+ minitest (5.11.3)
57
+ nokogiri (1.10.3)
58
+ mini_portile2 (~> 2.4.0)
59
+ parallel (1.17.0)
60
+ parser (2.6.3.0)
61
+ ast (~> 2.4.0)
62
+ rack (2.0.7)
63
+ rack-test (1.1.0)
64
+ rack (>= 1.0, < 3)
65
+ rails-dom-testing (2.0.3)
66
+ activesupport (>= 4.2.0)
67
+ nokogiri (>= 1.6)
68
+ rails-html-sanitizer (1.0.4)
69
+ loofah (~> 2.2, >= 2.2.2)
70
+ railties (5.2.3)
71
+ actionpack (= 5.2.3)
72
+ activesupport (= 5.2.3)
73
+ method_source
74
+ rake (>= 0.8.7)
75
+ thor (>= 0.19.0, < 2.0)
76
+ rainbow (3.0.0)
77
+ rake (12.3.2)
78
+ rspec (3.8.0)
79
+ rspec-core (~> 3.8.0)
80
+ rspec-expectations (~> 3.8.0)
81
+ rspec-mocks (~> 3.8.0)
82
+ rspec-core (3.8.1)
83
+ rspec-support (~> 3.8.0)
84
+ rspec-expectations (3.8.4)
85
+ diff-lcs (>= 1.2.0, < 2.0)
86
+ rspec-support (~> 3.8.0)
87
+ rspec-mocks (3.8.1)
88
+ diff-lcs (>= 1.2.0, < 2.0)
89
+ rspec-support (~> 3.8.0)
90
+ rspec-support (3.8.2)
91
+ rubocop (0.72.0)
92
+ jaro_winkler (~> 1.5.1)
93
+ parallel (~> 1.10)
94
+ parser (>= 2.6)
95
+ rainbow (>= 2.2.2, < 4.0)
96
+ ruby-progressbar (~> 1.7)
97
+ unicode-display_width (>= 1.4.0, < 1.7)
98
+ rubocop-performance (1.4.0)
99
+ rubocop (>= 0.71.0)
100
+ rubocop-rspec (1.33.0)
101
+ rubocop (>= 0.60.0)
102
+ ruby-progressbar (1.10.1)
103
+ ruby_parser (3.13.1)
104
+ sexp_processor (~> 4.9)
105
+ sexp_processor (4.12.1)
106
+ sqlite3 (1.4.1)
107
+ thor (0.20.3)
108
+ thread_safe (0.3.6)
109
+ tzinfo (1.2.5)
110
+ thread_safe (~> 0.1)
111
+ unicode-display_width (1.6.0)
112
+
113
+ PLATFORMS
114
+ ruby
115
+
116
+ DEPENDENCIES
117
+ activerecord
118
+ activesupport
119
+ bundler
120
+ database_cleaner
121
+ fasterer
122
+ generator_spec
123
+ lite-uxid!
124
+ rake
125
+ rspec
126
+ rubocop
127
+ rubocop-performance
128
+ rubocop-rspec
129
+ sqlite3
130
+
131
+ BUNDLED WITH
132
+ 2.0.1
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Juan Gomez
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,126 @@
1
+ # Lite::Uxid
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/lite-uxid.svg)](http://badge.fury.io/rb/lite-uxid)
4
+ [![Build Status](https://travis-ci.org/drexed/lite-uxid.svg?branch=master)](https://travis-ci.org/drexed/lite-uxid)
5
+
6
+ Lite::Uxid is a library for generating or obfuscating Id's based on Hash and ULID algorithms.
7
+ It's very useful to hide the number of resources in your database and protect against enumeration attacks.
8
+
9
+ **NOTE:** If you are coming from `ActiveUxid`, please read the [port](#port) section.
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem 'lite-uxid'
17
+ ```
18
+
19
+ And then execute:
20
+
21
+ $ bundle
22
+
23
+ Or install it yourself as:
24
+
25
+ $ gem install lite-uxid
26
+
27
+ ## Table of Contents
28
+
29
+ * [Configuration](#configuration)
30
+ * [Hash](#hash)
31
+ * [ULID](#ulid)
32
+ * [ActiveRecord](#active_record)
33
+ * [Port](#port)
34
+
35
+ ## Configuration
36
+
37
+ `rails g lite:uxid:install` will generate the following file:
38
+ `../config/initalizers/lite-uxid.rb`
39
+
40
+ ```ruby
41
+ Lite::Uxid.configure do |config|
42
+ config.encoding_chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
43
+ config.encoding_length = 26
44
+ config.encoding_salt = 1_369_136
45
+ end
46
+ ```
47
+
48
+ ## Hash
49
+
50
+ Hash ID's are reversible but less performant than ULID.
51
+
52
+ ```ruby
53
+ Lite::Uxid::Hash.encode(10) #=> 'q5D8inm0'
54
+ Lite::Uxid::Hash.decode('q5D8inm0') #=> 10
55
+ ```
56
+
57
+ ## ULID
58
+
59
+ ULID are not reversible and more performant than Hash Id's.
60
+
61
+ ```ruby
62
+ Lite::Uxid::Ulid.encode #=> '1mqfg9qa96s8s5f02o1ucf8lcc'
63
+ ```
64
+
65
+ ## ActiveRecord
66
+
67
+ **Table**
68
+
69
+ Add the following attribute to all corresponding tables.
70
+
71
+ ```ruby
72
+ # omitted
73
+ t.binary :uxid, limit: 16, index: { unique: true }
74
+ # omitted
75
+ ```
76
+
77
+ **Setup**
78
+
79
+ All `uxid` attributes will be automatically generated and applied when the record is created.
80
+
81
+ ```ruby
82
+ class User < ActiveRecord::Base
83
+ include Lite::Uxid::Record::Hash
84
+
85
+ # Or
86
+
87
+ include Lite::Uxid::Record::Ulid
88
+ end
89
+ ```
90
+
91
+ **Usage**
92
+
93
+ The following methods are for Hash based Uxid's.
94
+
95
+ ```ruby
96
+ User.find_by_uxid('x123') #=> Find record by uxid
97
+ User.find_by_uxid!('x123') #=> Raises an ActiveRecord::RecordNotFound error if not found
98
+
99
+ user = User.new
100
+ user.uxid_to_id #=> Decodes the records uxid to id (only for Hash based Id's)
101
+ ```
102
+
103
+ ## Port
104
+
105
+ `Lite::Uxid` is compatible port of [ActiveUxid](https://github.com/drexed/active_uxid).
106
+
107
+ Switching is as easy as renaming `ActiveUxid::Hash` to `Lite::Uxid::Hash`
108
+ and `ActiveUxid::Ulid` to `Lite::Uxid::Ulid`.
109
+
110
+ ## Development
111
+
112
+ 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.
113
+
114
+ 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).
115
+
116
+ ## Contributing
117
+
118
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/lite-uxid. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
119
+
120
+ ## License
121
+
122
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
123
+
124
+ ## Code of Conduct
125
+
126
+ Everyone interacting in the Lite::Uxid project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/lite-uxid/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
data/_config.yml ADDED
@@ -0,0 +1 @@
1
+ theme: jekyll-theme-leap-day
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'lite/uxid'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require 'irb'
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails/generators'
4
+
5
+ module Lite
6
+ module Uxid
7
+ class InstallGenerator < Rails::Generators::Base
8
+
9
+ source_root File.expand_path('../templates', __FILE__)
10
+
11
+ def copy_initializer_file
12
+ copy_file('install.rb', 'config/initializers/lite-uxid.rb')
13
+ end
14
+
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ Lite::Uxid.configure do |config|
4
+ config.encoding_chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
5
+ config.encoding_length = 26
6
+ config.encoding_salt = 1_369_136
7
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lite
4
+ module Uxid
5
+ class Base
6
+
7
+ def initialize
8
+ @config = Lite::Uxid.configuration
9
+ end
10
+
11
+ def encoding_base
12
+ encoding_chars.length
13
+ end
14
+
15
+ Lite::Uxid.configuration.instance_variables.each do |setting|
16
+ setting = setting.to_s.tr(':@', '')
17
+ define_method(setting) { @config.send(setting) }
18
+ end
19
+
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lite
4
+ module Uxid
5
+
6
+ class Configuration
7
+
8
+ attr_accessor :encoding_chars, :encoding_length, :encoding_salt
9
+
10
+ def initialize
11
+ @encoding_chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
12
+ @encoding_length = 26
13
+ @encoding_salt = 1_369_136
14
+ end
15
+
16
+ end
17
+
18
+ def self.configuration
19
+ @configuration ||= Configuration.new
20
+ end
21
+
22
+ def self.configuration=(config)
23
+ @configuration = config
24
+ end
25
+
26
+ def self.configure
27
+ yield(configuration)
28
+ end
29
+
30
+ end
31
+ end
@@ -0,0 +1,65 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lite
4
+ module Uxid
5
+ class Hash < Lite::Uxid::Base
6
+
7
+ def initialize(id)
8
+ @id = id
9
+ super()
10
+ end
11
+
12
+ class << self
13
+
14
+ def encode(id)
15
+ klass = new(id)
16
+ klass.encode_uxid
17
+ end
18
+
19
+ end
20
+
21
+ def self.decode(id)
22
+ klass = new(id)
23
+ klass.decode_uxid
24
+ end
25
+
26
+ def encode_uxid
27
+ uxid_encode_chars((@id + encoding_salt) << encoding_length)
28
+ end
29
+
30
+ def decode_uxid
31
+ (uxid_decode_chars(@id) >> encoding_length) - encoding_salt
32
+ end
33
+
34
+ def uxid_encode_chars(id)
35
+ return '0' if id.zero?
36
+ return nil if id.negative?
37
+
38
+ str = ''
39
+
40
+ while id.positive?
41
+ str = "#{encoding_chars[id % encoding_base]}#{str}"
42
+ id /= encoding_base
43
+ end
44
+
45
+ str
46
+ end
47
+
48
+ def uxid_decode_chars(id)
49
+ pos = 0
50
+ num = 0
51
+ len = id.length
52
+ max = len - 1
53
+
54
+ while pos < len
55
+ pow = encoding_base**(max - pos)
56
+ num += encoding_chars.index(id[pos]) * pow
57
+ pos += 1
58
+ end
59
+
60
+ num
61
+ end
62
+
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_record'
4
+ require 'active_support'
5
+
6
+ module Lite
7
+ module Uxid
8
+ module Record
9
+ module Hash
10
+
11
+ extend ActiveSupport::Concern
12
+
13
+ included do
14
+ after_create :callback_generate_uxid!, if: proc { respond_to?(:uxid) }
15
+
16
+ def self.find_by_uxid(uxid)
17
+ decoded_id = Lite::Uxid::Hash.decode(uxid)
18
+ find_by(id: decoded_id)
19
+ end
20
+
21
+ def self.find_by_uxid!(uxid)
22
+ record = find_by_uxid(uxid)
23
+ return record unless record.nil?
24
+
25
+ raise ActiveRecord::RecordNotFound
26
+ end
27
+ end
28
+
29
+ def uxid_to_id
30
+ return unless respond_to?(:uxid)
31
+
32
+ Lite::Uxid::Hash.decode(uxid)
33
+ end
34
+
35
+ private
36
+
37
+ def callback_generate_uxid!
38
+ hash = Lite::Uxid::Hash.encode(id)
39
+ update_column(:uxid, hash)
40
+ end
41
+
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_record'
4
+ require 'active_support'
5
+
6
+ module Lite
7
+ module Uxid
8
+ module Record
9
+ module Ulid
10
+
11
+ extend ActiveSupport::Concern
12
+
13
+ included do
14
+ before_create :callback_generate_uxid!, if: proc { respond_to?(:uxid) }
15
+
16
+ def self.find_by_uxid(uxid)
17
+ find_by(uxid: uxid)
18
+ end
19
+
20
+ def self.find_by_uxid!(uxid)
21
+ record = find_by_uxid(uxid)
22
+ return record unless record.nil?
23
+
24
+ raise ActiveRecord::RecordNotFound
25
+ end
26
+ end
27
+
28
+ private
29
+
30
+ def callback_generate_uxid!
31
+ self.uxid = Lite::Uxid::Ulid.encode
32
+ end
33
+
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'securerandom'
4
+
5
+ module Lite
6
+ module Uxid
7
+ class Ulid < Lite::Uxid::Base
8
+
9
+ class << self
10
+
11
+ def encode
12
+ klass = new
13
+ klass.uxid_encode
14
+ end
15
+
16
+ end
17
+
18
+ def uxid_encode
19
+ (1..encoding_length).reduce('') do |str, num|
20
+ shift = 128 - 5 * num
21
+ "#{str}#{encoding_chars[(uxid_octect >> shift) & 0x1f]}"
22
+ end
23
+ end
24
+
25
+ def uxid_bytes
26
+ "#{uxid_unixtime_48bit}#{SecureRandom.random_bytes(10)}"
27
+ end
28
+
29
+ def uxid_octect
30
+ (hi, lo) = uxid_bytes.unpack('Q>Q>')
31
+ (hi << 64) | lo
32
+ end
33
+
34
+ def uxid_unixtime_flex
35
+ time = Time.respond_to?(:current) ? Time.current : Time.now
36
+ (time.to_f * 10_000).to_i
37
+ end
38
+
39
+ def uxid_unixtime_48bit
40
+ [uxid_unixtime_flex].pack('Q>')[2..-1]
41
+ end
42
+
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lite
4
+ module Uxid
5
+
6
+ VERSION ||= '1.0.0'
7
+
8
+ end
9
+ end
data/lib/lite/uxid.rb ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ %w[version configuration base].each do |name|
4
+ require "lite/uxid/#{name}"
5
+ end
6
+
7
+ %w[hash ulid].each do |name|
8
+ require "lite/uxid/record/#{name}"
9
+ require "lite/uxid/#{name}"
10
+ end
11
+
12
+ require 'generators/lite/uxid/install_generator'
data/lite-uxid.gemspec ADDED
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('../lib', __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'lite/uxid/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'lite-uxid'
9
+ spec.version = Lite::Uxid::VERSION
10
+ spec.authors = ['Juan Gomez']
11
+ spec.email = %w[j.gomez@drexed.com]
12
+
13
+ spec.summary = "Generate or obfuscate Hash or ULID based Id's"
14
+ spec.homepage = 'http://drexed.github.io/lite-uxid'
15
+ spec.license = 'MIT'
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata.merge(
21
+ 'allowed_push_host' => 'https://rubygems.org',
22
+ 'changelog_uri' => 'https://github.com/drexed/lite-uxid/blob/master/CHANGELOG.md',
23
+ 'homepage_uri' => spec.homepage,
24
+ 'source_code_uri' => 'https://github.com/drexed/lite-uxid'
25
+ )
26
+ else
27
+ raise 'RubyGems 2.0 or newer is required to protect against ' \
28
+ 'public gem pushes.'
29
+ end
30
+
31
+ # Specify which files should be added to the gem when it is released.
32
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
33
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
34
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
35
+ end
36
+ spec.bindir = 'exe'
37
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
38
+ spec.require_paths = %w[lib]
39
+
40
+ spec.add_development_dependency 'activerecord'
41
+ spec.add_development_dependency 'activesupport'
42
+ spec.add_development_dependency 'bundler'
43
+ spec.add_development_dependency 'database_cleaner'
44
+ spec.add_development_dependency 'fasterer'
45
+ spec.add_development_dependency 'generator_spec'
46
+ spec.add_development_dependency 'rake'
47
+ spec.add_development_dependency 'rspec'
48
+ spec.add_development_dependency 'rubocop'
49
+ spec.add_development_dependency 'rubocop-performance'
50
+ spec.add_development_dependency 'rubocop-rspec'
51
+ spec.add_development_dependency 'sqlite3'
52
+ end
metadata ADDED
@@ -0,0 +1,237 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lite-uxid
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Juan Gomez
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-06-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
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'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: database_cleaner
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'
69
+ - !ruby/object:Gem::Dependency
70
+ name: fasterer
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: generator_spec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rake
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rspec
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: rubocop
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
+ - !ruby/object:Gem::Dependency
140
+ name: rubocop-performance
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ - !ruby/object:Gem::Dependency
154
+ name: rubocop-rspec
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - ">="
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
167
+ - !ruby/object:Gem::Dependency
168
+ name: sqlite3
169
+ requirement: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - ">="
172
+ - !ruby/object:Gem::Version
173
+ version: '0'
174
+ type: :development
175
+ prerelease: false
176
+ version_requirements: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - ">="
179
+ - !ruby/object:Gem::Version
180
+ version: '0'
181
+ description:
182
+ email:
183
+ - j.gomez@drexed.com
184
+ executables: []
185
+ extensions: []
186
+ extra_rdoc_files: []
187
+ files:
188
+ - ".fasterer.yml"
189
+ - ".gitignore"
190
+ - ".rspec"
191
+ - ".rubocop.yml"
192
+ - ".travis.yml"
193
+ - CHANGELOG.md
194
+ - CODE_OF_CONDUCT.md
195
+ - Gemfile
196
+ - Gemfile.lock
197
+ - LICENSE.txt
198
+ - README.md
199
+ - Rakefile
200
+ - _config.yml
201
+ - bin/console
202
+ - bin/setup
203
+ - lib/generators/lite/uxid/install_generator.rb
204
+ - lib/generators/lite/uxid/templates/install.rb
205
+ - lib/lite/uxid.rb
206
+ - lib/lite/uxid/base.rb
207
+ - lib/lite/uxid/configuration.rb
208
+ - lib/lite/uxid/hash.rb
209
+ - lib/lite/uxid/record/hash.rb
210
+ - lib/lite/uxid/record/ulid.rb
211
+ - lib/lite/uxid/ulid.rb
212
+ - lib/lite/uxid/version.rb
213
+ - lite-uxid.gemspec
214
+ homepage: http://drexed.github.io/lite-uxid
215
+ licenses:
216
+ - MIT
217
+ metadata: {}
218
+ post_install_message:
219
+ rdoc_options: []
220
+ require_paths:
221
+ - lib
222
+ required_ruby_version: !ruby/object:Gem::Requirement
223
+ requirements:
224
+ - - ">="
225
+ - !ruby/object:Gem::Version
226
+ version: '0'
227
+ required_rubygems_version: !ruby/object:Gem::Requirement
228
+ requirements:
229
+ - - ">="
230
+ - !ruby/object:Gem::Version
231
+ version: '0'
232
+ requirements: []
233
+ rubygems_version: 3.0.4
234
+ signing_key:
235
+ specification_version: 4
236
+ summary: Generate or obfuscate Hash or ULID based Id's
237
+ test_files: []