jsondiff 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'rspec'
data/Gemfile.lock ADDED
@@ -0,0 +1,18 @@
1
+ GEM
2
+ remote: https://rubygems.org/
3
+ specs:
4
+ diff-lcs (1.2.2)
5
+ rspec (2.13.0)
6
+ rspec-core (~> 2.13.0)
7
+ rspec-expectations (~> 2.13.0)
8
+ rspec-mocks (~> 2.13.0)
9
+ rspec-core (2.13.1)
10
+ rspec-expectations (2.13.0)
11
+ diff-lcs (>= 1.1.3, < 2.0)
12
+ rspec-mocks (2.13.1)
13
+
14
+ PLATFORMS
15
+ ruby
16
+
17
+ DEPENDENCIES
18
+ rspec
data/README.md ADDED
@@ -0,0 +1,40 @@
1
+ # JSON Diff - JSON Patch Generator
2
+
3
+ A JSON patch generator in Ruby that is compliant to the JSON Patch specification [RFC6902](http://www.rfc-editor.org/rfc/rfc6902.txt).
4
+
5
+ To apply a patch, you should use [hana](https://github.com/tenderlove/hana).
6
+
7
+ ## Status
8
+
9
+ This is proof of concept only. Only example of the specification works.
10
+
11
+ As for now, it generate only patch with *add*, *remove* and *replace* operations.
12
+
13
+ ## Install
14
+
15
+ No gems yet
16
+
17
+ ## Usage
18
+
19
+ ```ruby
20
+ JsonDiff.generate({foo: :bar}, {foo: :plop})
21
+ ```
22
+ => will result in
23
+
24
+ ```ruby
25
+ [{:op=>:replace, :path=>"/foo", :value=>:plop}]
26
+ ```
27
+
28
+ ## Tests
29
+
30
+ rspec
31
+
32
+ ## License
33
+
34
+ Copyright (c) 2013 François de Metz
35
+
36
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
37
+
38
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
39
+
40
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/jsondiff.gemspec ADDED
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/json_diff/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["François de Metz"]
6
+ gem.email = ["francois@2metz.fr"]
7
+ gem.description = %q{Generate a JSON Patch from 2 ruby hash}
8
+ gem.summary = %q{}
9
+ gem.homepage = "https://github.com/francois2metz/jsondiff"
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+
15
+ gem.name = "jsondiff"
16
+ gem.require_paths = ["lib"]
17
+ gem.version = JsonDiff::VERSION
18
+
19
+ gem.add_development_dependency "rspec", "~> 2.13.0"
20
+ end
data/lib/json_diff.rb ADDED
@@ -0,0 +1,15 @@
1
+ require 'json_diff/helpers'
2
+ require 'json_diff/hash_diff'
3
+ require 'json_diff/array_diff'
4
+
5
+ module JsonDiff
6
+ # Generate a patch from two ruby hash
7
+ #
8
+ # hash1 - the first hash
9
+ # hash2 - the second hash
10
+ #
11
+ # Returns an array of operations
12
+ def self.generate(hash1, hash2)
13
+ HashDiff.generate([], "", hash1, hash2)
14
+ end
15
+ end
@@ -0,0 +1,34 @@
1
+ module JsonDiff
2
+ class ArrayDiff
3
+ include Helpers
4
+
5
+ def self.generate(result, prefix, array1, array2)
6
+ if array1.size < array2.size
7
+ array2.each_with_index do |value, index|
8
+ if array1.at(index) != value
9
+ result << add_op(prefix, index, value)
10
+ array1.insert(index, value)
11
+ end
12
+ end
13
+ elsif array1.size > array2.size
14
+ array1.each_with_index do |value, index|
15
+ if array2.at(index) != value
16
+ result << remove_op(prefix, index)
17
+ array1.delete_at(index)
18
+ end
19
+ end
20
+ end
21
+ array2.each_with_index do |value, index|
22
+ if array1.at(index) != value
23
+ if value.kind_of?(Hash) && array1.at(index).kind_of?(Hash)
24
+ HashDiff.generate(result, "#{prefix}/#{index}", array1.at(index), value)
25
+ elsif value.kind_of?(Array) && array1.at(index).kind_of?(Array)
26
+ ArrayDiff.generate(result, "#{prefix}/#{index}", array1.at(index), value)
27
+ else
28
+ result << replace_op(prefix, index, value)
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,30 @@
1
+ module JsonDiff
2
+ class HashDiff
3
+ include Helpers
4
+
5
+ def self.generate(result, prefix, hash1, hash2)
6
+ hash2.each do |key, value|
7
+ if !hash1.has_key? key
8
+ result << add_op(prefix, key, value)
9
+ else
10
+ value2 = hash1.fetch(key)
11
+ if value != value2
12
+ if value.kind_of?(Array) && value2.kind_of?(Array)
13
+ ArrayDiff.generate(result, "#{prefix}/#{key}", value2, value)
14
+ elsif value.kind_of?(Hash) && value2.kind_of?(Hash)
15
+ HashDiff.generate(result, "#{prefix}/#{key}", value2, value)
16
+ else
17
+ result << replace_op(prefix, key, value)
18
+ end
19
+ end
20
+ end
21
+ end
22
+ hash1.each do |key, value|
23
+ unless hash2.has_key? key
24
+ result << remove_op(prefix, key)
25
+ end
26
+ end
27
+ result
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,21 @@
1
+ module JsonDiff
2
+ module Helpers
3
+ def self.included(base)
4
+ base.extend(ClassMethods)
5
+ end
6
+
7
+ module ClassMethods
8
+ def add_op(prefix, key, value)
9
+ {op: :add, path: "#{prefix}/#{key}", value: value}
10
+ end
11
+
12
+ def remove_op(prefix, key)
13
+ {op: :remove, path: "#{prefix}/#{key}"}
14
+ end
15
+
16
+ def replace_op(prefix, key, value)
17
+ {op: :replace, path: "#{prefix}/#{key}", value: value}
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,3 @@
1
+ module JsonDiff
2
+ VERSION = '0.0.1'
3
+ end
data/spec/diff_spec.rb ADDED
@@ -0,0 +1,87 @@
1
+ require_relative 'spec_helper'
2
+
3
+ describe JsonDiff do
4
+ describe "#generate" do
5
+ it "generate empty patch if nothing has changed" do
6
+ subject.generate({}, {}).should == []
7
+ end
8
+
9
+ context "add op" do
10
+ it "on hash member" do
11
+ subject.generate({foo: :bar},
12
+ {foo: :bar, baz: :qux})
13
+ .should == [{ op: :add, path: "/baz", value: :qux }]
14
+ end
15
+
16
+ it "on array element" do
17
+ subject.generate({foo: [:bar, :baz]},
18
+ {foo: [:bar, :qux, :baz]})
19
+ .should == [{ op: :add, path: "/foo/1", value: :qux }]
20
+ end
21
+
22
+ it "on nested member object" do
23
+ subject.generate({foo: :bar},
24
+ {foo: :bar, child: {grandchild: {}}})
25
+ .should == [{ op: :add, path: "/child", value: {grandchild: {}} }]
26
+ end
27
+
28
+ it "on more nested member object" do
29
+ subject.generate({child: {grandchild: {foo: :bar}}},
30
+ {child: {grandchild: {foo: :bar, chuck: :norris}}})
31
+ .should == [{ op: :add, path: "/child/grandchild/chuck", value: :norris }]
32
+ end
33
+
34
+ it "on nested object inside arrays" do
35
+ subject.generate({child: [{foo: :bar}]},
36
+ {child: [{foo: :bar, chuck: :norris}]})
37
+ .should == [{ op: :add, path: '/child/0/chuck', value: :norris}]
38
+ end
39
+
40
+ it "on nested array inside arrays" do
41
+ subject.generate({child: [[:foo, :bar]]},
42
+ {child: [[:foo, :bar, :chuck]]})
43
+ .should == [{ op: :add, path: '/child/0/2', value: :chuck}]
44
+ end
45
+ end
46
+
47
+ context "remove op" do
48
+ it "on hash member" do
49
+ subject.generate({foo: :bar, baz: :qux},
50
+ {foo: :bar})
51
+ .should == [{ op: :remove, path: "/baz"}]
52
+ end
53
+
54
+ it "on array element" do
55
+ subject.generate({foo: [:bar, :qux, :baz]},
56
+ {foo: [:bar, :baz]})
57
+ .should == [{ op: :remove, path: "/foo/1" }]
58
+ end
59
+ end
60
+
61
+ context "replace op" do
62
+ it "on hash member" do
63
+ subject.generate({foo: :bar, baz: :qux},
64
+ {foo: :bar, baz: :boo})
65
+ .should == [{ op: :replace, path: "/baz", value: :boo}]
66
+ end
67
+
68
+ it "on array element" do
69
+ subject.generate({foo: [:bar, :qux, :baz]},
70
+ {foo: [:bar, :foo, :baz]})
71
+ .should == [{ op: :replace, path: "/foo/1", value: :foo }]
72
+ end
73
+
74
+ it "when type differ from array to hash" do
75
+ subject.generate({foo: [:bar]},
76
+ {foo: {bar: :foo}})
77
+ .should == [{ op: :replace, path: "/foo", value: {bar: :foo} }]
78
+ end
79
+
80
+ it "when type differ from hash to array" do
81
+ subject.generate({foo: {bar: :foo}},
82
+ {foo: [:bar]})
83
+ .should == [{ op: :replace, path: "/foo", value: [:bar] }]
84
+ end
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,6 @@
1
+ require_relative '../lib/json_diff'
2
+
3
+ RSpec.configure do |c|
4
+ c.filter_run focus: true
5
+ c.run_all_when_everything_filtered = true
6
+ end
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jsondiff
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - François de Metz
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-04-15 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: &20852580 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 2.13.0
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *20852580
25
+ description: Generate a JSON Patch from 2 ruby hash
26
+ email:
27
+ - francois@2metz.fr
28
+ executables: []
29
+ extensions: []
30
+ extra_rdoc_files: []
31
+ files:
32
+ - Gemfile
33
+ - Gemfile.lock
34
+ - README.md
35
+ - jsondiff.gemspec
36
+ - lib/json_diff.rb
37
+ - lib/json_diff/array_diff.rb
38
+ - lib/json_diff/hash_diff.rb
39
+ - lib/json_diff/helpers.rb
40
+ - lib/json_diff/version.rb
41
+ - spec/diff_spec.rb
42
+ - spec/spec_helper.rb
43
+ homepage: https://github.com/francois2metz/jsondiff
44
+ licenses: []
45
+ post_install_message:
46
+ rdoc_options: []
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ! '>='
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ requirements: []
62
+ rubyforge_project:
63
+ rubygems_version: 1.8.10
64
+ signing_key:
65
+ specification_version: 3
66
+ summary: ''
67
+ test_files:
68
+ - spec/diff_spec.rb
69
+ - spec/spec_helper.rb