json-compare 0.1.1 → 0.1.4
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/LICENSE +2 -2
 - data/README.md +5 -0
 - data/TODO +1 -3
 - data/lib/json-compare.rb +2 -1
 - data/lib/json-compare/comparer.rb +4 -0
 - data/lib/json-compare/version.rb +1 -1
 - data/spec/lib/json-compare_spec.rb +31 -0
 - metadata +2 -2
 
    
        data/LICENSE
    CHANGED
    
    | 
         @@ -1,4 +1,4 @@ 
     | 
|
| 
       1 
     | 
    
         
            -
            Copyright (c) 2012  
     | 
| 
      
 1 
     | 
    
         
            +
            Copyright (c) 2012 A2 Design
         
     | 
| 
       2 
2 
     | 
    
         | 
| 
       3 
3 
     | 
    
         
             
            MIT License
         
     | 
| 
       4 
4 
     | 
    
         | 
| 
         @@ -19,4 +19,4 @@ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
     | 
|
| 
       19 
19 
     | 
    
         
             
            NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
         
     | 
| 
       20 
20 
     | 
    
         
             
            LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
         
     | 
| 
       21 
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.
         
     | 
| 
      
 22 
     | 
    
         
            +
            WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
         
     | 
    
        data/README.md
    CHANGED
    
    | 
         @@ -30,6 +30,11 @@ Or install it yourself as: 
     | 
|
| 
       30 
30 
     | 
    
         
             
                old, new = Yajl::Parser.parse(json1), Yajl::Parser.parse(json2)
         
     | 
| 
       31 
31 
     | 
    
         
             
                result = JsonCompare.get_diff(old, new)
         
     | 
| 
       32 
32 
     | 
    
         | 
| 
      
 33 
     | 
    
         
            +
            If you want to exclude some keys from comparison use exclusion param:
         
     | 
| 
      
 34 
     | 
    
         
            +
             
     | 
| 
      
 35 
     | 
    
         
            +
                exclusion = ["from_user", "to_user_id"]
         
     | 
| 
      
 36 
     | 
    
         
            +
                result = JsonCompare.get_diff(old, new, exclusion)
         
     | 
| 
      
 37 
     | 
    
         
            +
             
     | 
| 
       33 
38 
     | 
    
         
             
            ## Contributing
         
     | 
| 
       34 
39 
     | 
    
         | 
| 
       35 
40 
     | 
    
         
             
            1. Fork it
         
     | 
    
        data/TODO
    CHANGED
    
    | 
         @@ -1,7 +1,5 @@ 
     | 
|
| 
       1 
     | 
    
         
            -
            - Add possibility  
     | 
| 
       2 
     | 
    
         
            -
            or in other way to check only some keys
         
     | 
| 
      
 1 
     | 
    
         
            +
            - Add possibility to check only some keys in comparison
         
     | 
| 
       3 
2 
     | 
    
         
             
            - Improve the code quality
         
     | 
| 
       4 
     | 
    
         
            -
            - Make it workable for 1.8.7, rbx and ree
         
     | 
| 
       5 
3 
     | 
    
         
             
            - Add documentation!
         
     | 
| 
       6 
4 
     | 
    
         
             
            - Add more examples!!
         
     | 
| 
       7 
5 
     | 
    
         
             
            - Add more specs!!!
         
     | 
    
        data/lib/json-compare.rb
    CHANGED
    
    | 
         @@ -2,8 +2,9 @@ require 'json-compare/version' 
     | 
|
| 
       2 
2 
     | 
    
         
             
            require 'json-compare/comparer'
         
     | 
| 
       3 
3 
     | 
    
         | 
| 
       4 
4 
     | 
    
         
             
            module JsonCompare
         
     | 
| 
       5 
     | 
    
         
            -
              def self.get_diff(old, new)
         
     | 
| 
      
 5 
     | 
    
         
            +
              def self.get_diff(old, new, exclusion = [])
         
     | 
| 
       6 
6 
     | 
    
         
             
                comparer = JsonCompare::Comparer.new
         
     | 
| 
      
 7 
     | 
    
         
            +
                comparer.excluded_keys = exclusion
         
     | 
| 
       7 
8 
     | 
    
         
             
                comparer.compare_elements(old,new)
         
     | 
| 
       8 
9 
     | 
    
         
             
              end
         
     | 
| 
       9 
10 
     | 
    
         
             
            end
         
     | 
| 
         @@ -1,5 +1,8 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            module JsonCompare
         
     | 
| 
       2 
2 
     | 
    
         
             
              class Comparer
         
     | 
| 
      
 3 
     | 
    
         
            +
             
     | 
| 
      
 4 
     | 
    
         
            +
                attr_accessor :excluded_keys
         
     | 
| 
      
 5 
     | 
    
         
            +
             
     | 
| 
       3 
6 
     | 
    
         
             
                def compare_elements(old, new)
         
     | 
| 
       4 
7 
     | 
    
         
             
                  diff = {}
         
     | 
| 
       5 
8 
     | 
    
         
             
                  if old.kind_of? Hash
         
     | 
| 
         @@ -96,6 +99,7 @@ module JsonCompare 
     | 
|
| 
       96 
99 
     | 
    
         
             
                    temp_hash = {}
         
     | 
| 
       97 
100 
     | 
    
         
             
                    result[change_type].each_key do |key|
         
     | 
| 
       98 
101 
     | 
    
         
             
                      next if result[change_type][key].nil?
         
     | 
