masque 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -21,7 +21,7 @@ On Debian/Ubuntu:
21
21
  Easy crawling websites they required JavaScript.
22
22
 
23
23
  require "masque"
24
- m = Masque.new(:display => 99)
24
+ m = Masque.new(:display => 99, :driver => :webkit) # or :driver => :poltergeist
25
25
  m.run do
26
26
  # Capybara::DSL syntax
27
27
  # https://github.com/jnicklas/capybara#the-dsl
@@ -45,6 +45,15 @@ Easy crawling websites they required JavaScript.
45
45
  puts titles.join("\n")
46
46
  end
47
47
 
48
+ more examples are available in examples/ directory.
49
+
50
+ ## TODO
51
+
52
+ - Masque instances should have own Capybara session
53
+ - Impl useful DSL
54
+ - Respect driver specific features/options
55
+
56
+
48
57
  ## Contributing
49
58
 
50
59
  1. Fork it
@@ -0,0 +1,43 @@
1
+ # -- coding: utf-8
2
+
3
+ =begin
4
+ Usage: ./text_from_image.rb [ImageURL]
5
+
6
+ $ ruby ./text_from_image.rb "https://assets.github.com/images/icons/emoji/trollface.png"
7
+
8
+ will put as below:
9
+
10
+ http://www.google.com/searchbyimage?filter=0&num=100&safe=off&image_url=https%3A%2F%2Fassets.github.com%2Fimages%2Ficons%2Femoji%2Ftrollface.png
11
+ http://www.google.com/search?tbs=sbi:AMhZZiv_1UzO9L3jB0q1kxd1EWjYqzQAYTqDj2995tv6uOgUgbjblvKBcb5tpLrNVO_1sFMhvYv2-q97rqK1-PBFyL8AhiskxGtR12nPu_1ei2H4IcoaAwd76Z-TRvjx8zD_1YWwFMJwGdhGnmV1pzXpqd68JK9wypWDgXhk3mhDzyNCBhQb6ahCzCzZIW5CefUKV8gv43Se4XuHXuxYB3_1cXkZkV5DMdEurDFVEs1R_1H2J6xVg-10g8tglowPxoaML866mnpy5bH3REOidP4QUN34pKIztahEYpJYCQN_1Y2ffXhp19HljhELwl_1Q2_1MuD_1XWGS4jFkyXoj6fyX0P9f3hH5mwW_1mKsRc3IgP0UMJ1sB8gZvngr-q4ZnhKbRDJHXj3nDhWUKg1kueVecwTC59nTENiY4uGh9rCh-sKMikcouRh8Fpn8pE4RT-JDC833St3w6Rq13K-HcaRXb2wK7mgSW25607IbfMqNJRAj_1wBkURIHabrZUEN3pUySYZClkxzXnqVW9BfdKG0dvhqxSRaRLiZcC7OX8NW6H7SwUvEGazZg8aFbQSfchKLVEbWt4LmwFordrVDna4p6t3-qnZmqYsvCjJC418xEaF19Pzdt6xkskmacGyEvg-4P1S0YbFd5VJ6RdTtktn5JJrjj0IzRyWbfOvrm-l9-S7aukOK93vanf3MnJDE_1spr372aH0yK5-tJ68h3R0lgXFmqb0q6mvTomq4JMMMZgQnD-qSy_1etIZ9EkRsX62qHyG_17OBNcOl44YYHx8hGRHLuWW4PCxMlogPT7HOpAHG2OS1pAH7iGaWSujlk6ZZCP1ajXniZhGy7XI5BhcdkzCO7FIMjg75IDLbDHWcW79i31Dt8FtUJMWQkc2tshL2CZSF12AazE2fp-a7wQp7T8AzseiGpC2xVBXZtGZ5tQJoKMdw_1j-f3PJHbAe-64HL7NikFOIXiYVsQTcz2a018SxAiG_1LeVtxPyuUTdj6BziRb4uhPS5Tnv4SGEbRR9pMhBq_149OD_1iPyUeCM0HEt-wOkuBk0-lpr33QYAVHue41Rh6kJrb-pHvkVG32WP2XNJxuPW_1sPqETwJ1mFNJ-SIrDow-zPlB13C7Y8ZC-IWF-hX6WmyeGnYG7Fg2LwIQp2FCvWkt-W9Q9BGuy0vNWGyqFVZrhWM_1u-lUxBg0nIPawtqk2EBPdUHf4iSC1pVcVVUi7CFc_1BTkwBeGlkyU1Iylt7PTbfo4J5q0AUJyD8LyrPeTXknMGsy8KioKJwDFhYQGd42gHo2xo28dmhQqulLycBqQozBZ5uTsne2z1dkNUdJlC6PdrXpkeFyGEorvkmRCb3cXQge6yTAfOew45cD-&filter=0&num=100&safe=off
12
+ ["trollface face photos - Photobucket",
13
+ "trolling face photos - Photobucket",
14
+ "Amazon.com: Troll Face Meme Face Large Bumper Sticker Decal ...",
15
+ "trollface Cancel - TinyPic",
16
+ "Amazon.com: Troll Face Meme Face Small Bumper Sticker Decal ..."]
17
+
18
+ =end
19
+
20
+ require File.expand_path("../../lib/masque.rb", __FILE__)
21
+ require "pp"
22
+
23
+ imageurl = ARGV.first || "https://assets.github.com/images/icons/emoji/trollface.png"
24
+ googleurl = "http://www.google.com/searchbyimage?filter=0&num=100&safe=off&image_url=#{URI.encode_www_form_component(imageurl)}"
25
+
26
+ anchors = Masque.run(:driver => :webkit) do
27
+ page.driver.header "User-Agent", "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.0; Trident/5.0)"
28
+ page.driver.header "Accept-Language", "en-US;"
29
+
30
+ puts googleurl
31
+ visit googleurl
32
+ puts current_url
33
+ evaluate_script <<-JS
34
+ (function(){
35
+ var links = document.querySelectorAll('td h3 a');
36
+ return Array.prototype.map.call(links, function(a){
37
+ return a.textContent || "";
38
+ });
39
+ })();
40
+ JS
41
+ end
42
+
43
+ pp anchors
data/lib/masque.rb CHANGED
@@ -15,35 +15,41 @@ class Masque
15
15
 
