crimp 0.0.1 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d887596d2b57389ff5f4542a66b6feb0675597c9
4
- data.tar.gz: fbc0a8b3882d8100ca3e75f9a2fff070480df6e5
3
+ metadata.gz: a3c9582fb50d4c24f80f495871d85326c8db1034
4
+ data.tar.gz: 6965f4d813151e897087fd66aef13dbfa930221d
5
5
  SHA512:
6
- metadata.gz: 9e9990c056596d3c3b559690bd931db0dd9207bdece77a2f7dd0c65b1b0c30decb8709f896fac86413f582ec2b712dc5569716c937feaf807d1bf6b4becefced
7
- data.tar.gz: cd88bcba3e77022a349f27d780025b4feb5f6ad77b6fbdd16b2b8dc4e71152f367404737064bd632f3e74c9dea854a3fe8a8dabbb4ed9099ac5e2ff76225918e
6
+ metadata.gz: b32357f31dff6cb09241cd676acbeb9aa027b472090444b6364631a661c3dab052f6c21db401b5e29ed97250b5b22e13ca4645e34e295f8e35e0fadb7ffebcc0
7
+ data.tar.gz: 5b374574b25360818eef4946c7c24526be6dfe44d28d4d71f046a11e01c3a771b9f77826f1f910ce7dc4a43e3a136b4b2383dab1a904fdc801d9b680629d4027
@@ -1 +1 @@
1
- jruby-1.7.9
1
+ jruby-1.7.17
@@ -0,0 +1,9 @@
1
+ language: ruby
2
+ rvm:
3
+ - "jruby"
4
+ notifications:
5
+ email:
6
+ recipients:
7
+ - kenoir@gmail.com
8
+ on_failure: change
9
+ on_success: never
data/README.md CHANGED
@@ -1,5 +1,8 @@
1
1
  # Crimp
2
2
 
