spider-gazelle 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/bin/sg +46 -17
- data/lib/spider-gazelle.rb +5 -0
- data/lib/spider-gazelle/app_store.rb +67 -0
- data/lib/spider-gazelle/binding.rb +72 -0
- data/lib/spider-gazelle/connection.rb +255 -25
- data/lib/spider-gazelle/gazelle.rb +52 -30
- data/lib/spider-gazelle/request.rb +40 -40
- data/lib/spider-gazelle/spider.rb +292 -139
- data/lib/spider-gazelle/upgrades/websocket.rb +83 -32
- data/lib/spider-gazelle/version.rb +1 -1
- data/spider-gazelle.gemspec +2 -0
- metadata +32 -2
@@ -2,36 +2,87 @@ require 'websocket/driver'
|
|
2
2
|
|
3
3
|
|
4
4
|
module SpiderGazelle
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
5
|
+
# TODO:: make a promise that resolves when closed
|
6
|
+
class Websocket < ::Libuv::Q::DeferredPromise
|
7
|
+
attr_reader :env, :url, :driver, :socket
|
8
|
+
|
9
|
+
|
10
|
+
def initialize(tcp, env)
|
11
|
+
@socket, @env = tcp, env
|
12
|
+
|
13
|
+
# Initialise the promise
|
14
|
+
super(@socket.loop, @socket.loop.defer)
|
15
|
+
|
16
|
+
scheme = env[Request::RACK_URLSCHEME] == Request::HTTPS_URL_SCHEME ? 'wss://' : 'ws://'
|
17
|
+
@url = scheme + env[Request::HTTP_HOST] + env[Request::REQUEST_URI]
|
18
|
+
@driver = ::WebSocket::Driver.rack(self)
|
19
|
+
|
20
|
+
# Pass data from the socket to the driver
|
21
|
+
@socket.progress &method(:socket_read)
|
22
|
+
@socket.finally &method(:socket_close)
|
23
|
+
|
24
|
+
|
25
|
+
# Driver has indicated that it is closing
|
26
|
+
# We'll close the socket after writing any remaining data
|
27
|
+
@driver.on(:close, &method(:on_close))
|
28
|
+
@driver.on(:message, &method(:on_message))
|
29
|
+
@driver.on(:error, &method(:on_error))
|
30
|
+
end
|
31
|
+
|
32
|
+
def start
|
33
|
+
@driver.start
|
34
|
+
end
|
35
|
+
|
36
|
+
def text(string)
|
37
|
+
data = string.to_s
|
38
|
+
@loop.schedule do
|
39
|
+
@driver.text(data)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def binary(array)
|
44
|
+
data = array.to_a
|
45
|
+
@loop.schedule do
|
46
|
+
@driver.binary(data)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def progress(callback = nil, &blk)
|
51
|
+
@progress = callback || blk
|
52
|
+
end
|
53
|
+
|
54
|
+
|
55
|
+
def write(data)
|
56
|
+
@socket.write(data)
|
57
|
+
end
|
58
|
+
|
59
|
+
|
60
|
+
protected
|
61
|
+
|
62
|
+
|
63
|
+
def socket_read(data, tcp)
|
64
|
+
@driver.parse(data)
|
65
|
+
end
|
66
|
+
|
67
|
+
def socket_close
|
68
|
+
if @shutdown_called.nil?
|
69
|
+
@defer.reject(WebSocket::Driver::CloseEvent.new(1006, 'connection was closed unexpectedly'))
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
|
74
|
+
def on_message(event)
|
75
|
+
@progress.call(event.data, self) unless @progress.nil?
|
76
|
+
end
|
77
|
+
|
78
|
+
def on_error(event)
|
79
|
+
@defer.reject(event)
|
80
|
+
end
|
81
|
+
|
82
|
+
def on_close(event)
|
83
|
+
@shutdown_called = true
|
84
|
+
@socket.shutdown
|
85
|
+
@defer.resolve(event)
|
86
|
+
end
|
87
|
+
end
|
37
88
|
end
|
data/spider-gazelle.gemspec
CHANGED
@@ -21,6 +21,8 @@ Gem::Specification.new do |s|
|
|
21
21
|
s.add_dependency 'libuv' # Ruby FFI bindings for https://github.com/joyent/libuv
|
22
22
|
s.add_dependency 'rack', '>= 1.0.0' # Ruby web server interface
|
23
23
|
s.add_dependency 'websocket-driver' # Websocket parser
|
24
|
+
s.add_dependency 'thread_safe' # Thread safe hashes
|
25
|
+
s.add_dependency 'radix' # Converts numbers to the unicode representation
|
24
26
|
|
25
27
|
s.add_development_dependency 'rspec' # Testing framework
|
26
28
|
s.add_development_dependency 'yard' # Comment based documentation generation
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: spider-gazelle
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Stephen von Takach
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2014-01-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
@@ -80,6 +80,34 @@ dependencies:
|
|
80
80
|
- - ">="
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: thread_safe
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :runtime
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: radix
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :runtime
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
83
111
|
- !ruby/object:Gem::Dependency
|
84
112
|
name: rspec
|
85
113
|
requirement: !ruby/object:Gem::Requirement
|
@@ -121,6 +149,8 @@ extra_rdoc_files:
|
|
121
149
|
- README.md
|
122
150
|
files:
|
123
151
|
- lib/rack/handler/spider-gazelle.rb
|
152
|
+
- lib/spider-gazelle/app_store.rb
|
153
|
+
- lib/spider-gazelle/binding.rb
|
124
154
|
- lib/spider-gazelle/connection.rb
|
125
155
|
- lib/spider-gazelle/error.rb
|
126
156
|
- lib/spider-gazelle/gazelle.rb
|