masque 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
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 masque.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 uu59
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,54 @@
1
+ # Masque
2
+
3
+ <a href="http://ja.wikipedia.org/wiki/%E3%83%95%E3%82%A1%E3%82%A4%E3%83%AB:Noh_mask02.jpg"><img src="http://upload.wikimedia.org/wikipedia/commons/b/bd/Noh_mask02.jpg" alt="女面" /></a>
4
+
5
+ Masque is a browser emulated crawler builder wrapping [capybara-webkit](https://github.com/thoughtbot/capybara-webkit) and [poltergeist](https://github.com/jonleighton/poltergeist).
6
+
7
+ ## Installation
8
+
9
+ You need Xvfb and QT libraries before installing Masque gem.
10
+
11
+ - <https://github.com/leonid-shevtsov/headless#installation>
12
+ - <https://github.com/thoughtbot/capybara-webkit/wiki/Installing-Qt-and-compiling-capybara-webkit>
13
+
14
+ On Debian/Ubuntu:
15
+
16
+ $ sudo apt-get install libqt4-dev xvfb libicu48 ttf-ubuntu-font-family
17
+ $ gem install masque
18
+
19
+ ## Usage
20
+
21
+ Easy crawling websites they required JavaScript.
22
+
23
+ require "masque"
24
+ m = Masque.new(:display => 99)
25
+ m.run do
26
+ # Capybara::DSL syntax
27
+ # https://github.com/jnicklas/capybara#the-dsl
28
+
29
+ visit "http://www.google.com/"
30
+ fill_in("q", :with => "capybara")
31
+ find('*[name="btnG"]').click
32
+
33
+ titles = evaluate_script <<-JS
34
+ (function(){
35
+ var titles = Array.prototype.map.call(
36
+ document.querySelectorAll('h3 a'),
37
+ function(a) {
38
+ return a.innerText;
39
+ }
40
+ );
41
+
42
+ return titles;
43
+ })();
44
+ JS
45
+ puts titles.join("\n")
46
+ end
47
+
48
+ ## Contributing
49
+
50
+ 1. Fork it
51
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
52
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
53
+ 4. Push to the branch (`git push origin my-new-feature`)
54
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/lib/masque.rb ADDED
@@ -0,0 +1,49 @@
1
+ require "capybara"
2
+ require "capybara/dsl"
3
+ require "capybara/webkit"
4
+ require 'capybara/poltergeist'
5
+ require "headless"
6
+ require "masque/version"
7
+ require "masque/dsl"
8
+
9
+ class Masque
10
+ attr_reader :options, :agent
11
+
12
+ def self.run(options = {}, &block)
13
+ new(options).run(&block)
14
+ end
15
+
16
+ def initialize(options = {})
17
+ @options = options
18
+ @agent = Class.new do
19
+ include Capybara::DSL
20
+ include Masque::DSL
21
+ end.new
22
+ end
23
+
24
+ def reset_session!
25
+ run do
26
+ driver = page.driver
27
+ driver.reset!
28
+ end
29
+ end
30
+
31
+ def run(&block)
32
+ case options[:driver]
33
+ when :poltergeist
34
+ Capybara.using_driver(:poltergeist) do
35
+ @agent.instance_eval(&block)
36
+ end
37
+
38
+ when :webkit, nil
39
+ h = Headless.new(options)
40
+ h.start
41
+ ObjectSpace.define_finalizer(@agent) do
42
+ h.destroy
43
+ end
44
+ Capybara.using_driver(:webkit) do
45
+ @agent.instance_eval(&block)
46
+ end
47
+ end
48
+ end
49
+ end
data/lib/masque/dsl.rb ADDED
@@ -0,0 +1,10 @@
1
+ # -- coding: utf-8
2
+
3
+ class Masque
4
+ module DSL
5
+ def save_screenshot(path)
6
+ page.driver.render(path)
7
+ end
8
+ alias :render :save_screenshot
9
+ end
10
+ end
@@ -0,0 +1,3 @@
1
+ class Masque
2
+ VERSION = "0.0.1"
3
+ end
data/masque.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'masque/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "masque"
8
+ gem.version = Masque::VERSION
9
+ gem.authors = ["uu59"]
10
+ gem.email = ["k@uu59.org"]
11
+ gem.description = %q{JavaScript enabled web crawler kit}
12
+ gem.summary = %q{JavaScript enabled web crawler kit}
13
+ gem.homepage = ""
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+ gem.add_dependency "capybara", "~> 1.1"
20
+ gem.add_dependency "capybara-webkit", "~> 0.12"
21
+ gem.add_dependency "poltergeist", "~> 1.0"
22
+ gem.add_development_dependency "rspec", "~> 2.11"
23
+ gem.add_development_dependency "sinatra"
24
+ gem.add_development_dependency "simplecov"
25
+ end
@@ -0,0 +1,69 @@
1
+ # -- coding: utf-8
2
+
3
+ require "spec_helper"
4
+
5
+ describe Masque do
6
+ [:webkit, :poltergeist].each do |driver|
7
+ context "Using #{driver} driver" do
8
+ let(:driver) { driver }
9
+
10
+ it ".new" do
11
+ opts = {:display => 50, :driver => driver}
12
+ masque = Masque.new(opts)
13
+ masque.options.should == opts
14
+ masque.respond_to?(:run).should be_true
15
+ end
16
+
17
+ describe ".run" do
18
+ it "Capybara::DSL ready" do
19
+ mods = []
20
+ body, title = nil
21
+ Masque.run(:driver => driver) do
22
+ mods = self.class.included_modules
23
+ visit "/#{driver}"
24
+ title = evaluate_script "document.title"
25
+ end
26
+ mods.include?(Capybara::DSL).should be_true
27
+ title[driver.to_s].should_not be_nil
28
+ end
29
+
30
+ it "#save_screenshot" do
31
+ path = "spec/tmp.png"
32
+ Masque.run(:display => 33, :driver => driver) do
33
+ visit "/"
34
+ save_screenshot(path)
35
+ end
36
+ File.file?(path).should be_true
37
+ File.size(path).should be > 0
38
+ File.unlink(path)
39
+ end
40
+ end
41
+
42
+ it "#reset_session!" do
43
+ masque = Masque.new(:driver => driver)
44
+ masque.reset_session!
45
+ first, second, third = nil
46
+
47
+ masque.run do
48
+ visit "/"
49
+ first = find('h1').text.to_i
50
+ end
51
+ masque.run do
52
+ visit "/"
53
+ second = find('h1').text.to_i
54
+ end
55
+
56
+ masque.reset_session!
57
+ masque.run do
58
+ visit "/"
59
+ third = find('h1').text.to_i
60
+ end
61
+
62
+ first.should == 1
63
+ second.should == 2
64
+ third.should == 1
65
+ end
66
+ end
67
+ end
68
+ end
69
+
@@ -0,0 +1,19 @@
1
+ # -- coding: utf-8
2
+
3
+ require "pp"
4
+ require "rubygems"
5
+ require "rspec-expectations"
6
+ require "rspec/matchers/built_in/be"
7
+ require File.expand_path("../support/app.rb", __FILE__)
8
+
9
+ if ENV["COVERAGE"]
10
+ require "simplecov"
11
+ SimpleCov.start
12
+ end
13
+
14
+ require File.expand_path("../../lib/masque.rb", __FILE__)
15
+
16
+
17
+ RSpec.configure do |config|
18
+ Capybara.app = Dummy
19
+ end
@@ -0,0 +1,24 @@
1
+ # -- coding: utf-8
2
+
3
+ require "sinatra/base"
4
+
5
+ class Dummy < Sinatra::Base
6
+ disable :logging
7
+ enable :sessions
8
+
9
+ get "/:arg?" do
10
+ session[:visit] ||= 0
11
+ session[:visit] += 1
12
+ <<-HTML
13
+ <style>
14
+ body {
15
+ background: #fff;
16
+ color: #222;
17
+ }
18
+ </style>
19
+ <title>hi #{params[:arg]}</title>
20
+ <h1 id="visits">#{session[:visit]}</h1>
21
+ <p>Hello, World!</p>
22
+ HTML
23
+ end
24
+ end
metadata ADDED
@@ -0,0 +1,157 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: masque
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - uu59
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-10-19 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: capybara
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.1'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.1'
30
+ - !ruby/object:Gem::Dependency
31
+ name: capybara-webkit
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '0.12'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '0.12'
46
+ - !ruby/object:Gem::Dependency
47
+ name: poltergeist
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: '1.0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '1.0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: rspec
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: '2.11'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: '2.11'
78
+ - !ruby/object:Gem::Dependency
79
+ name: sinatra
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ - !ruby/object:Gem::Dependency
95
+ name: simplecov
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ description: JavaScript enabled web crawler kit
111
+ email:
112
+ - k@uu59.org
113
+ executables: []
114
+ extensions: []
115
+ extra_rdoc_files: []
116
+ files:
117
+ - .gitignore
118
+ - Gemfile
119
+ - LICENSE.txt
120
+ - README.md
121
+ - Rakefile
122
+ - lib/masque.rb
123
+ - lib/masque/dsl.rb
124
+ - lib/masque/version.rb
125
+ - masque.gemspec
126
+ - spec/masque_spec.rb
127
+ - spec/spec_helper.rb
128
+ - spec/support/app.rb
129
+ homepage: ''
130
+ licenses: []
131
+ post_install_message:
132
+ rdoc_options: []
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
+ version: '0'
141
+ required_rubygems_version: !ruby/object:Gem::Requirement
142
+ none: false
143
+ requirements:
144
+ - - ! '>='
145
+ - !ruby/object:Gem::Version
146
+ version: '0'
147
+ requirements: []
148
+ rubyforge_project:
149
+ rubygems_version: 1.8.23
150
+ signing_key:
151
+ specification_version: 3
152
+ summary: JavaScript enabled web crawler kit
153
+ test_files:
154
+ - spec/masque_spec.rb
155
+ - spec/spec_helper.rb
156
+ - spec/support/app.rb
157
+ has_rdoc: