phantom_menace 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +45 -0
- data/Rakefile +2 -0
- data/bin/jquery.js +8374 -0
- data/bin/phantom_menace +7 -0
- data/bin/phantom_menace.coffee +148 -0
- data/examples/simple.rb +8 -0
- data/lib/phantom_menace.rb +7 -0
- data/lib/phantom_menace/browser.rb +31 -0
- data/lib/phantom_menace/element.rb +13 -0
- data/lib/phantom_menace/version.rb +3 -0
- data/phantom_menace.gemspec +21 -0
- data/spec/browser_spec.rb +22 -0
- metadata +118 -0
data/bin/phantom_menace
ADDED
@@ -0,0 +1,148 @@
|
|
1
|
+
USER_AGENT = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) ' +
|
2
|
+
'AppleWebKit/A.B (KHTML, like Gecko) Chrome/X.Y.Z.W Safari/A.B.'
|
3
|
+
|
4
|
+
class Page
|
5
|
+
constructor: ->
|
6
|
+
@_loaded = false
|
7
|
+
@_page = require('webpage').create()
|
8
|
+
|
9
|
+
open: (url) ->
|
10
|
+
@_page.open url
|
11
|
+
|
12
|
+
isLoaded: ->
|
13
|
+
@_loaded
|
14
|
+
|
15
|
+
class Browser
|
16
|
+
constructor: ->
|
17
|
+
# This is the response container
|
18
|
+
# Always clean this up.
|
19
|
+
@command = ''
|
20
|
+
@resp = null
|
21
|
+
|
22
|
+
@history = []
|
23
|
+
@state = 'default'
|
24
|
+
this._initPage()
|
25
|
+
|
26
|
+
click: (left, top) ->
|
27
|
+
@page.sendEvent left, top
|
28
|
+
|
29
|
+
find: (selector) ->
|
30
|
+
console.log 'Finding it'
|
31
|
+
|
32
|
+
ret = @page.evaluate ->
|
33
|
+
elements = []
|
34
|
+
jQuery('a').each (i, obj) ->
|
35
|
+
attrs = {}
|
36
|
+
attrs[attr.name] = attr.value for attr in obj.attributes
|
37
|
+
|
38
|
+
elements.push
|
39
|
+
attrs: attrs
|
40
|
+
text: jQuery(obj).text()
|
41
|
+
pos: jQuery(obj).offset()
|
42
|
+
elements
|
43
|
+
|
44
|
+
@resp =
|
45
|
+
success: true
|
46
|
+
ret: ret
|
47
|
+
|
48
|
+
@state = 'default'
|
49
|
+
|
50
|
+
|
51
|
+
findAll: ->
|
52
|
+
console.log 'Finding all'
|
53
|
+
|
54
|
+
goforward: ->
|
55
|
+
console.log 'Going forward a page'
|
56
|
+
|
57
|
+
goto: (url) ->
|
58
|
+
@page.open decodeURIComponent(url)
|
59
|
+
|
60
|
+
goback: ->
|
61
|
+
this.goto @history.pop
|
62
|
+
|
63
|
+
getResp: ->
|
64
|
+
@resp
|
65
|
+
|
66
|
+
reload: ->
|
67
|
+
this.goto @history[@history.length - 1]
|
68
|
+
|
69
|
+
render: ->
|
70
|
+
@page.render()
|
71
|
+
|
72
|
+
content: ->
|
73
|
+
@page.content
|
74
|
+
|
75
|
+
reset: ->
|
76
|
+
@page.release()
|
77
|
+
this._initPage()
|
78
|
+
|
79
|
+
runCommand: (command, args...) ->
|
80
|
+
@state = 'loading'
|
81
|
+
@command = command
|
82
|
+
this[command].apply(this, args)
|
83
|
+
|
84
|
+
_initPage: ->
|
85
|
+
@page = require('webpage').create()
|
86
|
+
@page.settings.userAgent = USER_AGENT
|
87
|
+
@page.onLoadStarted = this._onLoadStarted
|
88
|
+
@page.onLoadFinished = this._onLoadFinished
|
89
|
+
@page.onConsoleMessage = this._onConsoleMessage
|
90
|
+
|
91
|
+
_onLoadStarted: =>
|
92
|
+
console.log 'Started loading'
|
93
|
+
@state = 'loading'
|
94
|
+
|
95
|
+
_onLoadFinished: (status) =>
|
96
|
+
console.log 'Finished loading'
|
97
|
+
@page.injectJs 'jquery.js'
|
98
|
+
|
99
|
+
@page.evaluate ->
|
100
|
+
console.log JSON.stringify($('a').eq(0).offset())
|
101
|
+
|
102
|
+
if @command is 'goto'
|
103
|
+
@resp =
|
104
|
+
success: true
|
105
|
+
command: "goto"
|
106
|
+
|
107
|
+
@state = 'default'
|
108
|
+
|
109
|
+
_onConsoleMessage: (msg) =>
|
110
|
+
console.log 'MSG: ' + msg
|
111
|
+
|
112
|
+
class Server
|
113
|
+
constructor: ->
|
114
|
+
@browser = new Browser()
|
115
|
+
@server = require('webserver').create()
|
116
|
+
|
117
|
+
run: ->
|
118
|
+
@server.listen 8080, this._handleRequest
|
119
|
+
|
120
|
+
_handleRequest: (req, res) =>
|
121
|
+
params = req.post
|
122
|
+
|
123
|
+
if @browser[params.command]
|
124
|
+
@browser.runCommand(params.command, params.data)
|
125
|
+
this._runLoop(req, res)
|
126
|
+
else
|
127
|
+
res.statusCode = 200
|
128
|
+
res.write JSON.stringify({
|
129
|
+
success: false
|
130
|
+
message: "Command not found"
|
131
|
+
})
|
132
|
+
res.close()
|
133
|
+
|
134
|
+
_runLoop: (req, res) =>
|
135
|
+
setTimeout =>
|
136
|
+
console.log 'THE STATE: ' + @browser.state
|
137
|
+
|
138
|
+
if @browser.state is 'loading'
|
139
|
+
this._runLoop(req, res)
|
140
|
+
else if @browser.state is 'default'
|
141
|
+
res.statusCode = 200
|
142
|
+
res.write JSON.stringify(@browser.getResp())
|
143
|
+
res.close()
|
144
|
+
, 1000
|
145
|
+
|
146
|
+
server = new Server()
|
147
|
+
server.run()
|
148
|
+
console.log '=> Server running at 8080'
|
data/examples/simple.rb
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
require "net/http"
|
2
|
+
require "uri"
|
3
|
+
require "json"
|
4
|
+
|
5
|
+
module PhantomMenace
|
6
|
+
class Browser
|
7
|
+
def goto(url)
|
8
|
+
options = {
|
9
|
+
command: "goto",
|
10
|
+
data: url
|
11
|
+
}
|
12
|
+
post(options)
|
13
|
+
end
|
14
|
+
|
15
|
+
def find_all_links
|
16
|
+
options = {
|
17
|
+
command: "find"
|
18
|
+
}
|
19
|
+
post(options)["ret"].map do |node|
|
20
|
+
PhantomMenace::Element.new(node)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def post(options)
|
27
|
+
req = Net::HTTP.post_form(URI.parse(URL), options)
|
28
|
+
JSON.parse(req.body)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/phantom_menace/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["William Estoque"]
|
6
|
+
gem.email = ["william.estoque@gmail.com"]
|
7
|
+
gem.description = %q{Ruby Wrapper for Phantomjs with a Browser API}
|
8
|
+
gem.summary = %q{Phantomjs based browser with a ruby wrapper}
|
9
|
+
gem.homepage = ""
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "phantom_menace"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = PhantomMenace::VERSION
|
17
|
+
|
18
|
+
gem.add_development_dependency "rspec"
|
19
|
+
gem.add_development_dependency "rake"
|
20
|
+
gem.add_development_dependency "phantomjs.rb"
|
21
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'phantom_menace'
|
2
|
+
|
3
|
+
describe PhantomMenace::Browser do
|
4
|
+
before do
|
5
|
+
@browser = PhantomMenace::Browser.new
|
6
|
+
end
|
7
|
+
|
8
|
+
describe "#goto" do
|
9
|
+
it "responds with success if page opens successfully" do
|
10
|
+
resp = @browser.goto "http://google.com"
|
11
|
+
resp["success"].should == true
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
describe "#find_all_links" do
|
16
|
+
it "responds with the links in the page" do
|
17
|
+
@browser.goto "http://google.com"
|
18
|
+
resp = @browser.find_all_links
|
19
|
+
resp.length.should_not be(0)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
metadata
ADDED
@@ -0,0 +1,118 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: phantom_menace
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- William Estoque
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-06-08 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rake
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: phantomjs.rb
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
description: Ruby Wrapper for Phantomjs with a Browser API
|
63
|
+
email:
|
64
|
+
- william.estoque@gmail.com
|
65
|
+
executables:
|
66
|
+
- jquery.js
|
67
|
+
- phantom_menace
|
68
|
+
- phantom_menace.coffee
|
69
|
+
extensions: []
|
70
|
+
extra_rdoc_files: []
|
71
|
+
files:
|
72
|
+
- .gitignore
|
73
|
+
- Gemfile
|
74
|
+
- LICENSE
|
75
|
+
- README.md
|
76
|
+
- Rakefile
|
77
|
+
- bin/jquery.js
|
78
|
+
- bin/phantom_menace
|
79
|
+
- bin/phantom_menace.coffee
|
80
|
+
- examples/simple.rb
|
81
|
+
- lib/phantom_menace.rb
|
82
|
+
- lib/phantom_menace/browser.rb
|
83
|
+
- lib/phantom_menace/element.rb
|
84
|
+
- lib/phantom_menace/version.rb
|
85
|
+
- phantom_menace.gemspec
|
86
|
+
- spec/browser_spec.rb
|
87
|
+
homepage: ''
|
88
|
+
licenses: []
|
89
|
+
post_install_message:
|
90
|
+
rdoc_options: []
|
91
|
+
require_paths:
|
92
|
+
- lib
|
93
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
94
|
+
none: false
|
95
|
+
requirements:
|
96
|
+
- - ! '>='
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
version: '0'
|
99
|
+
segments:
|
100
|
+
- 0
|
101
|
+
hash: -1323133816147866598
|
102
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
103
|
+
none: false
|
104
|
+
requirements:
|
105
|
+
- - ! '>='
|
106
|
+
- !ruby/object:Gem::Version
|
107
|
+
version: '0'
|
108
|
+
segments:
|
109
|
+
- 0
|
110
|
+
hash: -1323133816147866598
|
111
|
+
requirements: []
|
112
|
+
rubyforge_project:
|
113
|
+
rubygems_version: 1.8.24
|
114
|
+
signing_key:
|
115
|
+
specification_version: 3
|
116
|
+
summary: Phantomjs based browser with a ruby wrapper
|
117
|
+
test_files:
|
118
|
+
- spec/browser_spec.rb
|