16
16
  def initialize(options = {})
17
17
  @options = options
18
+ @driver = options[:driver] || :webkit
18
19
  @agent = Class.new do
19
- include Capybara::DSL
20
20
  include Masque::DSL
21
- end.new
21
+
22
+ attr_accessor :session
23
+
24
+ def initialize(session)
25
+ @session = session
26
+ end
27
+
28
+ def page
29
+ session
30
+ end
31
+
32
+ def method_missing(*args)
33
+ session.__send__(*args)
34
+ end
35
+ end.new(Capybara::Session.new(@driver))
36
+
37
+ if @driver == :webkit
38
+ h = Headless.new(options.merge(:destroy_at_exit => false, :reuse => true))
39
+ h.start
40
+ ObjectSpace.define_finalizer(@agent.driver.browser.instance_variable_get(:@connection)) do
41
+ h.destroy
42
+ end
43
+ end
22
44
  end
23
45
 
24
46
  def reset_session!
25
47
  run do
26
- driver = page.driver
27
48
  driver.reset!
28
49
  end
29
50
  end
30
51
 
31
52
  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
53
+ @agent.instance_eval(&block)
48
54
  end
49
55
  end
@@ -1,3 +1,3 @@
1
1
  class Masque
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/spec/masque_spec.rb CHANGED
@@ -6,6 +6,16 @@ describe Masque do
6
6
  [:webkit, :poltergeist].each do |driver|
7
7
  context "Using #{driver} driver" do
8
8
  let(:driver) { driver }
9
+ let!(:masque) do
10
+ m = Masque.new(:driver => driver)
11
+ m.agent.session = Capybara::Session.new(driver, Dummy)
12
+ m
13
+ end
14
+ after do
15
+ masque.instance_eval do
16
+ @agent = nil
17
+ end
18
+ end
9
19
 
10
20
  it ".new" do
11
21
  opts = {:display => 50, :driver => driver}
@@ -17,31 +27,36 @@ describe Masque do
17
27
  describe ".run" do
18
28
  it "Capybara::DSL ready" do
19
29
  mods = []
20
- body, title = nil
21
- Masque.run(:driver => driver) do
30
+ title = nil
31
+ masque.run do
22
32
  mods = self.class.included_modules
23
- visit "/#{driver}"
33
+ visit "/dummy"
24
34
  title = evaluate_script "document.title"
25
35
  end
26
- mods.include?(Capybara::DSL).should be_true
27
- title[driver.to_s].should_not be_nil
36
+ title["dummy"].should_not be_nil
28
37
  end
29
38
 
30
39
  it "#save_screenshot" do
31
40
  path = "spec/tmp.png"
32
- Masque.run(:display => 33, :driver => driver) do
41
+ masque.run do
33
42
  visit "/"
34
- save_screenshot(path)
43
+ render(path)
35
44
  end
36
45
  File.file?(path).should be_true
37
46
  File.size(path).should be > 0
38
47
  File.unlink(path)
39
48
  end
49
+
50
+ it "return last evaluated value" do
51
+ body = masque.run do
52
+ visit "/"
53
+ page.body
54
+ end
55
+ body.should_not be_nil
56
+ end
40
57
  end
41
58
 
42
59
  it "#reset_session!" do
43
- masque = Masque.new(:driver => driver)
44
- masque.reset_session!
45
60
  first, second, third = nil
46
61
 
47
62
  masque.run do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: masque
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
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-10-19 00:00:00.000000000 Z
12
+ date: 2012-10-20 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: capybara
@@ -119,6 +119,7 @@ files:
119
119
  - LICENSE.txt
120
120
  - README.md
121
121
  - Rakefile
122
+ - examples/text_from_image.rb
122
123
  - lib/masque.rb
123
124
  - lib/masque/dsl.rb
124
125
  - lib/masque/version.rb