optical_diff 0.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 ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in optical_diff.gemspec
4
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,5 @@
1
+ guard 'rspec', :version => 2, :cli => '-c' do
2
+ watch(%r{^spec/.+_spec\.rb$})
3
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
4
+ watch('spec/spec_helper.rb') { "spec" }
5
+ end
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Yusuke Mito
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,34 @@
1
+ # OpticalDiff
2
+
3
+ This gem can check whether HTMLs are optically the same or not by ignoring specified html elements by parsing input as HTML.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'optical_diff'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install optical_diff
18
+
19
+ ## Usage
20
+
21
+ ```ruby
22
+ html1 = "<html><body>text</body></html>"
23
+ html2 = "<html><body>text<p class = 'random_text'>qwerty</p></body></html>"
24
+ diff = OpticalDiff.diff(html1, html2, :ignore => 'p.random_text')
25
+ diff.changed? #=> false
26
+ ```
27
+
28
+ ## Contributing
29
+
30
+ 1. Fork it
31
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
32
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
33
+ 4. Push to the branch (`git push origin my-new-feature`)
34
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,3 @@
1
+ class OpticalDiff
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,39 @@
1
+ require 'optical_diff/version'
2
+ require 'diffy'
3
+ require 'nokogiri'
4
+
5
+ class OpticalDiff
6
+ class << self
7
+ def diff(html1, html2, options = {})
8
+ options = {
9
+ :ignore => []
10
+ }.merge(options)
11
+
12
+ parsed_htmls = [html1, html2].map {|html| Nokogiri::HTML.parse(html) }
13
+ stripped_htmls = parsed_htmls.map {|html| strip_ignore_elements(html, options[:ignore]) }
14
+ DiffResult.new(Diffy::Diff.new(*stripped_htmls.map(&:to_html)))
15
+ end
16
+
17
+ def strip_ignore_elements(page, ignore = [])
18
+ ignore.each do |selector|
19
+ page.css(selector).remove
20
+ end
21
+ page
22
+ end
23
+ private :strip_ignore_elements
24
+ end
25
+
26
+ class DiffResult
27
+ def initialize(diff)
28
+ @diff = diff
29
+ end
30
+
31
+ def changes
32
+ @diff.select {|line| line =~ /^[+-]/ }
33
+ end
34
+
35
+ def changed?
36
+ changes.size > 0
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,26 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.unshift File.expand_path("../lib", __FILE__)
3
+ require 'optical_diff/version'
4
+
5
+ Gem::Specification.new do |gem|
6
+ gem.authors = ["Yusuke Mito"]
7
+ gem.email = ["y310.1984@gmail.com"]
8
+ gem.description = %q{This gem can check whether HTMLs are optically the same or not by ignoring specified html elements by parsing input as HTML.}
9
+ gem.summary = %q{This gem can check whether HTMLs are optically the same or not by ignoring specified html elements by parsing input as HTML.}
10
+ gem.homepage = "https://github.com/y310/optical_diff"
11
+
12
+ gem.files = `git ls-files`.split($\)
13
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
14
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
15
+ gem.name = "optical_diff"
16
+ gem.require_paths = ["lib"]
17
+ gem.version = OpticalDiff::VERSION
18
+
19
+ gem.add_dependency 'diffy', '~> 2.0'
20
+ gem.add_dependency 'nokogiri', '~> 1.5'
21
+
22
+ gem.add_development_dependency 'rake'
23
+ gem.add_development_dependency 'rspec'
24
+ gem.add_development_dependency 'pry'
25
+ gem.add_development_dependency 'guard-rspec'
26
+ end
@@ -0,0 +1,24 @@
1
+ require 'spec_helper'
2
+
3
+ describe OpticalDiff do
4
+ context 'has no diff' do
5
+ let(:html1) { "<html><body>abcde</body></html>" }
6
+ let(:html2) { "<html><body>abcde</body></html>" }
7
+ subject { OpticalDiff.diff(html1, html2) }
8
+ it { should_not be_changed }
9
+ end
10
+
11
+ context 'has diff' do
12
+ let(:html1) { "<html><body>abcde</body></html>" }
13
+ let(:html2) { "<html><body>aaaaa</body></html>" }
14
+ subject { OpticalDiff.diff(html1, html2) }
15
+ it { should be_changed }
16
+ end
17
+
18
+ context 'ignore elements' do
19
+ let(:html1) { "<html><body>abcde</body></html>" }
20
+ let(:html2) { "<html><body>abcde<p class='ignore'>ignore</p></body></html>" }
21
+ subject { OpticalDiff.diff(html1, html2, :ignore => ['.ignore']) }
22
+ it { should_not be_changed }
23
+ end
24
+ end
@@ -0,0 +1,3 @@
1
+ require 'rspec'
2
+ require 'pry'
3
+ require 'optical_diff'
metadata ADDED
@@ -0,0 +1,162 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: optical_diff
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Yusuke Mito
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-08-31 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: diffy
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ~>
27
+ - !ruby/object:Gem::Version
28
+ hash: 3
29
+ segments:
30
+ - 2
31
+ - 0
32
+ version: "2.0"
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: nokogiri
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ hash: 5
44
+ segments:
45
+ - 1
46
+ - 5
47
+ version: "1.5"
48
+ type: :runtime
49
+ version_requirements: *id002
50
+ - !ruby/object:Gem::Dependency
51
+ name: rake
52
+ prerelease: false
53
+ requirement: &id003 !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ hash: 3
59
+ segments:
60
+ - 0
61
+ version: "0"
62
+ type: :development
63
+ version_requirements: *id003
64
+ - !ruby/object:Gem::Dependency
65
+ name: rspec
66
+ prerelease: false
67
+ requirement: &id004 !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ hash: 3
73
+ segments:
74
+ - 0
75
+ version: "0"
76
+ type: :development
77
+ version_requirements: *id004
78
+ - !ruby/object:Gem::Dependency
79
+ name: pry
80
+ prerelease: false
81
+ requirement: &id005 !ruby/object:Gem::Requirement
82
+ none: false
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ hash: 3
87
+ segments:
88
+ - 0
89
+ version: "0"
90
+ type: :development
91
+ version_requirements: *id005
92
+ - !ruby/object:Gem::Dependency
93
+ name: guard-rspec
94
+ prerelease: false
95
+ requirement: &id006 !ruby/object:Gem::Requirement
96
+ none: false
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ hash: 3
101
+ segments:
102
+ - 0
103
+ version: "0"
104
+ type: :development
105
+ version_requirements: *id006
106
+ description: This gem can check whether HTMLs are optically the same or not by ignoring specified html elements by parsing input as HTML.
107
+ email:
108
+ - y310.1984@gmail.com
109
+ executables: []
110
+
111
+ extensions: []
112
+
113
+ extra_rdoc_files: []
114
+
115
+ files:
116
+ - .gitignore
117
+ - Gemfile
118
+ - Guardfile
119
+ - LICENSE
120
+ - README.md
121
+ - Rakefile
122
+ - lib/optical_diff.rb
123
+ - lib/optical_diff/version.rb
124
+ - optical_diff.gemspec
125
+ - spec/lib/optical_diff_spec.rb
126
+ - spec/spec_helper.rb
127
+ homepage: https://github.com/y310/optical_diff
128
+ licenses: []
129
+
130
+ post_install_message:
131
+ rdoc_options: []
132
+
133
+ require_paths:
134
+ - lib
135
+ required_ruby_version: !ruby/object:Gem::Requirement
136
+ none: false
137
+ requirements:
138
+ - - ">="
139
+ - !ruby/object:Gem::Version
140
+ hash: 3
141
+ segments:
142
+ - 0
143
+ version: "0"
144
+ required_rubygems_version: !ruby/object:Gem::Requirement
145
+ none: false
146
+ requirements:
147
+ - - ">="
148
+ - !ruby/object:Gem::Version
149
+ hash: 3
150
+ segments:
151
+ - 0
152
+ version: "0"
153
+ requirements: []
154
+
155
+ rubyforge_project:
156
+ rubygems_version: 1.8.24
157
+ signing_key:
158
+ specification_version: 3
159
+ summary: This gem can check whether HTMLs are optically the same or not by ignoring specified html elements by parsing input as HTML.
160
+ test_files:
161
+ - spec/lib/optical_diff_spec.rb
162
+ - spec/spec_helper.rb