galago-router 0.0.2 → 0.1.0

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: 24dc9a36ea2bca4e8d75e2722b5ae5a78010d19f
4
- data.tar.gz: 39497251884dfba4e67bc8746029734b13a87642
3
+ metadata.gz: d57fe2ce224db366f326e84d2f12eba38bbe6120
4
+ data.tar.gz: 21925f7fdb67bfbb2766f2e862f588220c7a1eac
5
5
  SHA512:
6
- metadata.gz: 7cba7c52f27784cb3fe98ab0b199f8d88dfd911808bdeece4039d969fa44eec49feefc99f9b908505a5003268a58aa82780daea0a197b722dcbcf2791a372286
7
- data.tar.gz: 41bf1fdb47a22e7cdd7a873c3f8209c5ceedabaa60387bf37e2f0eb1e9a229c028dc7065383b4fa3a9b1cebaf34f1a3bf3dd289e4111e442e299f938794f56ef
6
+ metadata.gz: 7d27054aac2da1077f7f3adb2cb6e3b631bc95291d8571f31541015b59f8b913702630b38935f74b46fbf8b386aa59e84cd05cca115f79324f255e10d1914266
7
+ data.tar.gz: 69186fb89959bd9d8aa96537e0202c2c70924a5a58b3f3cf0b832ef09ff59aef7b13bfd562bc37edba2132575bf3d39861e2cf6ca87297725e59bbc6d97e195b
data/README.md CHANGED
@@ -24,11 +24,17 @@ require 'galago/router'
24
24
  require 'rack/lobster'
25
25
 
26
26
  router = Galago::Router.new do
27
- get '/lobsters', to: Rack::Lobster.new
28
- post '/lobsters', to: Rack::Lobster.new
29
- patch '/lobsters/:name', to: Rack::Lobster.new
30
- put '/lobsters/:name', to: Rack::Lobster.new
31
- delete '/lobsters/:name', to: Rack::Lobster.new
27
+ get '/foo', to: ->(env) { [200, {}, ['foo']] }
28
+ post '/bar/:bar', to: ->(env) { [200, {}, ['bar']] }
29
+
30
+ namespace 'lobsters' do
31
+ get '/', to: Rack::Lobster.new
32
+ post '/', to: Rack::Lobster.new
33
+
34
+ patch ':name', to: Rack::Lobster.new
35
+ put ':name', to: Rack::Lobster.new
36
+ delete ':name', to: Rack::Lobster.new
37
+ end
32
38
  end
33
39
 
34
40
  run router
@@ -0,0 +1,19 @@
1
+ require 'galago/router'
2
+ require 'rack/lobster'
3
+
4
+ router = Galago::Router.new do
5
+ get '/foo', to: ->(env) { [200, {}, ['foo']] }
6
+ post '/bar/:bar', to: ->(env) { [200, {}, ['bar']] }
7
+
8
+ namespace :lobsters do
9
+ get '/', to: Rack::Lobster.new
10
+ post '/', to: Rack::Lobster.new
11
+
12
+ patch ':name', to: Rack::Lobster.new
13
+ put ':name', to: Rack::Lobster.new
14
+ delete ':name', to: Rack::Lobster.new
15
+ end
16
+ end
17
+
18
+ run router
19
+
@@ -3,27 +3,48 @@ module Galago
3
3
  class DSL
4
4
  def initialize(router, block)
5
5
  @router = router
6
+ @namespace = ''
6
7
  instance_eval(&block)
7
8
  end
8
9
 
10
+ def namespace(new_namespace)
11
+ @namespace << "/#{new_namespace}"
12
+ yield
13
+ @namespace = ''
14
+ end
15
+
9
16
  def get(path, options)
10
- @router.add_route("GET", path, options[:to])
17
+ add_route("GET", path, options[:to])
11
18
  end
12
19
 
13
20
  def patch(path, options)
14
- @router.add_route("PATCH", path, options[:to])
21
+ add_route("PATCH", path, options[:to])
15
22
  end
16
23
 
17
24
  def post(path, options)
18
- @router.add_route("POST", path, options[:to])
25
+ add_route("POST", path, options[:to])
19
26
  end
20
27
 
21
28
  def put(path, options)
22
- @router.add_route("PUT", path, options[:to])
29
+ add_route("PUT", path, options[:to])
23
30
  end
24
31
 
25
32
  def delete(path, options)