| 
      
 102 
     | 
    
         
            +
                      next if @excluded_keys.include? key
         
     | 
| 
       99 
103 
     | 
    
         
             
                      temp_hash[key] = result[change_type][key]
         
     | 
| 
       100 
104 
     | 
    
         
             
                    end
         
     | 
| 
       101 
105 
     | 
    
         
             
                    out_result[change_type] = temp_hash if temp_hash.count > 0
         
     | 
    
        data/lib/json-compare/version.rb
    CHANGED
    
    
| 
         @@ -106,4 +106,35 @@ describe 'Json compare' do 
     | 
|
| 
       106 
106 
     | 
    
         
             
                  result.should eq(expected_result)
         
     | 
| 
       107 
107 
     | 
    
         
             
                end
         
     | 
| 
       108 
108 
     | 
    
         
             
              end
         
     | 
| 
      
 109 
     | 
    
         
            +
             
     | 
| 
      
 110 
     | 
    
         
            +
              describe "keys exclusion" do
         
     | 
| 
      
 111 
     | 
    
         
            +
                it "should be empty hash" do
         
     | 
| 
      
 112 
     | 
    
         
            +
                  json1 = File.new('spec/fixtures/twitter-search.json', 'r')
         
     | 
| 
      
 113 
     | 
    
         
            +
                  json2 = File.new('spec/fixtures/twitter-search2.json', 'r')
         
     | 
| 
      
 114 
     | 
    
         
            +
                  old, new = Yajl::Parser.parse(json1), Yajl::Parser.parse(json2)
         
     | 
| 
      
 115 
     | 
    
         
            +
                  result = JsonCompare.get_diff(old, new, ["results"])
         
     | 
| 
      
 116 
     | 
    
         
            +
                  result.should eq({})
         
     | 
| 
      
 117 
     | 
    
         
            +
                end
         
     | 
| 
      
 118 
     | 
    
         
            +
             
     | 
| 
      
 119 
     | 
    
         
            +
                it "should not contain 'from_user' changes" do
         
     | 
| 
      
 120 
     | 
    
         
            +
                  json1 = File.new('spec/fixtures/twitter-search.json', 'r')
         
     | 
| 
      
 121 
     | 
    
         
            +
                  json2 = File.new('spec/fixtures/twitter-search2.json', 'r')
         
     | 
| 
      
 122 
     | 
    
         
            +
                  old, new = Yajl::Parser.parse(json1), Yajl::Parser.parse(json2)
         
     | 
| 
      
 123 
     | 
    
         
            +
                  result = JsonCompare.get_diff(old, new, ["from_user"])
         
     | 
| 
      
 124 
     | 
    
         
            +
                  expected = {
         
     | 
| 
      
 125 
     | 
    
         
            +
                    :update => {
         
     | 
| 
      
 126 
     | 
    
         
            +
                      "results" => {
         
     | 
| 
      
 127 
     | 
    
         
            +
                        :update => {
         
     | 
| 
      
 128 
     | 
    
         
            +
                          0 => {
         
     | 
| 
      
 129 
     | 
    
         
            +
                            :update => {
         
     | 
| 
      
 130 
     | 
    
         
            +
                              "to_user_id" => "8153091",
         
     | 
| 
      
 131 
     | 
    
         
            +
                            }
         
     | 
| 
      
 132 
     | 
    
         
            +
                          }
         
     | 
| 
      
 133 
     | 
    
         
            +
                        }
         
     | 
| 
      
 134 
     | 
    
         
            +
                      }
         
     | 
| 
      
 135 
     | 
    
         
            +
                    }
         
     | 
| 
      
 136 
     | 
    
         
            +
                  }
         
     | 
| 
      
 137 
     | 
    
         
            +
                  result.should eq(expected)
         
     | 
| 
      
 138 
     | 
    
         
            +
                end
         
     | 
| 
      
 139 
     | 
    
         
            +
              end
         
     | 
| 
       109 
140 
     | 
    
         
             
            end
         
     | 
    
        metadata
    CHANGED
    
    | 
         @@ -1,7 +1,7 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            --- !ruby/object:Gem::Specification
         
     | 
| 
       2 
2 
     | 
    
         
             
            name: json-compare
         
     | 
| 
       3 
3 
     | 
    
         
             
            version: !ruby/object:Gem::Version
         
     | 
| 
       4 
     | 
    
         
            -
              version: 0.1. 
     | 
| 
      
 4 
     | 
    
         
            +
              version: 0.1.4
         
     | 
| 
       5 
5 
     | 
    
         
             
              prerelease: 
         
     | 
| 
       6 
6 
     | 
    
         
             
            platform: ruby
         
     | 
| 
       7 
7 
     | 
    
         
             
            authors:
         
     | 
| 
         @@ -9,7 +9,7 @@ authors: 
     | 
|
| 
       9 
9 
     | 
    
         
             
            autorequire: 
         
     | 
| 
       10 
10 
     | 
    
         
             
            bindir: bin
         
     | 
| 
       11 
11 
     | 
    
         
             
            cert_chain: []
         
     | 
| 
       12 
     | 
    
         
            -
            date: 2012- 
     | 
| 
      
 12 
     | 
    
         
            +
            date: 2012-09-09 00:00:00.000000000 Z
         
     | 
| 
       13 
13 
     | 
    
         
             
            dependencies:
         
     | 
| 
       14 
14 
     | 
    
         
             
            - !ruby/object:Gem::Dependency
         
     | 
| 
       15 
15 
     | 
    
         
             
              name: rake
         
     |