rack-routes 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,9 +2,7 @@ require 'uri'
2
2
 
3
3
  module Rack
4
4
  class Routes
5
- VERSION = '0.1.1'
6
-
7
- class LocDirectiveError < RuntimeError; end
5
+ VERSION = '0.2.0'
8
6
 
9
7
  class << self
10
8
 
@@ -38,6 +36,8 @@ module Rack
38
36
  # === Args
39
37
  # +path+:: The path to match on. Can be String or Regexp.
40
38
  # +opts+:: Hash of options. see below.
39
+ # +app+:: Optional object which responds to :call
40
+ # +block+:: required if +app+ is missing
41
41
  #
42
42
  # +opts+ keys:
43
43
  # +:exact+:: Type of string matching. default false
@@ -53,6 +53,8 @@ module Rack
53
53
  #
54
54
  # yields +env+
55
55
  #
56
+ # raises ArgumentError if app or block is missing or if type is invalid
57
+ #
56
58
  # === Examples
57
59
  #
58
60
  # # config.ru
@@ -89,7 +91,12 @@ module Rack
89
91
  #
90
92
  # run Rack::Routes
91
93
 
92
- def location path, opts = {}, &blk
94
+ def location path, *args, &blk
95
+ app = args.last.respond_to?(:call) ? args.pop : blk
96
+ raise ArgumentError, 'must provide either an app or a block' unless app
97
+
98
+ opts = Hash === args.last ? args.pop : {}
99
+
93
100
  type = opts.fetch(:type, nil)
94
101
  type = :regex if Regexp === path
95
102
  type ||= case opts.fetch(:exact, opts.fetch(:prefix, false))
@@ -101,11 +108,9 @@ module Rack
101
108
  :string_break
102
109
  end
103
110
 
104
- raise LocDirectiveError, "unknown type `#{type}'" unless
111
+ raise ArgumentError, "unknown type `#{type}'" unless
105
112
  [:regex, :string, :exact, :string_break].include? type
106
113
 
107
- app = blk
108
-
109
114
  locations[type] << [path, app, opts]
110
115
  end
111
116
 
@@ -123,16 +123,34 @@ class TestRack::TestRoutes < RoutesTestCase
123
123
  end
124
124
 
125
125
  def test_invalid_types
126
- assert_raises app::LocDirectiveError do
126
+ assert_raises ArgumentError do
127
127
  app.location '/', :prefix => nil do
128
128
  end
129
129
  end
130
- assert_raises app::LocDirectiveError do
130
+ assert_raises ArgumentError do
131
131
  app.location '/', :prefix => 'unknown' do
132
132
  end
133
133
  end
134
134
  end
135
135
 
136
+ def test_app_or_block
137
+ app.location '/a', lambda{|e| '3' }
138
+ assert_location_match '3', '/a'
139
+
140
+ app.location '/b' do; '4' end
141
+ assert_location_match '4', '/b'
142
+
143
+ assert_raises ArgumentError do
144
+ app.location '/', 'doesnt respond to call'
145
+ end
146
+ end
147
+
148
+ def test_app_with_opts
149
+ app.location '/a', {:exact => true}, lambda{|e| '1'}
150
+
151
+ assert_equal '/a', app.locations[:exact][0][0]
152
+ end
153
+
136
154
  def assert_location_match expected, path, opts = {}
137
155
  @env.merge! opts
138
156
  @env['PATH_INFO'] = path
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-routes
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-02-22 00:00:00.000000000 Z
12
+ date: 2012-02-27 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rdoc
16
- requirement: &10643260 !ruby/object:Gem::Requirement
16
+ requirement: &23243340 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '3.10'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *10643260
24
+ version_requirements: *23243340
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: hoe
27
- requirement: &10642840 !ruby/object:Gem::Requirement
27
+ requirement: &23242740 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,7 +32,7 @@ dependencies:
32
32
  version: '2.13'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *10642840
35
+ version_requirements: *23242740
36
36
  description: ! 'Provides a routing layer for Rack similar to nginx location directive.
37
37
 
38
38
  http://wiki.nginx.org/HttpCoreModule#location