domreactor-redglass 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 1.9.3-p448
data/README.md CHANGED
@@ -31,7 +31,7 @@ end
31
31
 
32
32
  # Send the page archives to DomReactor.
33
33
  DomReactorRedGlass.auth_token = '12345'
34
- DomReactorRedGlass.create_chain_reaction(page_url, "#{archive_location}/#{test_id}", opts)
34
+ DomReactorRedGlass.create_chain_reaction("#{archive_location}/#{test_id}", opts)
35
35
  ```
36
36
 
37
37
  # License
@@ -6,7 +6,7 @@ require 'domreactor-redglass/version'
6
6
  Gem::Specification.new do |s|
7
7
  s.name = 'domreactor-redglass'
8
8
  s.version = DomReactorRedGlass::VERSION
9
- s.date = '2013-10-31'
9
+ s.date = '2013-11-14'
10
10
  s.summary = 'DomReactor plugin for RedGlass.'
11
11
  s.description = 'Send RedGlass page archives to DomReactor for automated layout analysis.'
12
12
  s.authors = ["Frank O'Hara", "Chris Lamb"]
@@ -24,4 +24,4 @@ end
24
24
 
25
25
  # Send the page archives to DomReactor.
26
26
  DomReactorRedGlass.auth_token = '12345'
27
- DomReactorRedGlass.create_chain_reaction('http://google.com', "#{archive_location}/#{test_id}", opts)
27
+ DomReactorRedGlass.create_chain_reaction("#{archive_location}/#{test_id}", opts)
@@ -0,0 +1,50 @@
1
+ class Archive
2
+ attr_reader :path, :opts
3
+
4
+ REQUIRED_ARCHIVE_FILES = %w(dom.json metadata.json screenshot.png source.html)
5
+
6
+ def initialize(path, opts={})
7
+ @path = path
8
+ @opts = opts
9
+ end
10
+
11
+ def meta_data
12
+ @meta_data = parse_json_file("#{path}/metadata.json")
13
+ end
14
+
15
+ def browser
16
+ meta_data[:browser]
17
+ end
18
+
19
+ def page_url
20
+ meta_data[:page_url]
21
+ end
22
+
23
+ def dom_elements
24
+ @dom_elements ||= File.open("#{path}/dom.json", 'rb') {|f| f.read}
25
+ end
26
+
27
+ def page_source
28
+ @page_source ||= File.open("#{path}/source.html") {|f| f.read}
29
+ end
30
+
31
+ def screenshot
32
+ @file ||= File.open("#{path}/screenshot.png")
33
+ end
34
+
35
+ def self.is_valid_page_archive?(file)
36
+ is_valid = false
37
+ if File.directory? file
38
+ dir_files = Dir.entries(file).delete_if {|name| name == '.' || name == '..'}
39
+ is_valid = dir_files.sort == REQUIRED_ARCHIVE_FILES.sort
40
+ end
41
+ is_valid
42
+ end
43
+
44
+ private
45
+
46
+ def parse_json_file(path)
47
+ json_str = File.open(path, 'rb') {|f| f.read}
48
+ JSON.parse(json_str, symbolize_names: true)
49
+ end
50
+ end
@@ -0,0 +1,82 @@
1
+ class ArchiveLocation
2
+ attr_reader :location, :opts
3
+
4
+ REQUIRED_BASELINE_BROWSER_CONFIG_KEYS = [:name, :version, :platform]
5
+
6
+ def initialize(location, opts={})
7
+ @location = location
8
+ @opts = opts
9
+ end
10
+
11
+ def archives
12
+ @archives ||= archive_list(location)
13
+ end
14
+
15
+ def validate!
16
+ detect_archive_location
17
+ detect_min_archive_quota
18
+ detect_baseline_browser
19
+ end
20
+
21
+ def detect_archive_location
22
+ unless File.directory?(location)
23
+ raise 'A valid archive location is required.'
24
+ end
25
+ end
26
+
27
+ def detect_min_archive_quota
28
+ raise 'At least two valid page archives are required.' unless sum_archive_count(location) >= 2
29
+ end
30
+
31
+ def detect_baseline_browser
32
+ unless opts[:baseline_browser] && is_valid_baseline_browser_config?(opts[:baseline_browser])
33
+ raise 'A valid baseline_browser configuration is required.'
34
+ end
35
+ unless has_baseline_archive?(location, opts)
36
+ raise 'A page archive that corresponds to the baseline browser configuration is required.'
37
+ end
38
+ end
39
+
40
+ def is_valid_baseline_browser_config?(baseline_browser_config)
41
+ baseline_browser_config.keys.sort == REQUIRED_BASELINE_BROWSER_CONFIG_KEYS.sort
42
+ end
43
+
44
+
45
+ private
46
+
47
+ def has_baseline_archive?(archive_location, config)
48
+ found_archive = false
49
+ Dir.foreach(archive_location) do |file|
50
+ next if file == '.' or file == '..'
51
+ path = "#{archive_location}/#{file}"
52
+ if Archive.is_valid_page_archive?(path)
53
+ if Archive.new(path).browser == config[:baseline_browser]
54
+ found_archive = true
55
+ break
56
+ end
57
+ end
58
+ end
59
+ found_archive
60
+ end
61
+
62
+ def sum_archive_count(archive_location)
63
+ archive_count = 0
64
+ Dir.foreach(archive_location) do |file|
65
+ next if file == '.' or file == '..'
66
+ archive_count += 1 if Archive.is_valid_page_archive?("#{archive_location}/#{file}")
67
+ end
68
+ archive_count
69
+ end
70
+
71
+ def archive_list(archive_location)
72
+ list = []
73
+ Dir.foreach(archive_location) do |file|
74
+ next if file == '.' or file == '..'
75
+ path = "#{archive_location}/#{file}"
76
+ if Archive.is_valid_page_archive? path
77
+ list << Archive.new(path)
78
+ end
79
+ end
80
+ list
81
+ end
82
+ end
@@ -3,10 +3,9 @@ require 'rest-client'
3
3
 
4
4
  module DomReactorRedGlass
5
5
  class ChainReaction
6
- attr_reader :id, :info, :page_url
6
+ attr_reader :id, :info
7
7
 
8
- def initialize(page_url, opts)
9
- @page_url = page_url
8
+ def initialize(opts)
10
9
  @opts = opts
11
10
  @info = info
12
11
  end
@@ -33,13 +32,9 @@ module DomReactorRedGlass
33
32
 
34
33
 
35
34
  def post_archives(archive_location)
36
- Dir.foreach(archive_location) do |file|
37
- next if file == '.' or file == '..'
38
- path = "#{archive_location}/#{file}"
39
- if is_valid_page_archive? path
40
- payload = create_payload(path)
41
- create_dom_gun_reaction(payload)
42
- end
35
+ archive_location.archives.each do |archive|
36
+ payload = create_payload(archive)
37
+ create_dom_gun_reaction(payload)
43
38
  end
44
39
  start_reaction
45
40
  end
@@ -85,16 +80,15 @@ module DomReactorRedGlass
85
80
  JSON.parse(response, symbolize_names: true)[:chain_reaction]
86
81
  end
87
82
 
88
- def create_payload(path)
89
- meta_data = parse_json_file("#{path}/metadata.json")
83
+ def create_payload(archive)
90
84
  {
91
85
  auth_token: auth_token,
92
- meta_data: meta_data,
93
- web_browser_id: get_web_browser_info(meta_data[:browser])[:id],
94
- page_url: meta_data[:page_url],
95
- dom_elements: File.open("#{path}/dom.json", 'rb') {|f| f.read},
96
- page_source: File.open("#{path}/source.html") {|f| f.read},
97
- file: File.open("#{path}/screenshot.png")
86
+ meta_data: archive.meta_data,
87
+ web_browser_id: get_web_browser_info(archive.browser)[:id],
88
+ page_url: archive.page_url,
89
+ dom_elements: archive.dom_elements,
90
+ page_source: archive.page_source,
91
+ file: archive.screenshot
98
92
  }
99
93
  end
100
94
  end
@@ -1,7 +1,6 @@
1
1
  module DomReactorRedGlass
2
2
  class Config
3
- #DOMREACTOR_INIT_CHAIN_REACTION_URL = 'http://domreactor.com'
4
- DOMREACTOR_INIT_CHAIN_REACTION_URL = 'http://localhost:3000'
3
+ DOMREACTOR_INIT_CHAIN_REACTION_URL = 'http://www.domreactor.com'
5
4
 
6
5
  class << self
7
6
  def url
@@ -1,3 +1,3 @@
1
1
  module DomReactorRedGlass
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
@@ -1,12 +1,11 @@
1
1
  require 'domreactor-redglass/chain_reaction'
2
+ require 'domreactor-redglass/archive'
3
+ require 'domreactor-redglass/archive_location'
2
4
  require 'domreactor-redglass/config'
3
5
  require 'domreactor-redglass/version'
4
6
 
5
7
  module DomReactorRedGlass
6
8
 
7
- REQUIRED_ARCHIVE_FILES = %w(dom.json metadata.json screenshot.png source.html)
8
- REQUIRED_BASELINE_BROWSER_CONFIG_KEYS = [:name, :version, :platform]
9
-
10
9
  def auth_token=(auth_token)
11
10
  Config.auth_token=auth_token
12
11
  end
@@ -15,82 +14,13 @@ module DomReactorRedGlass
15
14
  Config.auth_token
16
15
  end
17
16
 
18
- def create_chain_reaction(page_url, archive_location, opts)
19
- detect_archive_location archive_location
20
- detect_min_archive_quota archive_location
21
- detect_baseline_browser archive_location, opts
22
- @chain_reaction = ChainReaction.new(page_url, opts)
17
+ def create_chain_reaction(archive_location, opts)
18
+ archive_location = ArchiveLocation.new(archive_location, opts)
19
+ archive_location.validate!
20
+ @chain_reaction = ChainReaction.new(opts)
23
21
  @chain_reaction.post_archives(archive_location)
24
22
  end
25
23
 
26
- def detect_archive_location(archive_location)
27
- unless File.directory?(archive_location)
28
- raise 'A valid archive location is required.'
29
- end
30
- end
31
-
32
- def detect_min_archive_quota(archive_location)
33
- raise 'At least two valid page archives are required.' unless sum_archive_count(archive_location) >= 2
34
- end
35
-
36
- def sum_archive_count(archive_location)
37
- archive_count = 0
38
- Dir.foreach(archive_location) do |file|
39
- next if file == '.' or file == '..'
40
- archive_count += 1 if is_valid_page_archive? "#{archive_location}/#{file}"
41
- end
42
- archive_count
43
- end
44
-
45
- def is_valid_page_archive?(file)
46
- is_valid = false
47
- if File.directory? file
48
- dir_files = Dir.entries(file).delete_if {|name| name == '.' || name == '..'}
49
- is_valid = true if dir_files == REQUIRED_ARCHIVE_FILES
50
- end
51
- is_valid
52
- end
53
-
54
- def detect_baseline_browser(archive_location, config)
55
- unless config[:baseline_browser] && is_valid_baseline_browser_config?(config[:baseline_browser])
56
- raise 'A valid baseline_browser configuration is required.'
57
- end
58
- unless has_baseline_archive?(archive_location, config)
59
- raise 'A page archive that corresponds to the baseline browser configuration is required.'
60
- end
61
- end
62
-
63
- def is_valid_baseline_browser_config?(baseline_browser_config)
64
- baseline_browser_config.keys == REQUIRED_BASELINE_BROWSER_CONFIG_KEYS
65
- end
66
-
67
- def has_baseline_archive?(archive_location, config)
68
- found_archive = false
69
- Dir.foreach(archive_location) do |file|
70
- next if file == '.' or file == '..'
71
- path = "#{archive_location}/#{file}"
72
- if is_valid_page_archive? path
73
- if parse_json_file("#{path}/metadata.json")[:browser] == config[:baseline_browser]
74
- found_archive = true
75
- break
76
- end
77
- end
78
- end
79
- found_archive
80
- end
81
-
82
- def archive_metadata(archive_location)
83
- metadata = []
84
- Dir.foreach(archive_location) do |file|
85
- next if file == '.' or file == '..'
86
- path = "#{archive_location}/#{file}"
87
- if is_valid_page_archive? path
88
- metadata << parse_json_file("#{path}/metadata.json")
89
- end
90
- end
91
- metadata
92
- end
93
-
94
24
  def parse_json_file(path)
95
25
  json_str = File.open(path, 'rb') {|f| f.read}
96
26
  JSON.parse(json_str, symbolize_names: true)
@@ -0,0 +1,31 @@
1
+ require 'spec_helper'
2
+
3
+ describe ArchiveLocation do
4
+ describe '#detect_archive_location' do
5
+ it 'requires a valid archive location' do
6
+ expect { ArchiveLocation.new('/does_not_exist').detect_archive_location }
7
+ .to raise_error('A valid archive location is required.')
8
+ end
9
+ end
10
+ describe '#detect_min_archive_quota' do
11
+ it 'requires at last two valid page archives' do
12
+ expect { ArchiveLocation.new("#{SPEC_ROOT}/data/invalid_archive_by_quota").detect_min_archive_quota }
13
+ .to raise_error('At least two valid page archives are required.')
14
+ end
15
+ end
16
+ describe '#detect_baseline_browser' do
17
+ it 'requires a baseline_browser config' do
18
+ expect { ArchiveLocation.new('', {}).detect_baseline_browser }
19
+ .to raise_error('A valid baseline_browser configuration is required.')
20
+ end
21
+ it 'requires a valid baseline_browser config' do
22
+ expect { ArchiveLocation.new('', {baseline_browser: {name: '', version: ''}}).detect_baseline_browser }
23
+ .to raise_error('A valid baseline_browser configuration is required.')
24
+ end
25
+ it 'requires a corresponding page archive' do
26
+ expect { ArchiveLocation.new("#{SPEC_ROOT}/data/valid_archive", {
27
+ baseline_browser: {name: 'mosaic', version: '1.0', platform: 'darwin'}}).detect_baseline_browser }
28
+ .to raise_error('A page archive that corresponds to the baseline browser configuration is required.')
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,27 @@
1
+ require 'spec_helper'
2
+
3
+ describe Archive do
4
+ subject { Archive.new("test") }
5
+ let(:meta_data) do
6
+ {
7
+ browser: 'firefox',
8
+ page_url: 'www.example.com',
9
+ }
10
+ end
11
+ before { File.stub(:open) { meta_data.to_json } }
12
+ describe "#meta_data" do
13
+ it "parses the metadata json" do
14
+ expect(subject.meta_data).to eq(meta_data)
15
+ end
16
+ end
17
+ describe "#browser" do
18
+ it "returns the browser from the meta data" do
19
+ subject.browser.should eq("firefox")
20
+ end
21
+ end
22
+ describe "#page_url" do
23
+ it "returns the page_url from the meta data" do
24
+ subject.page_url.should eq("www.example.com")
25
+ end
26
+ end
27
+ end
@@ -1,7 +1,7 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe DomReactorRedGlass::ChainReaction do
4
- let(:chain_reaction) { DomReactorRedGlass::ChainReaction.new('http://domreactor.com', {}) }
4
+ let(:chain_reaction) { DomReactorRedGlass::ChainReaction.new({}) }
5
5
  before do
6
6
  #stub chain reaction creation call
7
7
  RestClient.stub(:post) { {chain_reaction: {id: 42, some_info: 'yay'}}.to_json }
@@ -26,7 +26,7 @@ describe DomReactorRedGlass::ChainReaction do
26
26
  before do
27
27
  chain_reaction.stub(:create_payload)
28
28
  chain_reaction.stub(:create_dom_gun_reaction)
29
- chain_reaction.post_archives("#{SPEC_ROOT}/data/valid_archive")
29
+ chain_reaction.post_archives(ArchiveLocation.new("#{SPEC_ROOT}/data/valid_archive"))
30
30
  end
31
31
  it 'creates the payload' do
32
32
  expect(chain_reaction).to have_received(:create_payload).twice
@@ -3,33 +3,6 @@ require 'spec_helper'
3
3
  include DomReactorRedGlass
4
4
 
5
5
  describe DomReactorRedGlass do
6
- describe '.detect_archive_location' do
7
- it 'requires a valid archive location' do
8
- expect { DomReactorRedGlass.detect_archive_location('/does_not_exist') }
9
- .to raise_error('A valid archive location is required.')
10
- end
11
- end
12
- describe '.detect_min_archive_quota' do
13
- it 'requires at last two valid page archives' do
14
- expect { DomReactorRedGlass.detect_min_archive_quota("#{SPEC_ROOT}/data/invalid_archive_by_quota") }
15
- .to raise_error('At least two valid page archives are required.')
16
- end
17
- end
18
- describe '.detect_baseline_browser' do
19
- it 'requires a baseline_browser config' do
20
- expect { DomReactorRedGlass.detect_baseline_browser('', {}) }
21
- .to raise_error('A valid baseline_browser configuration is required.')
22
- end
23
- it 'requires a valid baseline_browser config' do
24
- expect { DomReactorRedGlass.detect_baseline_browser('', {baseline_browser: {name: '', version: ''}}) }
25
- .to raise_error('A valid baseline_browser configuration is required.')
26
- end
27
- it 'requires a corresponding page archive' do
28
- expect { DomReactorRedGlass.detect_baseline_browser("#{SPEC_ROOT}/data/valid_archive", {
29
- baseline_browser: {name: 'mosaic', version: '1.0', platform: 'darwin'}}) }
30
- .to raise_error('A page archive that corresponds to the baseline browser configuration is required.')
31
- end
32
- end
33
6
  describe '.auth_token' do
34
7
  it "sets the auth token" do
35
8
  DomReactorRedGlass.auth_token = 'abc123'
@@ -39,7 +12,7 @@ describe DomReactorRedGlass do
39
12
 
40
13
  describe '.create_chain_reaction' do
41
14
  it 'requires a valid archive location' do
42
- expect { DomReactorRedGlass.create_chain_reaction('some_url', '/does_not_exist', {})}
15
+ expect { DomReactorRedGlass.create_chain_reaction('/does_not_exist', {})}
43
16
  .to raise_error('A valid archive location is required.')
44
17
  end
45
18
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: domreactor-redglass
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2013-10-31 00:00:00.000000000 Z
13
+ date: 2013-11-14 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rest-client
@@ -149,6 +149,7 @@ extensions: []
149
149
  extra_rdoc_files: []
150
150
  files:
151
151
  - .gitignore
152
+ - .ruby-version
152
153
  - Gemfile
153
154
  - LICENSE
154
155
  - README.md
@@ -156,9 +157,13 @@ files:
156
157
  - domreactor-redglass.gemspec
157
158
  - example/domreactor_example.rb
158
159
  - lib/domreactor-redglass.rb
160
+ - lib/domreactor-redglass/archive.rb
161
+ - lib/domreactor-redglass/archive_location.rb
159
162
  - lib/domreactor-redglass/chain_reaction.rb
160
163
  - lib/domreactor-redglass/config.rb
161
164
  - lib/domreactor-redglass/version.rb
165
+ - spec/archive_location_spec.rb
166
+ - spec/archive_spec.rb
162
167
  - spec/chain_reaction_spec.rb
163
168
  - spec/data/invalid_archive_by_quota/firefox_20.0_1369614643/dom.json
164
169
  - spec/data/invalid_archive_by_quota/firefox_20.0_1369614643/metadata.json
@@ -195,11 +200,13 @@ required_rubygems_version: !ruby/object:Gem::Requirement
195
200
  version: '0'
196
201
  requirements: []
197
202
  rubyforge_project:
198
- rubygems_version: 1.8.24
203
+ rubygems_version: 1.8.23
199
204
  signing_key:
200
205
  specification_version: 3
201
206
  summary: DomReactor plugin for RedGlass.
202
207
  test_files:
208
+ - spec/archive_location_spec.rb
209
+ - spec/archive_spec.rb
203
210
  - spec/chain_reaction_spec.rb
204
211
  - spec/data/invalid_archive_by_quota/firefox_20.0_1369614643/dom.json
205
212
  - spec/data/invalid_archive_by_quota/firefox_20.0_1369614643/metadata.json