digester 1.0.0
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.
- data/.gitignore +18 -0
- data/.rspec +2 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +48 -0
- data/Rakefile +1 -0
- data/digester.gemspec +27 -0
- data/lib/digester.rb +6 -0
- data/lib/digester/digester.rb +85 -0
- data/lib/digester/version.rb +3 -0
- data/spec/lib/digester_spec.rb +59 -0
- metadata +128 -0
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Simeon Simeonov
|
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,48 @@
|
|
1
|
+
# Digester
|
2
|
+
|
3
|
+
Use `Digester` anytime you want to use a complex data structure as a primary key
|
4
|
+
in a database or a lookup key in a `Hash` or a key-value store.
|
5
|
+
|
6
|
+
`Digester` creates an [MD5 digest](http://en.wikipedia.org/wiki/MD5) for Ruby
|
7
|
+
data structures in a consistent algorithmic manner such that two identical
|
8
|
+
data structures generate the same digests regardless of how they were
|
9
|
+
created.
|
10
|
+
|
11
|
+
This way it is also possible to compare data structures and digests
|
12
|
+
across different programming languages. At [Swoop](http://swoop.com),
|
13
|
+
we use consistent data structure digests to implement variations of
|
14
|
+
[content addressable storage](http://en.wikipedia.org/wiki/Content-addressable_storage)
|
15
|
+
across S3, Redis and MongoDB with Ruby, Python and Java clients.
|
16
|
+
|
17
|
+
## Installation
|
18
|
+
|
19
|
+
Add this line to your application's Gemfile:
|
20
|
+
|
21
|
+
gem 'digester'
|
22
|
+
|
23
|
+
And then execute:
|
24
|
+
|
25
|
+
$ bundle
|
26
|
+
|
27
|
+
Or install it yourself as:
|
28
|
+
|
29
|
+
$ gem install digester
|
30
|
+
|
31
|
+
## Usage
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
require 'digester'
|
35
|
+
data = {x: {y: { z: [1, 2, 3]}}}
|
36
|
+
Digester::Digester.new.digest(data)
|
37
|
+
=> "d5221448a0765677cbce09eebd557c61"
|
38
|
+
Digester::Digester.digest(data, {upcase: true})
|
39
|
+
=> "D5221448A0765677CBCE09EEBD557C61"
|
40
|
+
```
|
41
|
+
|
42
|
+
## Contributing
|
43
|
+
|
44
|
+
1. Fork it
|
45
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
46
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
47
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
48
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/digester.gemspec
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'digester/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "digester"
|
8
|
+
spec.version = Digester::VERSION
|
9
|
+
spec.authors = ["Simeon Simeonov"]
|
10
|
+
spec.email = ["sim@swoop_dot_com"]
|
11
|
+
spec.summary = %q{Digester builds consistent MD5 signatures for arbitrary Ruby data structures.}
|
12
|
+
spec.homepage = "https://github.com/swoop-inc/digester"
|
13
|
+
spec.license = "MIT"
|
14
|
+
|
15
|
+
spec.rubyforge_project = "digester"
|
16
|
+
|
17
|
+
spec.files = `git ls-files`.split($/)
|
18
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
19
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_dependency "activesupport"
|
23
|
+
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
25
|
+
spec.add_development_dependency "rake"
|
26
|
+
spec.add_development_dependency "rspec", "~> 2.13"
|
27
|
+
end
|
data/lib/digester.rb
ADDED
@@ -0,0 +1,85 @@
|
|
1
|
+
require 'active_support/core_ext'
|
2
|
+
require 'digest/md5'
|
3
|
+
|
4
|
+
module Digester
|
5
|
+
class Digester
|
6
|
+
|
7
|
+
DEFAULT_NAMESPACE = ''
|
8
|
+
DEFAULT_SEPARATOR = '::'
|
9
|
+
|
10
|
+
attr_accessor :options
|
11
|
+
|
12
|
+
# Creates a digester using a set of options.
|
13
|
+
#
|
14
|
+
# @option opts [String] :namespace ('') The namespace for the digests
|
15
|
+
# @option opts [String] :separator ('::') Internal separator between data structure elements
|
16
|
+
# @option opts [String] :sort (false) Should data structure
|
17
|
+
# elements be sorted before a digest is computed. Note that this could make two
|
18
|
+
# different data structures produce the same digest but it is useful in some cases.
|
19
|
+
# @option opts [String] :uniquify (false) Should data structure
|
20
|
+
# elements be uniquified before a digest is computed. Note that this could make two
|
21
|
+
# different data structures produce the same digest but it is useful in some cases.
|
22
|
+
# @option opts [String] :upcase (false) Should the MD5 digest be upcased?
|
23
|
+
def initialize(opts = {})
|
24
|
+
@options = self.class.options_from opts
|
25
|
+
end
|
26
|
+
|
27
|
+
# Creates an MD5 digest of its arguments.
|
28
|
+
# The data structures cannot have any circular references.
|
29
|
+
#
|
30
|
+
# @return [String] MD5 digest
|
31
|
+
def digest(*args)
|
32
|
+
self.class.digest(args, options)
|
33
|
+
end
|
34
|
+
|
35
|
+
# Creates an MD5 digest for a tree data structure.
|
36
|
+
# The data structure cannot have any circular references.
|
37
|
+
#
|
38
|
+
# @param args [Object] the data to digest
|
39
|
+
# @option opts [String] :namespace ('') The namespace for the digests.
|
40
|
+
# This allows different digests for the same data structures used in different contexts.
|
41
|
+
# @option opts [String] :separator ('::') Internal separator between data structure elements.
|
42
|
+
# This is an aid in ensuring different data structures generate different digests.
|
43
|
+
# @option opts [String] :sort (false) Should data structure
|
44
|
+
# elements be sorted before a digest is computed. Note that this could make two
|
45
|
+
# different data structures produce the same digest but it is useful in some cases.
|
46
|
+
# @option opts [String] :uniquify (false) Should data structure
|
47
|
+
# elements be uniquified before a digest is computed. Note that this could make two
|
48
|
+
# different data structures produce the same digest but it is useful in some cases.
|
49
|
+
# @option opts [String] :upcase (false) Should the MD5 digest be upcased?
|
50
|
+
# @return [String] MD5 digest
|
51
|
+
def self.digest(args, opts = {})
|
52
|
+
opts = options_from opts
|
53
|
+
args = Array.wrap(args).flatten.map(&:to_s)
|
54
|
+
args.uniq! if opts[:uniquify]
|
55
|
+
args.sort! if opts[:sort]
|
56
|
+
content = args.join(opts[:separator])
|
57
|
+
digest = ::Digest::MD5.hexdigest(content)
|
58
|
+
digest.upcase! if opts[:upcase]
|
59
|
+
if opts[:namespace].blank?
|
60
|
+
digest
|
61
|
+
else
|
62
|
+
namespace = opts[:namespace]
|
63
|
+
namespace.upcase! if opts[:upcase]
|
64
|
+
"#{namespace}:#{digest}"
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
protected
|
69
|
+
|
70
|
+
def self.options_from(opts)
|
71
|
+
opts.reverse_merge default_opts
|
72
|
+
end
|
73
|
+
|
74
|
+
def self.default_opts
|
75
|
+
{
|
76
|
+
namespace: DEFAULT_NAMESPACE,
|
77
|
+
separator: DEFAULT_SEPARATOR,
|
78
|
+
sort: false,
|
79
|
+
uniquify: false,
|
80
|
+
upcase: false
|
81
|
+
}
|
82
|
+
end
|
83
|
+
|
84
|
+
end
|
85
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
require 'digester/digester'
|
2
|
+
|
3
|
+
describe Digester::Digester do
|
4
|
+
klass = Digester::Digester
|
5
|
+
|
6
|
+
[
|
7
|
+
{},
|
8
|
+
{
|
9
|
+
namespace: 'md5',
|
10
|
+
},
|
11
|
+
{
|
12
|
+
namespace: 'CPO',
|
13
|
+
separator: '.',
|
14
|
+
sort: true,
|
15
|
+
uniquify: true,
|
16
|
+
upcase: false
|
17
|
+
},
|
18
|
+
].each_with_index do |options, i|
|
19
|
+
|
20
|
+
context "run No.#{i+1} with options #{options}" do
|
21
|
+
|
22
|
+
[
|
23
|
+
[
|
24
|
+
%w(first second third),
|
25
|
+
'first::second::third',
|
26
|
+
[true, true, false]
|
27
|
+
],
|
28
|
+
[
|
29
|
+
[1, 2, "2", {}, {x: 5}, [1, 2, 3]],
|
30
|
+
'1.2.3.{:x=>5}.{}',
|
31
|
+
[false, false, true]
|
32
|
+
]
|
33
|
+
].each do |(input, processed, equals)|
|
34
|
+
context "with input #{input}" do
|
35
|
+
before(:each) do
|
36
|
+
digest = ::Digest::MD5.hexdigest(processed)
|
37
|
+
if options[:namespace]
|
38
|
+
@output_guess = "#{options[:namespace]}:#{digest}"
|
39
|
+
else
|
40
|
+
@output_guess = digest
|
41
|
+
end
|
42
|
+
@output_guess.upcase! if options[:upcase]
|
43
|
+
end
|
44
|
+
|
45
|
+
subject { klass.digest(input, options) }
|
46
|
+
|
47
|
+
it { should be_a String }
|
48
|
+
|
49
|
+
describe "should #{equals[i] ? "" : "not "}match the guessed digest for #{processed}" do
|
50
|
+
specify { equals[i] ?
|
51
|
+
subject.should == @output_guess :
|
52
|
+
subject.should_not == @output_guess }
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
metadata
ADDED
@@ -0,0 +1,128 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: digester
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Simeon Simeonov
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-05-18 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activesupport
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: bundler
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '1.3'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '1.3'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rake
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: rspec
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ~>
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '2.13'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ~>
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '2.13'
|
78
|
+
description:
|
79
|
+
email:
|
80
|
+
- sim@swoop_dot_com
|
81
|
+
executables: []
|
82
|
+
extensions: []
|
83
|
+
extra_rdoc_files: []
|
84
|
+
files:
|
85
|
+
- .gitignore
|
86
|
+
- .rspec
|
87
|
+
- Gemfile
|
88
|
+
- LICENSE.txt
|
89
|
+
- README.md
|
90
|
+
- Rakefile
|
91
|
+
- digester.gemspec
|
92
|
+
- lib/digester.rb
|
93
|
+
- lib/digester/digester.rb
|
94
|
+
- lib/digester/version.rb
|
95
|
+
- spec/lib/digester_spec.rb
|
96
|
+
homepage: https://github.com/swoop-inc/digester
|
97
|
+
licenses:
|
98
|
+
- MIT
|
99
|
+
post_install_message:
|
100
|
+
rdoc_options: []
|
101
|
+
require_paths:
|
102
|
+
- lib
|
103
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
104
|
+
none: false
|
105
|
+
requirements:
|
106
|
+
- - ! '>='
|
107
|
+
- !ruby/object:Gem::Version
|
108
|
+
version: '0'
|
109
|
+
segments:
|
110
|
+
- 0
|
111
|
+
hash: 834053746874837549
|
112
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
113
|
+
none: false
|
114
|
+
requirements:
|
115
|
+
- - ! '>='
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
segments:
|
119
|
+
- 0
|
120
|
+
hash: 834053746874837549
|
121
|
+
requirements: []
|
122
|
+
rubyforge_project: digester
|
123
|
+
rubygems_version: 1.8.25
|
124
|
+
signing_key:
|
125
|
+
specification_version: 3
|
126
|
+
summary: Digester builds consistent MD5 signatures for arbitrary Ruby data structures.
|
127
|
+
test_files:
|
128
|
+
- spec/lib/digester_spec.rb
|