json_hasher 0.1.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9c43b96281db87452b4319a83b038ce3b7bbe4b9
4
+ data.tar.gz: 8f61ad2a9071b86b142565ccc9fce047d11e2787
5
+ SHA512:
6
+ metadata.gz: 1127a0933a2eb23c0d11b0ef162ad038c6260c23ecae84eaf3b9d0e5b60e5ae6ff948155e4f070bf00d2c565db27fa40060a8fd52f1a4041f4a712558e8b5a8a
7
+ data.tar.gz: ad865d556091c64b944fcc852a929650dee0b0e95d0f1f917260efc89902d563492a0aa606472a88b5edb5c73c44e2d2fdb3d9dd6c53ebedea90285e6bbce5bc
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.12.5
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in json_hasher.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Nick Poorman
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.
@@ -0,0 +1,51 @@
1
+ # JSONHasher
2
+
3
+ Create a stable SHA-2 hash from a JSON object.
4
+
5
+ JSON objects do not require the keys to be in any kind of order which can make creating a hash unstable. This gem solves that problem.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'json_hasher'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install json_hasher
22
+
23
+ ## Usage
24
+
25
+ ```ruby
26
+ test_json_one = '[{ "foo": "bar", "ping": "pong" }]'
27
+ test_json_two = '[{ "ping": "pong", "foo": "bar" }]'
28
+
29
+ hashed_one = JSONHasher::Parser.new(test_json_one).to_sha2
30
+ hashed_two = JSONHasher::Parser.new(test_json_two).to_sha2
31
+
32
+ # hashed_one: c15915ccb473c4cecca65f940f6a36091f70333b9e8226dfe51ca247c73e56f1
33
+ # hashed_two: c15915ccb473c4cecca65f940f6a36091f70333b9e8226dfe51ca247c73e56f1
34
+
35
+ assert_equal hashed_one, hashed_two
36
+ ```
37
+
38
+ ## Development
39
+
40
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
41
+
42
+ 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`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
43
+
44
+ ## Contributing
45
+
46
+ Bug reports and pull requests are welcome on GitHub at https://github.com/nickpoorman/ruby_json_hasher.
47
+
48
+
49
+ ## License
50
+
51
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "json_hasher"
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
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -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 'json_hasher/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "json_hasher"
8
+ spec.version = JSONHasher::VERSION
9
+ spec.authors = ["Nick Poorman"]
10
+ spec.email = ["nickpoorman@gmail.com"]
11
+
12
+ spec.summary = "Create a stable SHA-2 hash from a JSON object."
13
+ spec.description = "JSON objects do not require the keys to be in any kind of order which can make the hash unstable. This gem solves that problem."
14
+ spec.homepage = "https://github.com/nickpoorman/ruby_json_hasher"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.12"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "minitest", "~> 5.0"
25
+ end
@@ -0,0 +1,48 @@
1
+ require 'json_hasher/version'
2
+ require 'json'
3
+ require 'digest/sha2'
4
+
5
+ module JSONHasher
6
+ class Parser
7
+ def initialize(json_string)
8
+ raise 'JSONHasher only takes a String' unless json_string.is_a?(String)
9
+ @parsed_json = JSON.parse(json_string)
10
+ end
11
+
12
+ def to_sha2
13
+ hash_parsed_json(@parsed_json)
14
+ end
15
+
16
+ private
17
+
18
+ def hash_parsed_json(parsed_json)
19
+ case parsed_json.class.name
20
+ when 'Array'
21
+ hash_array(parsed_json)
22
+ when 'Hash'
23
+ hash_object(parsed_json)
24
+ else
25
+ hash_other(parsed_json)
26
+ end
27
+ end
28
+
29
+ def hash_array(array)
30
+ mapped = array.map do |elm|
31
+ hash_parsed_json(elm)
32
+ end
33
+ hash_other(mapped)
34
+ end
35
+
36
+ def hash_object(obj)
37
+ pairs = obj.to_a.sort_by(&:first)
38
+ hash_array(pairs)
39
+ end
40
+
41
+ def hash_other(value)
42
+ value_type = value.class.to_s
43
+ value_string = value.to_s
44
+ hashed = Digest::SHA2.new << "#{value_type}\"\"#{value_string}"
45
+ hashed.to_s
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,3 @@
1
+ module JSONHasher
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: json_hasher
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Nick Poorman
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-07-19 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: '1.12'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.12'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ description: JSON objects do not require the keys to be in any kind of order which
56
+ can make the hash unstable. This gem solves that problem.
57
+ email:
58
+ - nickpoorman@gmail.com
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - ".travis.yml"
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - bin/console
70
+ - bin/setup
71
+ - json_hasher.gemspec
72
+ - lib/json_hasher.rb
73
+ - lib/json_hasher/version.rb
74
+ homepage: https://github.com/nickpoorman/ruby_json_hasher
75
+ licenses:
76
+ - MIT
77
+ metadata: {}
78
+ post_install_message:
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ requirements: []
93
+ rubyforge_project:
94
+ rubygems_version: 2.5.1
95
+ signing_key:
96
+ specification_version: 4
97
+ summary: Create a stable SHA-2 hash from a JSON object.
98
+ test_files: []