rasti-web 0.0.4 → 0.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/rasti/web/controller.rb +10 -11
- data/lib/rasti/web/route.rb +12 -13
- data/lib/rasti/web/router.rb +1 -1
- data/lib/rasti/web/version.rb +1 -1
- data/spec/route_spec.rb +41 -45
- data/spec/streaming_spec.rb +10 -4
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 764cf867ae8950a2b647667bd7ed1b3a9170abd8
|
4
|
+
data.tar.gz: 5d96989d2b5e261bc162c2076133016991f6a4e1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4aa9e4e8a7cfd3453ba4358ad8cd8f5e6c0856886b4f689215fb87c7a374a5b4350ee8c5ce87702e9e22be0d11e45e4fe081200aa0de709a042823ddb0cd51fd
|
7
|
+
data.tar.gz: 5d867c009e657e111605a24e7dd3d3b9a564e5962d3d890d69c3ef5796d31fbd077bc8d3919babba0230d1c3b3acd6267d8895007de6196458913a188152f6ca
|
data/lib/rasti/web/controller.rb
CHANGED
@@ -15,22 +15,21 @@ module Rasti
|
|
15
15
|
@render = render
|
16
16
|
end
|
17
17
|
|
18
|
-
def execute(action_name)
|
19
|
-
public_send action_name
|
20
|
-
rescue => ex
|
21
|
-
if respond_to? ex.class.name
|
22
|
-
public_send ex.class.name, ex
|
23
|
-
else
|
24
|
-
raise ex
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
18
|
class << self
|
29
19
|
def action(action_name)
|
30
20
|
raise "Undefined action '#{action_name}' in #{name}" unless instance_methods.include? action_name.to_sym
|
31
21
|
|
32
22
|
Endpoint.new do |req, res, render|
|
33
|
-
new
|
23
|
+
controller = new req, res, render
|
24
|
+
begin
|
25
|
+
controller.public_send action_name
|
26
|
+
rescue => ex
|
27
|
+
if controller.respond_to? ex.class.name
|
28
|
+
controller.public_send ex.class.name, ex
|
29
|
+
else
|
30
|
+
raise ex
|
31
|
+
end
|
32
|
+
end
|
34
33
|
end
|
35
34
|
end
|
36
35
|
|
data/lib/rasti/web/route.rb
CHANGED
@@ -2,32 +2,31 @@ module Rasti
|
|
2
2
|
module Web
|
3
3
|
class Route
|
4
4
|
|
5
|
-
attr_reader :pattern
|
6
|
-
|
5
|
+
attr_reader :pattern
|
6
|
+
|
7
7
|
def initialize(pattern, endpoint=nil, &block)
|
8
8
|
@pattern = normalize pattern
|
9
9
|
@endpoint = endpoint || Endpoint.new(&block)
|
10
|
-
compile
|
10
|
+
@regexp = compile
|
11
11
|
end
|
12
12
|
|
13
13
|
def match?(path)
|
14
|
-
|
14
|
+
!@regexp.match(normalize(path)).nil?
|
15
15
|
end
|
16
16
|
|
17
17
|
def extract_params(path)
|
18
|
-
result = regexp.match path
|
19
|
-
result ?
|
18
|
+
result = @regexp.match path
|
19
|
+
result ? result.names.each_with_object({}) { |v,h| h[v] = result[v] } : {}
|
20
|
+
end
|
21
|
+
|
22
|
+
def call(env)
|
23
|
+
@endpoint.call env
|
20
24
|
end
|
21
25
|
|
22
|
-
private
|
26
|
+
private
|
23
27
|
|
24
28
|
def compile
|
25
|
-
|
26
|
-
regexp = pattern.gsub(/(:\w+)/) do |match|
|
27
|
-
@params << match[1..-1]
|
28
|
-
"([^/?#]+)"
|
29
|
-
end
|
30
|
-
@regexp = %r{^#{regexp}$}
|
29
|
+
%r{^#{pattern.gsub(')', '){0,1}').gsub(/:[a-z0-9_-]+/) { |var| "(?<#{var[1..-1]}>[^\/?#]+)" }}$}
|
31
30
|
end
|
32
31
|
|
33
32
|
def normalize(path)
|
data/lib/rasti/web/router.rb
CHANGED
data/lib/rasti/web/version.rb
CHANGED
data/spec/route_spec.rb
CHANGED
@@ -2,66 +2,62 @@ require 'minitest_helper'
|
|
2
2
|
|
3
3
|
describe Rasti::Web::Route do
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
|
5
|
+
ROUTES = [
|
6
|
+
'/',
|
7
|
+
'/resource',
|
8
|
+
'/resource/:id/:action',
|
9
|
+
'/:resource(/:id(/:action))'
|
10
|
+
]
|
8
11
|
|
9
|
-
|
10
|
-
route = build_route '/resource'
|
12
|
+
RESPONSE = [200, {}, []]
|
11
13
|
|
12
|
-
|
13
|
-
|
14
|
-
route.endpoint.must_equal :fake_endpoint
|
15
|
-
route.params.must_equal []
|
14
|
+
def build_route(pattern)
|
15
|
+
Rasti::Web::Route.new pattern, ->(env) { RESPONSE }
|
16
16
|
end
|
17
17
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
route.pattern.must_equal '/resource/:id/:action'
|
22
|
-
route.regexp.to_s.must_equal /^\/resource\/([^\/?#]+)\/([^\/?#]+)$/.to_s
|
23
|
-
route.endpoint.must_equal :fake_endpoint
|
24
|
-
route.params.must_equal %w(id action)
|
18
|
+
def route_for(path)
|
19
|
+
ROUTES.map { |r| build_route r }
|
20
|
+
.detect { |r| r.match? path }
|
25
21
|
end
|
26
22
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
build_route('/resource').must_be :match?, '/resource'
|
32
|
-
build_route('/resource').must_be :match?, '/resource/'
|
33
|
-
build_route('/resource').wont_be :match?, '/'
|
23
|
+
['', '/'].each do |path|
|
24
|
+
it "Root '#{path}'" do
|
25
|
+
route = route_for path
|
34
26
|
|
35
|
-
|
36
|
-
|
27
|
+
route.pattern.must_equal '/'
|
28
|
+
route.extract_params(path).must_be_empty
|
29
|
+
route.call({}).must_equal RESPONSE
|
30
|
+
end
|
37
31
|
end
|
38
32
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
end
|
33
|
+
['/resource', '/resource/'].each do |path|
|
34
|
+
it "Static '#{path}'" do
|
35
|
+
route = route_for path
|
43
36
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
build_route('/resource/').pattern.must_equal '/resource'
|
37
|
+
route.pattern.must_equal '/resource'
|
38
|
+
route.extract_params(path).must_be_empty
|
39
|
+
route.call({}).must_equal RESPONSE
|
40
|
+
end
|
49
41
|
end
|
50
42
|
|
51
|
-
it '
|
52
|
-
|
53
|
-
|
54
|
-
res['Content-Type'] = 'text/html'
|
55
|
-
res.write '<h1>Not found</h1>'
|
56
|
-
end
|
43
|
+
it 'Params' do
|
44
|
+
path = '/resource/123/show'
|
45
|
+
route = route_for path
|
57
46
|
|
58
|
-
route.
|
47
|
+
route.pattern.must_equal '/resource/:id/:action'
|
48
|
+
route.extract_params(path).must_equal 'id' => '123', 'action' => 'show'
|
49
|
+
route.call({}).must_equal RESPONSE
|
50
|
+
end
|
59
51
|
|
60
|
-
|
52
|
+
['/other', '/other/456', '/other/456/edit'].each do |path|
|
53
|
+
it "Optional params '#{path}'" do
|
54
|
+
route = route_for path
|
55
|
+
sections = path[1..-1].split('/')
|
61
56
|
|
62
|
-
|
63
|
-
|
64
|
-
|
57
|
+
route.pattern.must_equal '/:resource(/:id(/:action))'
|
58
|
+
route.extract_params(path).must_equal 'resource' => sections[0], 'id' => sections[1], 'action' => sections[2]
|
59
|
+
route.call({}).must_equal RESPONSE
|
60
|
+
end
|
65
61
|
end
|
66
62
|
|
67
63
|
end
|
data/spec/streaming_spec.rb
CHANGED
@@ -7,11 +7,17 @@ describe 'Straming' do
|
|
7
7
|
let(:render) { Rasti::Web::Render.new request, response }
|
8
8
|
|
9
9
|
it 'Server sent events' do
|
10
|
-
|
11
|
-
|
10
|
+
render.server_sent_events :test_channel
|
11
|
+
|
12
|
+
response.status.must_equal 200
|
13
|
+
response['Content-Type'].must_equal 'text/event-stream'
|
14
|
+
response['Cache-Control'].must_equal 'no-cache'
|
15
|
+
response['Connection'].must_equal 'keep-alive'
|
16
|
+
response.body.must_be_instance_of Rasti::Web::Stream
|
12
17
|
|
18
|
+
events = []
|
13
19
|
thread = Thread.new do
|
14
|
-
|
20
|
+
response.body.each { |e| events << e }
|
15
21
|
end
|
16
22
|
|
17
23
|
3.times do |i|
|
@@ -21,7 +27,7 @@ describe 'Straming' do
|
|
21
27
|
end
|
22
28
|
|
23
29
|
while events.count < 3; sleep 0.0001 end
|
24
|
-
|
30
|
+
response.body.close
|
25
31
|
|
26
32
|
events.must_equal 3.times.map { |i| "id: #{i}\nevent: tick\ndata: {\"text\":\"Tick #{i}\"}\n\n" }
|
27
33
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rasti-web
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Gabriel Naiman
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-11-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rack
|
@@ -234,7 +234,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
234
234
|
version: '0'
|
235
235
|
requirements: []
|
236
236
|
rubyforge_project:
|
237
|
-
rubygems_version: 2.4.
|
237
|
+
rubygems_version: 2.4.3
|
238
238
|
signing_key:
|
239
239
|
specification_version: 4
|
240
240
|
summary: Web blocks to build robust applications
|