26
- @router.add_route("DELETE", path, options[:to])
33
+ add_route("DELETE", path, options[:to])
34
+ end
35
+
36
+ private
37
+
38
+ def add_route(method, path, application)
39
+ path_with_namespace = add_namespace_to_path(path)
40
+ @router.add_route(method, path_with_namespace, application)
41
+ end
42
+
43
+ def add_namespace_to_path(path)
44
+ path = "#{@namespace}/#{path}"
45
+ path = path.gsub('//', '/')
46
+ path = path.gsub(/\/$/, '')
47
+ path
27
48
  end
28
49
  end
29
50
  end
@@ -34,7 +34,7 @@ module Galago
34
34
 
35
35
  def convert_path_to_regex(path)
36
36
  regexp = path.to_s.gsub(/\:\w+/, '([\w-]+)')
37
- Regexp.new("^#{regexp}$")
37
+ /\A#{regexp}$/
38
38
  end
39
39
 
40
40
  def identify_params_in_path(path)
@@ -1,5 +1,5 @@
1
1
  module Galago
2
2
  class Router
3
- VERSION = '0.0.2'
3
+ VERSION = '0.1.0'
4
4
  end
5
5
  end
@@ -18,6 +18,11 @@ module Galago
18
18
  path = Router::Path.new('/users/:id')
19
19
  expect(path).not_to be_recognizes '/users/:id'
20
20
  end
21
+
22
+ it "does not recognize paths with newlines" do
23
+ path = Router::Path.new('/users/:id')
24
+ expect(path).not_to be_recognizes "\n/users/1"
25
+ end
21
26
  end
22
27
 
23
28
  describe "#to_s" do
@@ -2,7 +2,7 @@ require 'spec_helper'
2
2
 
3
3
  module Galago
4
4
  describe Router do
5
- describe '.routes' do
5
+ describe 'routes' do
6
6
  it 'adds the specified routes' do
7
7
  router = Router.new do
8
8
  get '/foo' , to: lambda { |env| 'bar' }
@@ -18,6 +18,50 @@ module Galago
18
18
  expect(router).to have_route(:put, '/foo')
19
19
  expect(router).to have_route(:delete, '/foo')
20
20
  end
21
+
22
+ it 'adds the namespace to the route' do
23
+ router = Router.new do
24
+ namespace :foo do
25
+ get '/', to: ->(env) { [200, {}, ['foo']] }
26
+ post '/', to: ->(env) { [200, {}, ['post foo']] }
27
+
28
+ get '/:foo', to: ->(env) { [200, {}, ['get :foo']] }
29
+ patch '/:foo', to: ->(env) { [200, {}, ['patch :foo']] }
30
+ put '/:foo', to: ->(env) { [200, {}, ['put :foo']] }
31
+ delete '/:foo', to: ->(env) { [200, {}, ['delete :foo']] }
32
+ end
33
+ end
34
+
35
+ expect(router).to have_route(:get, '/foo')
36
+ expect(router).to have_route(:post, '/foo')
37
+
38
+ expect(router).to have_route(:get, '/foo/1')
39
+ expect(router).to have_route(:patch, '/foo/2')
40
+ expect(router).to have_route(:put, '/foo/3')
41
+ expect(router).to have_route(:delete, '/foo/4')
42
+ end
43
+
44
+ it 'can have multiple namespaces' do
45
+ router = Router.new do
46
+ namespace :foo do
47
+ get '/', to: ->(env) { [200, {}, ['foo']] }
48
+
49
+ namespace :bar do
50
+ get '/', to: ->(env) { [200, {}, ['foo bar']] }
51
+ get ':bar', to: ->(env) { [200, {}, ['foo bar']] }
52
+ end
53
+ end
54
+
55
+ namespace :hello do
56
+ get '/', to: ->(env) { [200, {}, ['hello']] }
57
+ end
58
+ end
59
+
60
+ expect(router).to have_route(:get, '/foo')
61
+ expect(router).to have_route(:get, '/foo/bar')
62
+ expect(router).to have_route(:get, '/foo/bar/1')
63
+ expect(router).to have_route(:get, '/hello')
64
+ end
21
65
  end
22
66
 
23
67
  describe "#add_route" do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: galago-router
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joe Karayusuf
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-03-03 00:00:00.000000000 Z
11
+ date: 2014-03-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack
@@ -108,6 +108,7 @@ files:
108
108
  - Rakefile
109
109
  - examples/echo_param.ru
110
110
  - examples/lobster.ru
111
+ - examples/readme.ru
111
112
  - galago-router.gemspec
112
113
  - lib/galago/router.rb
113
114
  - lib/galago/router/dsl.rb