3
+ [![Build Status](https://travis-ci.org/BBC-News/crimp.png?branch=master)](https://travis-ci.org/BBC-News/crimp)
4
+ [![Gem Version](https://badge.fury.io/rb/crimp.png)](http://badge.fury.io/rb/crimp)
5
+
3
6
  Creating an md5 hash of a number, string, array, or hash in Ruby
4
7
 
5
8
  ![mighty-boosh-four-way-crimp-o](https://f.cloud.github.com/assets/180050/2148112/b44fd6fa-93de-11e3-9f9a-ad941f069b5c.gif)
@@ -28,8 +31,7 @@ require 'crimp'
28
31
 
29
32
  Crimp.stringify({:a => {:b => 'b', :c => 'c'}, :d => 'd'})
30
33
 
31
- # => [\"aSymbol=>[\\\"bSymbol=>b\\\", \\\"cSymbol=>c\\\"]Array\",
32
- \"dSymbol=>d\"]Array"
34
+ # => [\"aSymbol=>[\\\"bSymbol=>b\\\", \\\"cSymbol=>c\\\"]Array\",\"dSymbol=>d\"]Array"
33
35
 
34
36
  Crimp.signature({:a => {:b => 'b', :c => 'c'}, :d => 'd'})
35
37
 
data/Rakefile CHANGED
@@ -1 +1,6 @@
1
- require "bundler/gem_tasks"
1
+ $:.unshift File.join(File.dirname(__FILE__), 'lib')
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rake/rspec'
5
+
6
+ task :default => :spec
@@ -6,8 +6,8 @@ require 'crimp/version'
6
6
  Gem::Specification.new do |spec|
7
7
  spec.name = "crimp"
8
8
  spec.version = Crimp::VERSION
9
- spec.authors = ["Robert Kenny", "Mark McDonnell"]
10
- spec.email = ["kenoir@gmail.com","mark.mcdx@gmail.com"]
9
+ spec.authors = ["BBC News"]
10
+ spec.email = ["FutureMediaNewsRubyGems@bbc.co.uk"]
11
11
  spec.summary = %q{Creating an md5 hash of a number, string, array, or hash in Ruby}
12
12
  spec.description = <<-EOS.gsub /^\s+/, ""
13
13
  Shamelessly lifted from http://stackoverflow.com/questions/6461812/creating-an-md5-hash-of-a-number-string-array-or-hash-in-ruby
@@ -25,11 +25,11 @@ Gem::Specification.new do |spec|
25
25
  spec.add_development_dependency "bundler", "~> 1.5"
26
26
  spec.add_development_dependency "rake"
27
27
  spec.add_development_dependency "rspec"
28
+ spec.add_development_dependency "rake-rspec"
28
29
  spec.add_development_dependency "rspec-nc"
29
30
  spec.add_development_dependency "guard"
30
31
  spec.add_development_dependency "guard-rspec"
31
32
  spec.add_development_dependency "pry"
32
33
  spec.add_development_dependency "pry-remote"
33
34
  spec.add_development_dependency "pry-nav"
34
-
35
35
  end
@@ -2,29 +2,48 @@ require "crimp/version"
2
2
  require "digest"
3
3
 
4
4
  module Crimp
5
+ def self.signature(obj)
6
+ Digest::MD5.hexdigest stringify(obj)
7
+ end
8
+
5
9
  def self.stringify(obj)
6
- if obj.class == Hash
7
- arr = []
8
- obj.each do |key, value|
9
- arr << "#{self.stringify key}=>#{self.stringify value}"
10
- end
11
- obj = arr
12
- end
13
- if obj.class == Array
14
- str = ''
15
- obj.map! do |value|
16
- self.stringify value
17
- end.sort!.each do |value|
18
- str << value
19
- end
10
+ convert(obj).tap { |o| return o.class == String ? o : to_string(o) }
11
+ end
12
+
13
+ private
14
+
15
+ def self.convert(obj)
16
+ case obj
17
+ when Array
18
+ parse_array obj
19
+ when Hash
20
+ parse_hash obj
21
+ when String
22
+ obj
23
+ else
24
+ to_string obj
20
25
  end
21
- if obj.class != String
22
- obj = obj.to_s << obj.class.to_s
26
+ end
27
+
28
+ def self.hash_to_array(hash)
29
+ [].tap do |a|
30
+ hash.each { |k, v| a << pair_to_string(k, v) }
23
31
  end
24
- obj
25
32
  end
26
33
 
27
- def self.signature(obj)
28
- Digest::MD5.hexdigest( self.stringify obj )
34
+ def self.pair_to_string(k, v)
35
+ "#{stringify k}=>#{stringify v}"
36
+ end
37
+
38
+ def self.parse_array(array)
39
+ array.map! { |e| stringify(e) }.sort!
40
+ end
41
+
42
+ def self.parse_hash(hash)
43
+ stringify hash_to_array(hash)
44
+ end
45
+
46
+ def self.to_string(obj)
47
+ "#{obj.to_s}#{obj.class.to_s}"
29
48
  end
30
49
  end
@@ -1,3 +1,3 @@
1
1
  module Crimp
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1.1"
3
3
  end
@@ -0,0 +1,44 @@
1
+ require 'spec_helper'
2
+
3
+ describe Crimp do
4
+ describe '.signature' do
5
+ context 'check MD5 consistent across versions' do
6
+ context 'given a Hash' do
7
+ let(:hash) do
8
+ {
9
+ :a => 123,
10
+ :b => 1.23,
11
+ :c => 'string',
12
+ :d => :sym
13
+ }
14
+ end
15
+ let(:md5) { '1dd744d51279187cc08cabe240f98be2' }
16
+
17
+ specify { expect(subject.signature hash).to eq md5 }
18
+ end
19
+
20
+ context 'given legacy Hash' do
21
+ let(:hash) do
22
+ {:a => {:b => 'b', :c => 'c'}, :d => 'd'}
23
+ end
24
+ let(:md5) { '68d07febc4f47f56fa6ef5de063a77b1' }
25
+
26
+ specify { expect(subject.signature hash).to eq md5 }
27
+ end
28
+
29
+ context 'given an Array' do
30
+ let(:array) { [123, 1.23, 'string', :sym] }
31
+ let(:md5) { 'cd29980f258eef3faceca4f4da02ec65' }
32
+
33
+ specify { expect(subject.signature array).to eq md5 }
34
+ end
35
+
36
+ context 'given legacy Array' do
37
+ let(:array) { [1, 2, 3, [4, [5, 6]]] }
38
+ let(:md5) { '4dc4e1161c9315db0bc43c0201b3ec05' }
39
+
40
+ specify { expect(subject.signature array).to eq md5 }
41
+ end
42
+ end
43
+ end
44
+ end
@@ -1,70 +1,60 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Crimp do
4
- let (:example_hash) { {:a => {:b => 'b', :c => 'c'}, :d => 'd'} }
5
- let (:example_hash_unordered) { {:d => 'd', :a => {:c => 'c', :b => 'b'}} }
6
- let (:example_array) { [1,2,3,[4,[5,6]]] }
7
- let (:example_array_unordered) { [3,2,1,[[5,6],4]] }
4
+ let (:hash) { {:a => {:b => 'b', :c => 'c'}, :d => 'd'} }
5
+ let (:hash_unordered) { {:d => 'd', :a => {:c => 'c', :b => 'b'}} }
6
+ let (:array) { [1, 2, 3, [4, [5, 6]]] }
7
+ let (:array_unordered) { [3, 2, 1, [[5, 6], 4]] }
8
8
 
9
- describe ".signature(obj)" do
10
- context "obj.class == Hash" do
11
- it "returns a string" do
12
- expect(subject.signature(example_hash)).to be_a String
13
- end
9
+ describe ".signature" do
10
+ context "given a Hash" do
11
+ specify { expect(subject.signature hash).to be_a String }
14
12
 
15
- it "returns Digest::MD5.hexdigest of self.stringify(obj)" do
13
+ it "returns MD5 hash of stringified Hash" do
16
14
  expect(
17
- subject.signature(example_hash)
15
+ subject.signature(hash)
18
16
  ).to eq(
19
- Digest::MD5.hexdigest(subject.stringify(example_hash))
17
+ Digest::MD5.hexdigest(subject.stringify(hash))
20
18
  )
21
19
  end
22
20
  end
23
21
 
24
- context "obj.class == Array" do
25
- it "returns a string" do
26
- expect(subject.signature(example_array)).to be_a String
27
- end
22
+ context "given an Array" do
23
+ specify { expect(subject.signature array).to be_a String }
28
24
 
29
- it "returns Digest::MD5.hexdigest of self.stringify(obj)" do
25
+ it "returns MD5 hash of stringified Array" do
30
26
  expect(
31
- subject.signature(example_array)
27
+ subject.signature(array)
32
28
  ).to eq(
33
- Digest::MD5.hexdigest(subject.stringify(example_array))
29
+ Digest::MD5.hexdigest(subject.stringify(array))
34
30
  )
35
31
  end
36
32
  end
37
-
38
33
  end
39
34
 
40
- describe ".stringify(obj)" do
41
- context "obj.class == Hash" do
42
- it "returns a string" do
43
- expect(subject.stringify(example_hash)).to be_a String
44
- end
35
+ describe ".stringify" do
36
+ context "given a Hash" do
37
+ specify { expect(subject.stringify hash).to be_a String }
45
38
 
46
- it "will return equal strings for differently ordered hashes" do
39
+ it "returns equal strings for differently ordered hashes" do
47
40
  expect(
48
- subject.stringify(example_hash)
41
+ subject.stringify(hash)
49
42
  ).to eq(
50
- subject.stringify(example_hash_unordered)
43
+ subject.stringify(hash_unordered)
51
44
  )
52
45
  end
53
46
  end
54
47
 
55
- context "obj.class == Array" do
56
- it "returns a string" do
57
- expect(subject.stringify(example_array)).to be_a String
58
- end
48
+ context "given an Array" do
49
+ specify { expect(subject.stringify array).to be_a String }
59
50
 
60
- it "will return equal strings for differently ordered arrays" do
51
+ it "returns equal strings for differently ordered arrays" do
61
52
  expect(
62
- subject.stringify(example_array)
53
+ subject.stringify(array)
63
54
  ).to eq(
64
- subject.stringify(example_array_unordered)
55
+ subject.stringify(array_unordered)
65
56
  )
66
57
  end
67
-
68
58
  end
69
59
  end
70
60
  end
metadata CHANGED
@@ -1,154 +1,167 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: crimp
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
- - Robert Kenny
8
- - Mark McDonnell
7
+ - BBC News
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2014-02-12 00:00:00.000000000 Z
11
+ date: 2015-06-02 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - ~>
17
+ - !ruby/object:Gem::Version
18
+ version: '1.5'
15
19
  name: bundler
20
+ prerelease: false
21
+ type: :development
16
22
  version_requirements: !ruby/object:Gem::Requirement
17
23
  requirements:
18
24
  - - ~>
19
25
  - !ruby/object:Gem::Version
20
26
  version: '1.5'
27
+ - !ruby/object:Gem::Dependency
21
28
  requirement: !ruby/object:Gem::Requirement
22
29
  requirements:
23
- - - ~>
30
+ - - '>='
24
31
  - !ruby/object:Gem::Version
25
- version: '1.5'
32
+ version: '0'
33
+ name: rake
26
34
  prerelease: false
27
35
  type: :development
28
- - !ruby/object:Gem::Dependency
29
- name: rake
30
36
  version_requirements: !ruby/object:Gem::Requirement
31
37
  requirements:
32
38
  - - '>='
33
39
  - !ruby/object:Gem::Version
34
40
  version: '0'
41
+ - !ruby/object:Gem::Dependency
35
42
  requirement: !ruby/object:Gem::Requirement
36
43
  requirements:
37
44
  - - '>='
38
45
  - !ruby/object:Gem::Version
39
46
  version: '0'
47
+ name: rspec
40
48
  prerelease: false
41
49
  type: :development
42
- - !ruby/object:Gem::Dependency
43
- name: rspec
44
50
  version_requirements: !ruby/object:Gem::Requirement
45
51
  requirements:
46
52
  - - '>='
47
53
  - !ruby/object:Gem::Version
48
54
  version: '0'
55
+ - !ruby/object:Gem::Dependency
49
56
  requirement: !ruby/object:Gem::Requirement
50
57
  requirements:
51
58
  - - '>='
52
59
  - !ruby/object:Gem::Version
53
60
  version: '0'
61
+ name: rake-rspec
54
62
  prerelease: false
55
63
  type: :development
56
- - !ruby/object:Gem::Dependency
57
- name: rspec-nc
58
64
  version_requirements: !ruby/object:Gem::Requirement
59
65
  requirements:
60
66
  - - '>='
61
67
  - !ruby/object:Gem::Version
62
68
  version: '0'
69
+ - !ruby/object:Gem::Dependency
63
70
  requirement: !ruby/object:Gem::Requirement
64
71
  requirements:
65
72
  - - '>='
66
73
  - !ruby/object:Gem::Version
67
74
  version: '0'
75
+ name: rspec-nc
68
76
  prerelease: false
69
77
  type: :development
70
- - !ruby/object:Gem::Dependency
71
- name: guard
72
78
  version_requirements: !ruby/object:Gem::Requirement
73
79
  requirements:
74
80
  - - '>='
75
81
  - !ruby/object:Gem::Version
76
82
  version: '0'
83
+ - !ruby/object:Gem::Dependency
77
84
  requirement: !ruby/object:Gem::Requirement
78
85
  requirements:
79
86
  - - '>='
80
87
  - !ruby/object:Gem::Version
81
88
  version: '0'
89
+ name: guard
82
90
  prerelease: false
83
91
  type: :development
84
- - !ruby/object:Gem::Dependency
85
- name: guard-rspec
86
92
  version_requirements: !ruby/object:Gem::Requirement
87
93
  requirements:
88
94
  - - '>='
89
95
  - !ruby/object:Gem::Version
90
96
  version: '0'
97
+ - !ruby/object:Gem::Dependency
91
98
  requirement: !ruby/object:Gem::Requirement
92
99
  requirements:
93
100
  - - '>='
94
101
  - !ruby/object:Gem::Version
95
102
  version: '0'
103
+ name: guard-rspec
96
104
  prerelease: false
97
105
  type: :development
98
- - !ruby/object:Gem::Dependency
99
- name: pry
100
106
  version_requirements: !ruby/object:Gem::Requirement
101
107
  requirements:
102
108
  - - '>='
103
109
  - !ruby/object:Gem::Version
104
110
  version: '0'
111
+ - !ruby/object:Gem::Dependency
105
112
  requirement: !ruby/object:Gem::Requirement
106
113
  requirements:
107
114
  - - '>='
108
115
  - !ruby/object:Gem::Version
109
116
  version: '0'
117
+ name: pry
110
118
  prerelease: false
111
119
  type: :development
112
- - !ruby/object:Gem::Dependency
113
- name: pry-remote
114
120
  version_requirements: !ruby/object:Gem::Requirement
115
121
  requirements:
116
122
  - - '>='
117
123
  - !ruby/object:Gem::Version
118
124
  version: '0'
125
+ - !ruby/object:Gem::Dependency
119
126
  requirement: !ruby/object:Gem::Requirement
120
127
  requirements:
121
128
  - - '>='
122
129
  - !ruby/object:Gem::Version
123
130
  version: '0'
131
+ name: pry-remote
124
132
  prerelease: false
125
133
  type: :development
126
- - !ruby/object:Gem::Dependency
127
- name: pry-nav
128
134
  version_requirements: !ruby/object:Gem::Requirement
129
135
  requirements:
130
136
  - - '>='
131
137
  - !ruby/object:Gem::Version
132
138
  version: '0'
139
+ - !ruby/object:Gem::Dependency
133
140
  requirement: !ruby/object:Gem::Requirement
134
141
  requirements:
135
142
  - - '>='
136
143
  - !ruby/object:Gem::Version
137
144
  version: '0'
145
+ name: pry-nav
138
146
  prerelease: false
139
147
  type: :development
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - '>='
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
140
153
  description: |
141
154
  Shamelessly lifted from http://stackoverflow.com/questions/6461812/creating-an-md5-hash-of-a-number-string-array-or-hash-in-ruby
142
155
  All credit should go to http://stackoverflow.com/users/394282/luke
143
156
  email:
144
- - kenoir@gmail.com
145
- - mark.mcdx@gmail.com
157
+ - FutureMediaNewsRubyGems@bbc.co.uk
146
158
  executables: []
147
159
  extensions: []
148
160
  extra_rdoc_files: []
149
161
  files:
150
162
  - .gitignore
151
163
  - .ruby-version
164
+ - .travis.yml
152
165
  - Gemfile
153
166
  - Guardfile
154
167
  - LICENSE.txt
@@ -157,6 +170,7 @@ files:
157
170
  - crimp.gemspec
158
171
  - lib/crimp.rb
159
172
  - lib/crimp/version.rb
173
+ - spec/consistency_spec.rb
160
174
  - spec/crimp_spec.rb
161
175
  - spec/spec_helper.rb
162
176
  homepage: ''
@@ -184,5 +198,6 @@ signing_key:
184
198
  specification_version: 4
185
199
  summary: Creating an md5 hash of a number, string, array, or hash in Ruby
186
200
  test_files:
201
+ - spec/consistency_spec.rb
187
202
  - spec/crimp_spec.rb
188
203
  - spec/spec_helper.rb