humanized_id 0.1.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
+ SHA1:
3
+ metadata.gz: 503446ac0138faee8cfe6329182e48b0a84e33b7
4
+ data.tar.gz: 0acf9a7b8cafec545fb4ae7f5e209705982eb5a9
5
+ SHA512:
6
+ metadata.gz: f36d58aa96c011295a2cfe2a1492fdb32ee75005d32777959c562f69562e54a1a565999630f750c70ac67e01ba2c14c9e75987273921b8f1459def17ba8dd7ae
7
+ data.tar.gz: 00a91acd6c619c851ae898cd5315f1d849d8371c0acb0f05ae25c329ef73cecf9aef0a0c223a44606c1826773fbf42f042042fd9162e084bbd36a47dc5e81613
data/.gitignore ADDED
@@ -0,0 +1,13 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /vendor/
11
+ /.idea/
12
+ .env
13
+ .DS_Store
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.rubocop.yml ADDED
@@ -0,0 +1,14 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.3
3
+
4
+ Metrics/LineLength:
5
+ Max: 100
6
+
7
+ Documentation:
8
+ Enabled: false
9
+
10
+ Style/BlockDelimiters:
11
+ EnforcedStyle: semantic
12
+
13
+ Style/TrivialAccessors:
14
+ AllowPredicates: true
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.3.0
data/.travis.yml ADDED
@@ -0,0 +1,13 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1
4
+ - 2.2
5
+ - 2.3.0
6
+ before_install: gem install bundler -v 1.11.2
7
+ notifications:
8
+ email:
9
+ - support@travellink.com.au
10
+ flowdock:
11
+ secure: CkLwygzeYX5y3r0eg1bn8wFLYd7qLeOrbrawQb/c/sTehXvU95sE/yZMNdPKD/Mv+IQ+blVVr4NAt8MvbN20Nw/W5EtcsMo2S2msuCsVbwCkvj21Dqm+pTRoD8O2yf3zxX5T7wmZ7PoD7UfE+wIPQ5C7QunODxCPnpPlj/RCaOKejarmdQWXQaT6/lJpzoRDav2oj+bbyPCwQbYGSYWFPFwQ3FylGNp/7eCD3wSPJtmYdunJtx1yVW4wnInTEzqsQ4eJubpwEfQ8vsWq7KbeUf2Z0IaPVtSzwN7gjIncGJ1gS5MGhZkFSsNhY9BvVkWw366qkTcESB3CbF5O9vUrUyWAaiLIHZrqTaqhocZ+4Y6jWJG/gSfLGYTSlVx+qxQ/Pd75bXu6ovlL2HF9lYEL1mbVp4GrOGcQTnw2SLOnLRmsGtf5CEVBE4llQXR0ySzqLCc0Q6XBLgbvG5yg+iKsnDQRZijStUQze/rQVGbtOLxXXyv+mQolaKd475igJYZB29KS0gW3gCJrTQPLRm1D/PPIOHLFysKgPc6kE5UcPWmTrnH8hWKj2K0tiN+u5S4jostoh1riDBr+baLQ5eI9k6KVevM7t1ICiSb9A9WpRfbUtGRHirnte/MgAc0EJ/J/iAT5breaV9urnjzyaYvpH8YzXHMbuw/tlS8jdxlI8qs=
12
+ sudo: false
13
+ cache: bundler
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Akil Madan
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/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
data/Readme.md ADDED
@@ -0,0 +1,98 @@
1
+ # HumanizedId
2
+
3
+ HumanizedId is a gem designed to help you either:
4
+ - Convert an existing numerical id into a 'human friendly' alphanumerical id
5
+ - Generate a random 'human friendly' id that is of a specified or default length
6
+
7
+ A 'humanized' (or 'human friendly') id is an id that is easy to read and is based
8
+ off a character set that does not allow vague or vulgar references.
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile
13
+
14
+ ```ruby
15
+ gem 'humanized_id'
16
+ ```
17
+
18
+ And then execute:
19
+
20
+ $ bundle
21
+
22
+ Or install it yourself as:
23
+
24
+ $ gem install humanized_id
25
+
26
+ ## Usage
27
+
28
+ ### Humanize an existing id
29
+
30
+ The simplest way to call humanize is to pass in an existing numerical id,
31
+ and let HumanizedId return a human friendly version of that id. This id will be shorter
32
+ in length (due to base conversion) and will be an alphanumerical string based on
33
+ a 'human safe' character-set
34
+
35
+ ```ruby
36
+ humanized_id = HumanizedId.humanize id: 1234567
37
+ # Returns '6RDFD'
38
+ ```
39
+
40
+ #### Ensuring minimum length of output id
41
+
42
+ An optional min_length flag can be passed in order to guarantee the minimum length
43
+ of the returned value. This will be done by 'padding' the return id with the
44
+ safe-charset default value '2'
45
+
46
+ ```ruby
47
+ humanized_id = HumanizedId.humanize id: 1234567, min_length: 20
48
+ # Returns '222222222222226RDFD'
49
+ ```
50
+
51
+ Note that the original length is not preserved during the base conversion, so you
52
+ will need to explicitely pass in a min_length if you'd like a return id of the same length
53
+
54
+ Also note that if you specify a min_length shorter than the actual output id length,
55
+ the output id will not be modified (as expected)
56
+
57
+ #### Adding a prefix
58
+
59
+ Pass in a prefix if you'd like to add a prefix to your humanized id
60
+
61
+ ```ruby
62
+ humanized_id = HumanizedId.humanize id: 1234567, min_length: 20, prefix: 'TEST'
63
+ # Will return 'TEST222222222222226RDFD'
64
+ ```
65
+
66
+ The prefix is added to the humanized id after all other processing (including min_length padding).
67
+ Therefore the total length of the above example wil be 20 + 'TEST'.length = 24
68
+
69
+ ### Generating a random humanized id
70
+
71
+ Call 'generate_random' with optional length and prefix
72
+
73
+ ```ruby
74
+ random_humanized_id = HumanizedId.generate_random
75
+ ```
76
+
77
+ Will generate a random human friendly id of default length (see HumanizedId::DEFAULT_GENERATION_LENGTH)
78
+
79
+ ```ruby
80
+ random_humanized_id = HumanizedId.generate_random length: 20, prefix: 'TEST'
81
+ ```
82
+
83
+ This will generate a random humanized id of length 20 and then add the prefix to the id
84
+ thus resulting in a total length of 24
85
+
86
+ ## Development
87
+
88
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
89
+
90
+ 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` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
91
+
92
+ ## Contributing
93
+
94
+ 1. Fork it ( https://github.com/sealink/humanized_id/fork )
95
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
96
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
97
+ 4. Push to the branch (`git push origin my-new-feature`)
98
+ 5. Create a new Pull Request
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'humanized_id'
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
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,38 @@
1
+ # coding: utf-8
2
+
3
+ lib = File.expand_path('../lib', __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'humanized_id/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'humanized_id'
9
+ spec.version = HumanizedId::VERSION
10
+ spec.authors = ['Akil Madan']
11
+ spec.email = ['support@travellink.com.au']
12
+
13
+ spec.summary = 'Gem to convert an existing id to a human friendly id or ' \
14
+ 'generate a new human friendly random id'
15
+ spec.description = 'HumanizedId can be used to generate (or convert an existing id to) a ' \
16
+ 'human friendly id'
17
+ spec.homepage = 'https://github.com/sealink/humanized_id'
18
+ spec.license = 'MIT'
19
+
20
+ spec.files = `git ls-files -z`
21
+ .split("\x0")
22
+ .reject { |f| f.match(%r{^(test|spec|features)/}) }
23
+ spec.bindir = 'exe'
24
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
25
+ spec.require_paths = ['lib']
26
+
27
+ spec.required_ruby_version = '>= 2.0'
28
+
29
+ spec.add_development_dependency 'bundler'
30
+ spec.add_development_dependency 'rake'
31
+ spec.add_development_dependency 'dotenv'
32
+ spec.add_development_dependency 'rspec'
33
+ spec.add_development_dependency 'simplecov'
34
+ spec.add_development_dependency 'simplecov-rcov'
35
+ spec.add_development_dependency 'coveralls'
36
+ spec.add_development_dependency 'rubocop'
37
+ spec.add_development_dependency 'travis'
38
+ end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+ module HumanizedId
3
+ class Humanizer
4
+ SOURCE_CHARSET = %w(0123456789abcdefghijklmnopqrstuvwxyz).freeze
5
+
6
+ def initialize(id:, min_length: nil, prefix: '')
7
+ @id = id
8
+ @min_length = min_length
9
+ @prefix = prefix.nil? ? '' : prefix
10
+ @target_charset = HumanizedId::CHARACTERSET.join('')
11
+ @source_charset = SOURCE_CHARSET.join('')
12
+ end
13
+
14
+ def generate_humanized_id
15
+ new_id = convert_to_target_base id: @id
16
+ new_id = convert_to_target_charset id: new_id
17
+ new_id = resize id: new_id if @min_length
18
+ "#{@prefix}#{new_id}"
19
+ end
20
+
21
+ private
22
+
23
+ def convert_to_target_base(id: @id, target_base: @target_charset.length)
24
+ id.to_s(target_base)
25
+ rescue ArgumentError
26
+ raise Error, 'id is not an integer'
27
+ end
28
+
29
+ def convert_to_target_charset(
30
+ id: @id, target_charset: @target_charset, source_charset: @source_charset)
31
+ source_charset_subset = source_charset.slice(0, target_charset.length)
32
+ id.tr(source_charset_subset, target_charset)
33
+ end
34
+
35
+ def resize(id: @id, min_length: @min_length, target_charset: @target_charset)
36
+ if min_length > id.length
37
+ padding = target_charset[0] * (min_length - id.length)
38
+ id = "#{padding}#{id}"
39
+ end
40
+ id
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,24 @@
1
+ module HumanizedId
2
+ class RandGenerator
3
+ def initialize(prefix: '', length:)
4
+ @prefix = prefix.nil? ? '' : prefix
5
+ @length = length
6
+ @target_charset = HumanizedId::CHARACTERSET
7
+ end
8
+
9
+ def generate_random_humanized_id
10
+ "#{@prefix}#{generate_random}"
11
+ end
12
+
13
+ private
14
+
15
+ def generate_random(length: @length, target_charset: @target_charset)
16
+ SecureRandom.random_bytes(length).unpack('C*').map{ |byte|
17
+ idx = byte % 64
18
+ idx = SecureRandom.random_number(target_charset.size) if
19
+ idx >= target_charset.size
20
+ target_charset[idx]
21
+ }.join
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+ module HumanizedId
3
+ VERSION = '0.1.0'.freeze
4
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+ module HumanizedId
3
+ DEFAULT_GENERATION_LENGTH = 24
4
+ SIMILAR_NUMBERS_LETTERS = %w(0 O 1 I L 5 S 8 B).freeze
5
+ VOWELS = %w(A E I O U).freeze
6
+ CHARACTERSET =
7
+ (('0'..'9').to_a + ('A'..'Z').to_a - SIMILAR_NUMBERS_LETTERS - VOWELS).freeze
8
+
9
+ class << self
10
+ def humanize(id:, min_length: nil, prefix: '')
11
+ HumanizedId::Humanizer.new(id: id, min_length: min_length, prefix: prefix).generate_humanized_id
12
+ end
13
+
14
+ def generate_random(prefix: '', length: DEFAULT_GENERATION_LENGTH)
15
+ HumanizedId::RandGenerator.new(prefix: prefix, length: length).generate_random_humanized_id
16
+ end
17
+ end
18
+ Error = Class.new(StandardError)
19
+ end
20
+
21
+ require 'humanized_id/version'
22
+ require 'humanized_id/humanizer'
23
+ require 'humanized_id/rand_generator'
metadata ADDED
@@ -0,0 +1,189 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: humanized_id
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Akil Madan
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-02-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
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: rake
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: dotenv
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: rspec
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: simplecov
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: simplecov-rcov
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: coveralls
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: rubocop
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: travis
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: HumanizedId can be used to generate (or convert an existing id to) a
140
+ human friendly id
141
+ email:
142
+ - support@travellink.com.au
143
+ executables: []
144
+ extensions: []
145
+ extra_rdoc_files: []
146
+ files:
147
+ - ".gitignore"
148
+ - ".rspec"
149
+ - ".rubocop.yml"
150
+ - ".ruby-version"
151
+ - ".travis.yml"
152
+ - CODE_OF_CONDUCT.md
153
+ - Gemfile
154
+ - LICENSE.txt
155
+ - Rakefile
156
+ - Readme.md
157
+ - bin/console
158
+ - bin/setup
159
+ - humanized_id.gemspec
160
+ - lib/humanized_id.rb
161
+ - lib/humanized_id/humanizer.rb
162
+ - lib/humanized_id/rand_generator.rb
163
+ - lib/humanized_id/version.rb
164
+ homepage: https://github.com/sealink/humanized_id
165
+ licenses:
166
+ - MIT
167
+ metadata: {}
168
+ post_install_message:
169
+ rdoc_options: []
170
+ require_paths:
171
+ - lib
172
+ required_ruby_version: !ruby/object:Gem::Requirement
173
+ requirements:
174
+ - - ">="
175
+ - !ruby/object:Gem::Version
176
+ version: '2.0'
177
+ required_rubygems_version: !ruby/object:Gem::Requirement
178
+ requirements:
179
+ - - ">="
180
+ - !ruby/object:Gem::Version
181
+ version: '0'
182
+ requirements: []
183
+ rubyforge_project:
184
+ rubygems_version: 2.5.1
185
+ signing_key:
186
+ specification_version: 4
187
+ summary: Gem to convert an existing id to a human friendly id or generate a new human
188
+ friendly random id
189
+ test_files: []