hash_ids 0.0.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 +7 -0
- data/.gitignore +14 -0
- data/.rspec +2 -0
- data/.travis.yml +3 -0
- data/Gemfile +5 -0
- data/LICENSE.txt +22 -0
- data/README.md +31 -0
- data/Rakefile +7 -0
- data/hash_ids.gemspec +25 -0
- data/lib/hash_ids/configuration.rb +12 -0
- data/lib/hash_ids/obfuscate.rb +58 -0
- data/lib/hash_ids/version.rb +3 -0
- data/lib/hash_ids.rb +30 -0
- data/spec/hash_ids_spec.rb +55 -0
- data/spec/spec_helper.rb +2 -0
- metadata +116 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: e6a52af0beb9680c0df6cae64443acf7a40dbd9c
|
4
|
+
data.tar.gz: 04f672bc36f6772d3a723de505d05113de90af2f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 0cf493b119e120d849e9b15a078b0a80d928efcc103952b574f8130ce0ca01bb8b7a390a68077cacec06c62fe1ab06f585ea80b6532f02a58cf417bfed87091f
|
7
|
+
data.tar.gz: 9a7fc89a22e818526965983db2b59547d425e51f4fb955ab76ae11a943e6de236078078228f227205a4266906c8c618dc22bb0574e9f3d274e0c0b27894dd806
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 kemingcao
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
# HashIds
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'hash_ids'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install hash_ids
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
TODO: Write usage instructions here
|
24
|
+
|
25
|
+
## Contributing
|
26
|
+
|
27
|
+
1. Fork it ( https://github.com/[my-github-username]/hash_ids/fork )
|
28
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
29
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
30
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
31
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/hash_ids.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'hash_ids/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "hash_ids"
|
8
|
+
spec.version = HashIds::VERSION
|
9
|
+
spec.authors = ["kemingcao"]
|
10
|
+
spec.email = ["keming.cao@gmail.com"]
|
11
|
+
spec.summary = %q{Obfuscate id like a foodie.}
|
12
|
+
spec.description = %q{Obfuscate id on your record.}
|
13
|
+
spec.homepage = "https://github.com/kemingcao/hash_ids"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_dependency 'hashids', '~> 1.0'
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
23
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
24
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
25
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
module HashIds
|
3
|
+
class Configuration
|
4
|
+
attr_accessor :salt, :min_hash_length, :alphabet
|
5
|
+
|
6
|
+
def initialize
|
7
|
+
@min_hash_length = 16
|
8
|
+
@alphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'
|
9
|
+
@salt = @alphabet
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
module HashIds
|
4
|
+
module Obfuscate
|
5
|
+
def hash_as_id
|
6
|
+
extend ClassMethods
|
7
|
+
include InstanceMethods
|
8
|
+
end
|
9
|
+
|
10
|
+
module ClassMethods
|
11
|
+
|
12
|
+
def super_able? args
|
13
|
+
primary_key.nil? ||
|
14
|
+
default_scopes.any? ||
|
15
|
+
columns_hash.include?(inheritance_column) ||
|
16
|
+
args.first.kind_of?(Hash)
|
17
|
+
end
|
18
|
+
|
19
|
+
def find(*args)
|
20
|
+
return super if args.first.kind_of?(Symbol)
|
21
|
+
return super if block_given? || super_able?(args)
|
22
|
+
|
23
|
+
scope = args
|
24
|
+
if scope.kind_of?(Array)
|
25
|
+
scope.map! do |id|
|
26
|
+
hash_ids?(id) ? hash_ids_decode(id).first : id
|
27
|
+
end
|
28
|
+
scope.reject &:nil?
|
29
|
+
end
|
30
|
+
super(scope)
|
31
|
+
end
|
32
|
+
|
33
|
+
def hash_ids_encode ids
|
34
|
+
HashIds::hasher.encode ids
|
35
|
+
end
|
36
|
+
|
37
|
+
def hash_ids_decode ids
|
38
|
+
HashIds::hasher.decode ids
|
39
|
+
end
|
40
|
+
|
41
|
+
def hash_ids? ids
|
42
|
+
ids = ids || ''
|
43
|
+
ds = HashIds::hasher.decode ids.to_s
|
44
|
+
return true unless ds.empty?
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
module InstanceMethods
|
49
|
+
def to_param
|
50
|
+
if self.id
|
51
|
+
self.class.hash_ids_encode self.id
|
52
|
+
else
|
53
|
+
nil
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
data/lib/hash_ids.rb
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
require "hashids"
|
3
|
+
require "hash_ids/version"
|
4
|
+
require "hash_ids/obfuscate"
|
5
|
+
require "hash_ids/configuration"
|
6
|
+
|
7
|
+
module HashIds
|
8
|
+
include Obfuscate
|
9
|
+
attr_reader :hasher
|
10
|
+
|
11
|
+
class << self
|
12
|
+
def config
|
13
|
+
#
|
14
|
+
# salt = secret salt
|
15
|
+
# min_hash_length = hash length
|
16
|
+
# alphabet = default is a..z + A..Z + 0..9
|
17
|
+
#
|
18
|
+
@config ||= Configuration.new
|
19
|
+
yield @config if block_given?
|
20
|
+
@config
|
21
|
+
end
|
22
|
+
|
23
|
+
def hasher
|
24
|
+
@hasher ||= Hashids.new config.salt, config.min_hash_length, config.alphabet
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
|
30
|
+
ActiveRecord::Base.extend HashIds
|
@@ -0,0 +1,55 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
class Foodie < ActiveRecord::Base
|
4
|
+
extend HashIds
|
5
|
+
hash_as_id
|
6
|
+
|
7
|
+
attr_reader :id
|
8
|
+
def id
|
9
|
+
@id ||= Random.new.rand 10000
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
describe HashIds do
|
14
|
+
|
15
|
+
it 'has a version number' do
|
16
|
+
expect(HashIds::VERSION).not_to be nil
|
17
|
+
end
|
18
|
+
|
19
|
+
before :all do
|
20
|
+
@chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'
|
21
|
+
|
22
|
+
@salt = @chars.split('').sample(10).join('')
|
23
|
+
@min_hash_length = Random.new.rand 2...20
|
24
|
+
@alphabet = @chars.split('').sample(18).join('')
|
25
|
+
|
26
|
+
HashIds.config do |c|
|
27
|
+
c.salt = @salt
|
28
|
+
c.min_hash_length = @min_hash_length
|
29
|
+
c.alphabet = @alphabet
|
30
|
+
end
|
31
|
+
|
32
|
+
@foodie = Foodie.new
|
33
|
+
end
|
34
|
+
|
35
|
+
|
36
|
+
describe 'HashIds config and hasher' do
|
37
|
+
it 'test config' do
|
38
|
+
expect(HashIds.config.salt).to eq @salt
|
39
|
+
expect(HashIds.config.min_hash_length).to eq @min_hash_length
|
40
|
+
expect(HashIds.config.alphabet).to eq @alphabet
|
41
|
+
end
|
42
|
+
|
43
|
+
it 'test hasher' do
|
44
|
+
expect(HashIds.hasher).not_to be nil
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
describe 'test hasher' do
|
49
|
+
it 'to_parmas' do
|
50
|
+
expect(@foodie.to_param).not_to be nil
|
51
|
+
expect(@foodie.to_param).to eq @foodie.hash_ids_encode(@foodie.id)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,116 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hash_ids
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- kemingcao
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-02-03 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.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.7'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.7'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.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: '3.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.0'
|
69
|
+
description: Obfuscate id on your record.
|
70
|
+
email:
|
71
|
+
- keming.cao@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".rspec"
|
78
|
+
- ".travis.yml"
|
79
|
+
- Gemfile
|
80
|
+
- LICENSE.txt
|
81
|
+
- README.md
|
82
|
+
- Rakefile
|
83
|
+
- hash_ids.gemspec
|
84
|
+
- lib/hash_ids.rb
|
85
|
+
- lib/hash_ids/configuration.rb
|
86
|
+
- lib/hash_ids/obfuscate.rb
|
87
|
+
- lib/hash_ids/version.rb
|
88
|
+
- spec/hash_ids_spec.rb
|
89
|
+
- spec/spec_helper.rb
|
90
|
+
homepage: https://github.com/kemingcao/hash_ids
|
91
|
+
licenses:
|
92
|
+
- MIT
|
93
|
+
metadata: {}
|
94
|
+
post_install_message:
|
95
|
+
rdoc_options: []
|
96
|
+
require_paths:
|
97
|
+
- lib
|
98
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
99
|
+
requirements:
|
100
|
+
- - ">="
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: '0'
|
103
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
104
|
+
requirements:
|
105
|
+
- - ">="
|
106
|
+
- !ruby/object:Gem::Version
|
107
|
+
version: '0'
|
108
|
+
requirements: []
|
109
|
+
rubyforge_project:
|
110
|
+
rubygems_version: 2.4.5
|
111
|
+
signing_key:
|
112
|
+
specification_version: 4
|
113
|
+
summary: Obfuscate id like a foodie.
|
114
|
+
test_files:
|
115
|
+
- spec/hash_ids_spec.rb
|
116
|
+
- spec/spec_helper.rb
|