rasti-web 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: fc7120ef16d98aaab088dc1d70dc5605cefce9bf
4
- data.tar.gz: eba0f9c11f7df1078c299e10c108b83885eff925
3
+ metadata.gz: 764cf867ae8950a2b647667bd7ed1b3a9170abd8
4
+ data.tar.gz: 5d96989d2b5e261bc162c2076133016991f6a4e1
5
5
  SHA512:
6
- metadata.gz: e0858476701c4d950f8827374183bed0cb69bbc341cf9755b5b0282a480770bcb8a6c4a81105ae5b4f435b9035a4d8ac53f01181fa2a237243b8a12e0cdeb390
7
- data.tar.gz: 03ea9bd24694ad5fa7e38b892d84d52e3b24bf32702133fbcef4414812eeafad52228cc8430ba77508b3fc82bdbb29b4f68c79e7e3ebd4913ac6bf0c84bddbf1
6
+ metadata.gz: 4aa9e4e8a7cfd3453ba4358ad8cd8f5e6c0856886b4f689215fb87c7a374a5b4350ee8c5ce87702e9e22be0d11e45e4fe081200aa0de709a042823ddb0cd51fd
7
+ data.tar.gz: 5d867c009e657e111605a24e7dd3d3b9a564e5962d3d890d69c3ef5796d31fbd077bc8d3919babba0230d1c3b3acd6267d8895007de6196458913a188152f6ca
@@ -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(req, res, render).execute(action_name)
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
 
@@ -2,32 +2,31 @@ module Rasti
2
2
  module Web
3
3
  class Route
4
4
 
5
- attr_reader :pattern, :endpoint, :regexp, :params
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
- !regexp.match(normalize(path)).nil?
14
+ !@regexp.match(normalize(path)).nil?
15
15
  end
16
16
 
17
17
  def extract_params(path)
18
- result = regexp.match path
19
- result ? Hash[params.zip(result.captures)] : {}
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
- @params = []
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)
@@ -19,7 +19,7 @@ module Rasti
19
19
  def call(env)
20
20
  route = route_for env
21
21
  env[ROUTE_PARAMS] = route.extract_params env['PATH_INFO']
22
- route.endpoint.call env
22
+ route.call env
23
23
  end
24
24
 
25
25
  private
@@ -1,5 +1,5 @@
1
1
  module Rasti
2
2
  module Web
3
- VERSION = '0.0.4'
3
+ VERSION = '0.0.5'
4
4
  end
5
5
  end
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
- def build_route(pattern, endpoint=:fake_endpoint)
6
- Rasti::Web::Route.new pattern, :fake_endpoint
7
- end
5
+ ROUTES = [
6
+ '/',
7
+ '/resource',
8
+ '/resource/:id/:action',
9
+ '/:resource(/:id(/:action))'
10
+ ]
8
11
 
9
- it 'Static' do
10
- route = build_route '/resource'
12
+ RESPONSE = [200, {}, []]
11
13
 
12
- route.pattern.must_equal '/resource'
13
- route.regexp.to_s.must_equal /^\/resource$/.to_s
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
- it 'Params' do
19
- route = build_route '/resource/:id/:action'
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
- it 'Match' do
28
- build_route('/').must_be :match?, '/'
29
- build_route('/').wont_be :match?, '/resource'
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
- build_route('/resource/:id/:action').must_be :match?, '/resource/123/show'
36
- build_route('/resource/:id/:action').wont_be :match?, '/123/show'
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
- it 'Extract params' do
40
- build_route('/resource').extract_params('/resource').must_equal Hash.new
41
- build_route('/resource/:id/:action').extract_params('/resource/123/show').must_equal 'id' => '123', 'action' => 'show'
42
- end
33
+ ['/resource', '/resource/'].each do |path|
34
+ it "Static '#{path}'" do
35
+ route = route_for path
43
36
 
44
- it 'Normalize path' do
45
- build_route('').pattern.must_equal '/'
46
- build_route('/').pattern.must_equal '/'
47
- build_route('/resource').pattern.must_equal '/resource'
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 'Block endpoint' do
52
- route = Rasti::Web::Route.new('/') do |req, res|
53
- res.status = 404
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.endpoint.must_be_instance_of Rasti::Web::Endpoint
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
- status, headers, response = route.endpoint.call(:fake_env)
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
- status.must_equal 404
63
- headers['Content-Type'].must_equal 'text/html'
64
- response.body.must_equal ['<h1>Not found</h1>']
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
@@ -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
- events = []
11
- stream = render.server_sent_events :test_channel
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
- stream.each { |e| events << e }
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
- stream.close
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
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-08-31 00:00:00.000000000 Z
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.7
237
+ rubygems_version: 2.4.3
238
238
  signing_key:
239
239
  specification_version: 4
240
240
  summary: Web blocks to build robust applications