sequel-hash_id 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +7 -0
  2. data/README.md +64 -0
  3. data/lib/sequel/plugins/hash_id.rb +58 -0
  4. metadata +115 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f0fda23b727eff09bf0c2047993946945ba8999f
4
+ data.tar.gz: 9ee99e696a6960f612ce546ff3d46731b7724f02
5
+ SHA512:
6
+ metadata.gz: 90cf852bcca48f78ecaffe374e91e54756e87f2592ef304a30f9c750610f417213aa1c91d7ca35350f29aa4b1a814ee5897331fba884503fe110d108231c95a0
7
+ data.tar.gz: ab19e431be943c905c83cf59582a6f1d28cc08b21df026529daf4fad7b4bbb54b9c2acbdcd56a24abb2182220484118fe8fda198cbba23529837e1bfc2b8568a
@@ -0,0 +1,64 @@
1
+ # Sequel Hash ID
2
+
3
+ Easily obfuscate your Integer-based primary keys in Sequel models. The anonymity
4
+ of a UUID without any overhead.
5
+
6
+ These are commonly used for URL shorteners, but they have other use cases as well.
7
+ Specifically, they are very handy to prevent nieve crawling / scraping, where your
8
+ records are known to be incremental.
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ ```ruby
15
+ gem "sequel-hash_id"
16
+ ```
17
+
18
+ And then execute:
19
+
20
+ $ bundle
21
+
22
+ Or install it yourself as:
23
+
24
+ $ gem install sequel-hash_id
25
+
26
+ ## Usage
27
+
28
+ This plugin likely only makes sense at the Model level, so configure it
29
+ for each model you want to have a hashid for. The only required option
30
+ is the salt you wish to use.
31
+
32
+ ```ruby
33
+ plugin :hash_id, salt: "your-salt".freeze
34
+ ```
35
+
36
+ Once you've done that, you can now access the following methods:
37
+
38
+ ```ruby
39
+ # Get the instance's hashid
40
+ instance = YourModel.create
41
+ instance.hashid
42
+
43
+ # Lookup using a hashid
44
+ YourModel.with_hashid("the-hashid")
45
+
46
+ # Dataset lookup method
47
+ YourModel.where { ... }.with_hashid("the-hashid")
48
+ ```
49
+
50
+ ## Contributing
51
+
52
+ Bug reports and pull requests are welcome on GitHub at https://github.com/adam12/sequel-hash_id.
53
+
54
+ I love pull requests! If you fork this project and modify it, please ping me to see
55
+ if your changes can be incorporated back into this project.
56
+
57
+ That said, if your feature idea is nontrivial, you should probably open an issue to
58
+ [discuss it](http://www.igvita.com/2011/12/19/dont-push-your-pull-requests/)
59
+ before attempting a pull request.
60
+
61
+ ## License
62
+
63
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
64
+
@@ -0,0 +1,58 @@
1
+ require "forwardable"
2
+ require "hashids"
3
+
4
+ module Sequel::Plugins # :nodoc:
5
+ # This plugin allows you to easily obscure the primary key of your models with
6
+ # an encoded hashid equivalent.
7
+ #
8
+ # A few convenience methods are provided which wrap around the +Hashids+ library.
9
+ #
10
+ # = Plugin Options
11
+ #
12
+ # :salt :: The salt used to hash/unhash the primary key values. Required.
13
+ # :length :: By default, the length is variable. Setting an integer here forces
14
+ # all hashids to be a specific length.
15
+ module HashId
16
+ def self.apply(model, opts = {}) # :nodoc:
17
+ model.instance_eval do
18
+ @hash_id_state = {}
19
+ end
20
+ end
21
+
22
+ def self.configure(model, opts = {}) # :nodoc:
23
+ model.instance_eval do
24
+ @hash_id_state[:salt] = opts[:salt] || raise(ArgumentError, "hash_id plugin missing salt option")
25
+ @hash_id_state[:length] = opts[:length] || 0
26
+ end
27
+ end
28
+
29
+ module ClassMethods
30
+ Sequel::Plugins.def_dataset_methods(self, :with_hashid)
31
+
32
+ # The instance of +Hashids+ used to encode and decode values
33
+ def hasher
34
+ Hashids.new(@hash_id_state[:salt], @hash_id_state[:length])
35
+ end
36
+
37
+ # Lookup a record with a hashid, returning nil if none is found
38
+ def with_hashid(hashid)
39
+ id ,= hasher.decode(hashid)
40
+
41
+ self[id] if id
42
+ end
43
+ end
44
+
45
+ module InstanceMethods
46
+ # The hashid of the model instance
47
+ def hashid
48
+ self.class.hasher.encode(id) if id
49
+ end
50
+ end
51
+
52
+ module DatasetMethods
53
+ extend Forwardable
54
+
55
+ def_delegators :model, :with_hashid
56
+ end
57
+ end
58
+ end
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sequel-hash_id
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Adam Daniels
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-02-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: sequel
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '4.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '4.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: hashids
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.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: '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: 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: rubygems-tasks
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0.2'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0.2'
83
+ description:
84
+ email: adam@mediadrive.ca
85
+ executables: []
86
+ extensions: []
87
+ extra_rdoc_files: []
88
+ files:
89
+ - README.md
90
+ - lib/sequel/plugins/hash_id.rb
91
+ homepage: https://github.com/adam12/sequel-hash_id
92
+ licenses:
93
+ - MIT
94
+ metadata: {}
95
+ post_install_message:
96
+ rdoc_options: []
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ requirements: []
110
+ rubyforge_project:
111
+ rubygems_version: 2.6.8
112
+ signing_key:
113
+ specification_version: 4
114
+ summary: Easily obscure the primary key of your Sequel models
115
+ test_files: []