json-compare 0.1
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 +17 -0
- data/.rspec +2 -0
- data/Gemfile +3 -0
- data/LICENSE +22 -0
- data/README.md +35 -0
- data/Rakefile +7 -0
- data/json-compare.gemspec +22 -0
- data/lib/json-compare.rb +9 -0
- data/lib/json-compare/comparer.rb +116 -0
- data/lib/json-compare/version.rb +3 -0
- data/spec/fixtures/twitter-search.json +1 -0
- data/spec/fixtures/twitter-search2.json +1 -0
- data/spec/lib/json-compare_spec.rb +109 -0
- data/spec/spec_helper.rb +6 -0
- metadata +111 -0
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 TODO: Write your name
|
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,35 @@
|
|
1
|
+
# JsonCompare
|
2
|
+
|
3
|
+
Returns the difference between two JSON files.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'json-compare'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install json-compare
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
require 'yajl'
|
22
|
+
require 'json-compare'
|
23
|
+
|
24
|
+
json1 = File.new('spec/fixtures/twitter-search.json', 'r')
|
25
|
+
json2 = File.new('spec/fixtures/twitter-search2.json', 'r')
|
26
|
+
old, new = Yajl::Parser.parse(json1), Yajl::Parser.parse(json2)
|
27
|
+
result = JsonCompare.get_diff(old, new)
|
28
|
+
|
29
|
+
## Contributing
|
30
|
+
|
31
|
+
1. Fork it
|
32
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
33
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
34
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
35
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/json-compare/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Stanislav Chistenko"]
|
6
|
+
gem.email = ["skvest1@gmail.com"]
|
7
|
+
gem.description = %q{Returns the difference between two JSON files}
|
8
|
+
gem.summary = %q{JSON Comparer}
|
9
|
+
gem.homepage = ""
|
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
|
+
gem.name = "json-compare"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = JsonCompare::VERSION
|
17
|
+
|
18
|
+
# tests
|
19
|
+
gem.add_development_dependency 'rake'
|
20
|
+
gem.add_development_dependency 'rspec', ">= 2.0.0"
|
21
|
+
gem.add_development_dependency 'yajl-ruby'
|
22
|
+
end
|
data/lib/json-compare.rb
ADDED
@@ -0,0 +1,116 @@
|
|
1
|
+
module JsonCompare
|
2
|
+
class Comparer
|
3
|
+
def compare_elements(old, new)
|
4
|
+
diff = {}
|
5
|
+
if old.kind_of? Hash
|
6
|
+
if new.kind_of? Array
|
7
|
+
diff_hash = compare_hash_array(old, new)
|
8
|
+
else
|
9
|
+
diff_hash = compare_hashes(old, new)
|
10
|
+
end
|
11
|
+
diff = diff_hash if diff_hash.count > 0
|
12
|
+
elsif old.class != new.class
|
13
|
+
diff = new
|
14
|
+
elsif old.kind_of? Array
|
15
|
+
diff_arr = compare_arrays(old, new)
|
16
|
+
diff = diff_arr if diff_arr.count > 0
|
17
|
+
else
|
18
|
+
string_diff = compare_strings(old, new)
|
19
|
+
diff = string_diff unless string_diff.nil?
|
20
|
+
end
|
21
|
+
diff
|
22
|
+
end
|
23
|
+
|
24
|
+
def compare_hashes(old_hash, new_hash)
|
25
|
+
old_keys = old_hash.keys
|
26
|
+
new_keys = new_hash.keys
|
27
|
+
keys = (old_keys + new_keys).uniq
|
28
|
+
|
29
|
+
result = {
|
30
|
+
append: {},
|
31
|
+
remove: {},
|
32
|
+
update: {}
|
33
|
+
}
|
34
|
+
keys.each do |k|
|
35
|
+
if !old_hash.has_key? k
|
36
|
+
result[:append][k] = new_hash[k]
|
37
|
+
elsif !new_hash.has_key? k
|
38
|
+
result[:remove][k] = new_hash[k]
|
39
|
+
else
|
40
|
+
res = compare_elements(old_hash[k], new_hash[k])
|
41
|
+
result[:update][k] = res unless res.empty?
|
42
|
+
end
|
43
|
+
end
|
44
|
+
filter_results(result)
|
45
|
+
end
|
46
|
+
|
47
|
+
def compare_arrays(old_array, new_array)
|
48
|
+
old_array_length = old_array.count
|
49
|
+
new_array_length = new_array.count
|
50
|
+
inters = [old_array.count, new_array.count].min
|
51
|
+
|
52
|
+
result = {
|
53
|
+
append: {},
|
54
|
+
remove: {},
|
55
|
+
update: {}
|
56
|
+
}
|
57
|
+
|
58
|
+
(0..inters).map do |n|
|
59
|
+
res = compare_elements(old_array[n], new_array[n])
|
60
|
+
result[:update][n] = res unless res.empty?
|
61
|
+
end
|
62
|
+
|
63
|
+
# the rest of the larger array
|
64
|
+
if inters == old_array_length
|
65
|
+
(inters..new_array_length).each do |n|
|
66
|
+
result[:append][n] = new_array[n]
|
67
|
+
end
|
68
|
+
else
|
69
|
+
(inters..old_array_length).each do |n|
|
70
|
+
result[:remove][n] = old_array[n]
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
filter_results(result)
|
75
|
+
end
|
76
|
+
|
77
|
+
def compare_hash_array(old_hash, new_array)
|
78
|
+
result = {
|
79
|
+
append: {},
|
80
|
+
remove: {},
|
81
|
+
update: {}
|
82
|
+
}
|
83
|
+
|
84
|
+
(0..new_array.count).map do |n|
|
85
|
+
next if new_array[n].nil?
|
86
|
+
if n == 0
|
87
|
+
res = compare_elements(old_hash, new_array[0])
|
88
|
+
result[:update][n] = res unless res.empty?
|
89
|
+
else
|
90
|
+
result[:append][n] = new_array[n]
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
filter_results(result)
|
95
|
+
end
|
96
|
+
|
97
|
+
def compare_strings(old_string, new_string)
|
98
|
+
(old_string != new_string) ? new_string.to_s : ""
|
99
|
+
end
|
100
|
+
|
101
|
+
def filter_results(result)
|
102
|
+
return {} if result.nil?
|
103
|
+
out_result = {}
|
104
|
+
result.each_key do |change_type|
|
105
|
+
next if result[change_type].nil?
|
106
|
+
temp_hash = {}
|
107
|
+
result[change_type].each_key do |key|
|
108
|
+
next if result[change_type][key].nil?
|
109
|
+
temp_hash[key] = result[change_type][key]
|
110
|
+
end
|
111
|
+
out_result[change_type] = temp_hash if temp_hash.count > 0
|
112
|
+
end
|
113
|
+
out_result
|
114
|
+
end
|
115
|
+
end
|
116
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
{"results":[{"text":"@stroughtonsmith You need to add a "Favourites" tab to TC\/iPhone. Like what TwitterFon did. I can't WAIT for your Twitter App!! :) Any ETA?","to_user_id":815309,"to_user":"stroughtonsmith","from_user":"Shaun_R","id":1125687077,"from_user_id":855523,"iso_language_code":"en","profile_image_url":"http:\/\/s3.amazonaws.com\/twitter_production\/profile_images\/68778135\/Safari_Dude_normal.jpg","created_at":"Sat, 17 Jan 2009 06:14:13 +0000"},{"text":"Beginning to understand the Twitter world...and liking it.","to_user_id":null,"from_user":"AWheeler15","id":1125687050,"from_user_id":3694831,"iso_language_code":"en","profile_image_url":"http:\/\/s3.amazonaws.com\/twitter_production\/profile_images\/71564236\/Photo_2_normal.jpg","created_at":"Sat, 17 Jan 2009 06:14:11 +0000"},{"text":"@genar hehe, she cant twitter from work, hasnt got it set up on the phone, and on our workout nights generally the computer is untouched too","to_user_id":1089113,"to_user":"genar","from_user":"donro","id":1125687042,"from_user_id":1907789,"iso_language_code":"en","profile_image_url":"http:\/\/s3.amazonaws.com\/twitter_production\/profile_images\/68316085\/stef_and_don_normal.jpg","created_at":"Sat, 17 Jan 2009 06:14:11 +0000"},{"text":"My morning routine: mail, flickr, google reader, friendfeed, twitter replies http:\/\/ff.im\/-DMrn","to_user_id":null,"from_user":"hakandahlstrom","id":1125686913,"from_user_id":213116,"iso_language_code":"en","profile_image_url":"http:\/\/s3.amazonaws.com\/twitter_production\/profile_images\/67707376\/squarelg_normal.jpg","created_at":"Sat, 17 Jan 2009 06:14:04 +0000"},{"text":"@LeeCollins If you have not seen Lee's Website..Check it out ..Perfect layout. Also.. Twitter Photo tool","to_user_id":381690,"to_user":"leecollins","from_user":"MichaelGPerry","id":1125686877,"from_user_id":2765433,"iso_language_code":"en","profile_image_url":"http:\/\/s3.amazonaws.com\/twitter_production\/profile_images\/70614206\/MG_Perry_normal.JPG","created_at":"Sat, 17 Jan 2009 06:14:02 +0000"},{"text":"Just Buzzed My Blog:: New Friend @AlohaArlene Gets Twooted From Twitter http:\/\/tinyurl.com\/8hd7qy","to_user_id":null,"from_user":"BabyBloggerBrie","id":1125686854,"from_user_id":3593267,"iso_language_code":"nl","profile_image_url":"http:\/\/s3.amazonaws.com\/twitter_production\/profile_images\/70969980\/brie_normal.jpg","created_at":"Sat, 17 Jan 2009 06:14:02 +0000"},{"text":"Current will air the inauguration while streaming tweets from the twitter audience on the TV as we watch. Check it - http:\/\/ub0.cc\/7C\/2d","to_user_id":null,"from_user":"my3rdeye","id":1125686843,"from_user_id":2553098,"iso_language_code":"en","profile_image_url":"http:\/\/s3.amazonaws.com\/twitter_production\/profile_images\/67353656\/Charlie_Boognish_normal.jpg","created_at":"Sat, 17 Jan 2009 06:14:01 +0000"},{"text":"milestone: Twitter Grader has now graded 1,000,000 unique twitter accounts. Woo hoo! (via @grader)","to_user_id":null,"from_user":"christyitamoto","id":1125686812,"from_user_id":1549031,"iso_language_code":"en","profile_image_url":"http:\/\/s3.amazonaws.com\/twitter_production\/profile_images\/60294809\/MyPicture_normal.jpg","created_at":"Sat, 17 Jan 2009 06:13:59 +0000"},{"text":"Twitter-Yahoo Mashup Yields Impressive News Search Engine http:\/\/twurl.nl\/pg8sxs","to_user_id":null,"from_user":"synectic","id":1125686791,"from_user_id":2563073,"iso_language_code":"en","profile_image_url":"http:\/\/s3.amazonaws.com\/twitter_production\/profile_images\/67483750\/8yplOv7l.kokopelli_trans_normal.png","created_at":"Sat, 17 Jan 2009 06:13:58 +0000"},{"text":"RT: @sarahamrin You really know how to work Twitter. *scribbles another mark for Sarah on the International T.. http:\/\/tinyurl.com\/7xt8hb","to_user_id":null,"from_user":"howtotweets","id":1125686790,"from_user_id":3437258,"iso_language_code":"en","profile_image_url":"http:\/\/static.twitter.com\/images\/default_profile_normal.png","created_at":"Sat, 17 Jan 2009 06:13:58 +0000"},{"text":"IPhone App Reviews - Breaking News in the 09s: iPhone and Twitter: Breaking News in the 09s: iPhone and Twitter .. http:\/\/tinyurl.com\/922qcl","to_user_id":null,"from_user":"ifones","id":1125686749,"from_user_id":1412337,"iso_language_code":"en","profile_image_url":"http:\/\/static.twitter.com\/images\/default_profile_normal.png","created_at":"Sat, 17 Jan 2009 06:13:56 +0000"},{"text":"RT: @davidall's book about how to use twitter RULES!! You can get it here: http:\/\/tinyurl.com\/495nm2 http:\/\/tinyurl.com\/8kuva5","to_user_id":null,"from_user":"howtotweets","id":1125686716,"from_user_id":3437258,"iso_language_code":"en","profile_image_url":"http:\/\/static.twitter.com\/images\/default_profile_normal.png","created_at":"Sat, 17 Jan 2009 06:13:54 +0000"},{"text":"@ev new 2 twitter & already hooked, thx 4 the welcome. It's rough being a newbie","to_user_id":5621,"to_user":"ev","from_user":"jgordo","id":1125686687,"from_user_id":3696186,"iso_language_code":"en","profile_image_url":"http:\/\/s3.amazonaws.com\/twitter_production\/profile_images\/68508879\/Img00020_normal.jpg","created_at":"Sat, 17 Jan 2009 06:13:53 +0000"},{"text":"Twitter applicatie TweetDeck heeft een investering v $500k binnengehaald: http:\/\/twurl.nl\/gfei3i","to_user_id":null,"from_user":"gvenkdaily","id":1125686526,"from_user_id":230616,"iso_language_code":"nl","profile_image_url":"http:\/\/s3.amazonaws.com\/twitter_production\/profile_images\/55316327\/gvenkdaily-logo-vierkant_normal.png","created_at":"Sat, 17 Jan 2009 06:13:46 +0000"},{"text":"We are like Twitter Retards.. HA ha ha. I thought I was going to be gay, but I totally changed my mind after being chewed on the other night","to_user_id":null,"from_user":"Aroyal88","id":1125686475,"from_user_id":3219428,"iso_language_code":"en","profile_image_url":"http:\/\/static.twitter.com\/images\/default_profile_normal.png","created_at":"Sat, 17 Jan 2009 06:13:43 +0000"}],"since_id":0,"max_id":1125687077,"refresh_url":"?since_id=1125687077&q=twitter","results_per_page":15,"next_page":"?page=2&max_id=1125687077&q=twitter","completed_in":0.01338,"page":1,"query":"twitter"}
|
@@ -0,0 +1 @@
|
|
1
|
+
{"results":[{"text":"@stroughtonsmith You need to add a "Favourites" tab to TC\/iPhone. Like what TwitterFon did. I can't WAIT for your Twitter App!! :) Any ETA?","to_user_id":8153091,"to_user":"stroughtonsmith","from_user":"Shaun_R","id":1125687077,"from_user_id":855523,"iso_language_code":"en","profile_image_url":"http:\/\/s3.amazonaws.com\/twitter_production\/profile_images\/68778135\/Safari_Dude_normal.jpg","created_at":"Sat, 17 Jan 2009 06:14:13 +0000"},{"text":"Beginning to understand the Twitter world...and liking it.","to_user_id":null,"from_user":"AWheeler156","id":1125687050,"from_user_id":3694831,"iso_language_code":"en","profile_image_url":"http:\/\/s3.amazonaws.com\/twitter_production\/profile_images\/71564236\/Photo_2_normal.jpg","created_at":"Sat, 17 Jan 2009 06:14:11 +0000"},{"text":"@genar hehe, she cant twitter from work, hasnt got it set up on the phone, and on our workout nights generally the computer is untouched too","to_user_id":1089113,"to_user":"genar","from_user":"donro","id":1125687042,"from_user_id":1907789,"iso_language_code":"en","profile_image_url":"http:\/\/s3.amazonaws.com\/twitter_production\/profile_images\/68316085\/stef_and_don_normal.jpg","created_at":"Sat, 17 Jan 2009 06:14:11 +0000"},{"text":"My morning routine: mail, flickr, google reader, friendfeed, twitter replies http:\/\/ff.im\/-DMrn","to_user_id":null,"from_user":"hakandahlstrom","id":1125686913,"from_user_id":213116,"iso_language_code":"en","profile_image_url":"http:\/\/s3.amazonaws.com\/twitter_production\/profile_images\/67707376\/squarelg_normal.jpg","created_at":"Sat, 17 Jan 2009 06:14:04 +0000"},{"text":"@LeeCollins If you have not seen Lee's Website..Check it out ..Perfect layout. Also.. Twitter Photo tool","to_user_id":381690,"to_user":"leecollins","from_user":"MichaelGPerry","id":1125686877,"from_user_id":2765433,"iso_language_code":"en","profile_image_url":"http:\/\/s3.amazonaws.com\/twitter_production\/profile_images\/70614206\/MG_Perry_normal.JPG","created_at":"Sat, 17 Jan 2009 06:14:02 +0000"},{"text":"Just Buzzed My Blog:: New Friend @AlohaArlene Gets Twooted From Twitter http:\/\/tinyurl.com\/8hd7qy","to_user_id":null,"from_user":"BabyBloggerBrie","id":1125686854,"from_user_id":3593267,"iso_language_code":"nl","profile_image_url":"http:\/\/s3.amazonaws.com\/twitter_production\/profile_images\/70969980\/brie_normal.jpg","created_at":"Sat, 17 Jan 2009 06:14:02 +0000"},{"text":"Current will air the inauguration while streaming tweets from the twitter audience on the TV as we watch. Check it - http:\/\/ub0.cc\/7C\/2d","to_user_id":null,"from_user":"my3rdeye","id":1125686843,"from_user_id":2553098,"iso_language_code":"en","profile_image_url":"http:\/\/s3.amazonaws.com\/twitter_production\/profile_images\/67353656\/Charlie_Boognish_normal.jpg","created_at":"Sat, 17 Jan 2009 06:14:01 +0000"},{"text":"milestone: Twitter Grader has now graded 1,000,000 unique twitter accounts. Woo hoo! (via @grader)","to_user_id":null,"from_user":"christyitamoto","id":1125686812,"from_user_id":1549031,"iso_language_code":"en","profile_image_url":"http:\/\/s3.amazonaws.com\/twitter_production\/profile_images\/60294809\/MyPicture_normal.jpg","created_at":"Sat, 17 Jan 2009 06:13:59 +0000"},{"text":"Twitter-Yahoo Mashup Yields Impressive News Search Engine http:\/\/twurl.nl\/pg8sxs","to_user_id":null,"from_user":"synectic","id":1125686791,"from_user_id":2563073,"iso_language_code":"en","profile_image_url":"http:\/\/s3.amazonaws.com\/twitter_production\/profile_images\/67483750\/8yplOv7l.kokopelli_trans_normal.png","created_at":"Sat, 17 Jan 2009 06:13:58 +0000"},{"text":"RT: @sarahamrin You really know how to work Twitter. *scribbles another mark for Sarah on the International T.. http:\/\/tinyurl.com\/7xt8hb","to_user_id":null,"from_user":"howtotweets","id":1125686790,"from_user_id":3437258,"iso_language_code":"en","profile_image_url":"http:\/\/static.twitter.com\/images\/default_profile_normal.png","created_at":"Sat, 17 Jan 2009 06:13:58 +0000"},{"text":"IPhone App Reviews - Breaking News in the 09s: iPhone and Twitter: Breaking News in the 09s: iPhone and Twitter .. http:\/\/tinyurl.com\/922qcl","to_user_id":null,"from_user":"ifones","id":1125686749,"from_user_id":1412337,"iso_language_code":"en","profile_image_url":"http:\/\/static.twitter.com\/images\/default_profile_normal.png","created_at":"Sat, 17 Jan 2009 06:13:56 +0000"},{"text":"RT: @davidall's book about how to use twitter RULES!! You can get it here: http:\/\/tinyurl.com\/495nm2 http:\/\/tinyurl.com\/8kuva5","to_user_id":null,"from_user":"howtotweets","id":1125686716,"from_user_id":3437258,"iso_language_code":"en","profile_image_url":"http:\/\/static.twitter.com\/images\/default_profile_normal.png","created_at":"Sat, 17 Jan 2009 06:13:54 +0000"},{"text":"@ev new 2 twitter & already hooked, thx 4 the welcome. It's rough being a newbie","to_user_id":5621,"to_user":"ev","from_user":"jgordo","id":1125686687,"from_user_id":3696186,"iso_language_code":"en","profile_image_url":"http:\/\/s3.amazonaws.com\/twitter_production\/profile_images\/68508879\/Img00020_normal.jpg","created_at":"Sat, 17 Jan 2009 06:13:53 +0000"},{"text":"Twitter applicatie TweetDeck heeft een investering v $500k binnengehaald: http:\/\/twurl.nl\/gfei3i","to_user_id":null,"from_user":"gvenkdaily","id":1125686526,"from_user_id":230616,"iso_language_code":"nl","profile_image_url":"http:\/\/s3.amazonaws.com\/twitter_production\/profile_images\/55316327\/gvenkdaily-logo-vierkant_normal.png","created_at":"Sat, 17 Jan 2009 06:13:46 +0000"},{"text":"We are like Twitter Retards.. HA ha ha. I thought I was going to be gay, but I totally changed my mind after being chewed on the other night","to_user_id":null,"from_user":"Aroyal88","id":1125686475,"from_user_id":3219428,"iso_language_code":"en","profile_image_url":"http:\/\/static.twitter.com\/images\/default_profile_normal.png","created_at":"Sat, 17 Jan 2009 06:13:43 +0000"}],"since_id":0,"max_id":1125687077,"refresh_url":"?since_id=1125687077&q=twitter","results_per_page":15,"next_page":"?page=2&max_id=1125687077&q=twitter","completed_in":0.01338,"page":1,"query":"twitter"}
|
@@ -0,0 +1,109 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'Json compare' do
|
4
|
+
describe 'Strings Comparison' do
|
5
|
+
it 'should return empty String' do
|
6
|
+
result = JsonCompare.get_diff('DummyTest', 'DummyTest')
|
7
|
+
result.should eq('')
|
8
|
+
end
|
9
|
+
|
10
|
+
it 'should return new String' do
|
11
|
+
result = JsonCompare.get_diff('Test', 'DummyTest')
|
12
|
+
result.should eq('DummyTest')
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
describe 'Hashes Comparison' do
|
17
|
+
it 'should return empty Hash' do
|
18
|
+
result = JsonCompare.get_diff({}, {})
|
19
|
+
result.should eq({})
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'should return empty Hash' do
|
23
|
+
json = File.new('spec/fixtures/twitter-search.json', 'r')
|
24
|
+
new = old = Yajl::Parser.parse(json)
|
25
|
+
result = JsonCompare.get_diff(old, new)
|
26
|
+
result.should eq({})
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'should return Hash with diff' do
|
30
|
+
json1 = File.new('spec/fixtures/twitter-search.json', 'r')
|
31
|
+
json2 = File.new('spec/fixtures/twitter-search2.json', 'r')
|
32
|
+
old, new = Yajl::Parser.parse(json1), Yajl::Parser.parse(json2)
|
33
|
+
result = JsonCompare.get_diff(old, new)
|
34
|
+
expected = {
|
35
|
+
:update => {
|
36
|
+
"results" => {
|
37
|
+
:update => {
|
38
|
+
0 => {
|
39
|
+
:update => {
|
40
|
+
"to_user_id" => "8153091",
|
41
|
+
}
|
42
|
+
},
|
43
|
+
1 => {
|
44
|
+
:update => {
|
45
|
+
"from_user" => "AWheeler156",
|
46
|
+
}
|
47
|
+
}
|
48
|
+
}
|
49
|
+
}
|
50
|
+
}
|
51
|
+
}
|
52
|
+
result.should eq(expected)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
describe 'Arrays Comparison' do
|
57
|
+
it 'should return empty Hash' do
|
58
|
+
result = JsonCompare.get_diff([],[])
|
59
|
+
result.should eq({})
|
60
|
+
end
|
61
|
+
|
62
|
+
it "should return empty Hash" do
|
63
|
+
old = new = [{
|
64
|
+
"ID" => "545",
|
65
|
+
"Data" => {
|
66
|
+
"Something" => [{"Something" => [{}]}]
|
67
|
+
}
|
68
|
+
}]
|
69
|
+
result = JsonCompare.get_diff(old,new)
|
70
|
+
result.should eq({})
|
71
|
+
end
|
72
|
+
|
73
|
+
it "should return Hash with diffs" do
|
74
|
+
old = [{
|
75
|
+
"ID" => "545",
|
76
|
+
"Data" => {
|
77
|
+
"Something" => [{"Something" => [{}]}]
|
78
|
+
}
|
79
|
+
}]
|
80
|
+
new = [{
|
81
|
+
"ID" => "546",
|
82
|
+
"Data" => {
|
83
|
+
"Something2" => [{"Something2" => [{"empty" => nil}]}]
|
84
|
+
}
|
85
|
+
}]
|
86
|
+
|
87
|
+
result = JsonCompare.get_diff(old,new)
|
88
|
+
expected_result = {
|
89
|
+
:update => {
|
90
|
+
0 => {
|
91
|
+
:update => {
|
92
|
+
"ID"=>"546",
|
93
|
+
"Data" => {
|
94
|
+
:append => {
|
95
|
+
"Something2" => [{
|
96
|
+
"Something2" => [{
|
97
|
+
"empty"=>nil
|
98
|
+
}]
|
99
|
+
}]
|
100
|
+
}
|
101
|
+
}
|
102
|
+
}
|
103
|
+
}
|
104
|
+
}
|
105
|
+
}
|
106
|
+
result.should eq(expected_result)
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,111 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: json-compare
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: '0.1'
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Stanislav Chistenko
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-08-07 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rake
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
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: rspec
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: 2.0.0
|
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: 2.0.0
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: yajl-ruby
|
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
|
+
description: Returns the difference between two JSON files
|
63
|
+
email:
|
64
|
+
- skvest1@gmail.com
|
65
|
+
executables: []
|
66
|
+
extensions: []
|
67
|
+
extra_rdoc_files: []
|
68
|
+
files:
|
69
|
+
- .gitignore
|
70
|
+
- .rspec
|
71
|
+
- Gemfile
|
72
|
+
- LICENSE
|
73
|
+
- README.md
|
74
|
+
- Rakefile
|
75
|
+
- json-compare.gemspec
|
76
|
+
- lib/json-compare.rb
|
77
|
+
- lib/json-compare/comparer.rb
|
78
|
+
- lib/json-compare/version.rb
|
79
|
+
- spec/fixtures/twitter-search.json
|
80
|
+
- spec/fixtures/twitter-search2.json
|
81
|
+
- spec/lib/json-compare_spec.rb
|
82
|
+
- spec/spec_helper.rb
|
83
|
+
homepage: ''
|
84
|
+
licenses: []
|
85
|
+
post_install_message:
|
86
|
+
rdoc_options: []
|
87
|
+
require_paths:
|
88
|
+
- lib
|
89
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
90
|
+
none: false
|
91
|
+
requirements:
|
92
|
+
- - ! '>='
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '0'
|
95
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
96
|
+
none: false
|
97
|
+
requirements:
|
98
|
+
- - ! '>='
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
version: '0'
|
101
|
+
requirements: []
|
102
|
+
rubyforge_project:
|
103
|
+
rubygems_version: 1.8.24
|
104
|
+
signing_key:
|
105
|
+
specification_version: 3
|
106
|
+
summary: JSON Comparer
|
107
|
+
test_files:
|
108
|
+
- spec/fixtures/twitter-search.json
|
109
|
+
- spec/fixtures/twitter-search2.json
|
110
|
+
- spec/lib/json-compare_spec.rb
|
111
|
+
- spec/spec_helper.rb
|