pakada-dispatch 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,6 +1,8 @@
1
1
  require "pakada"
2
2
  require "rack/mount"
3
+ require "yajl"
3
4
 
5
+ require "pakada/dispatch/util"
4
6
  require "pakada/dispatch/controller"
5
7
  require "pakada/dispatch/module"
6
8
  require "pakada/dispatch/version"
@@ -1,8 +1,10 @@
1
1
  class Pakada
2
2
  class Dispatch
3
3
  Action = Struct.new :block, :options
4
-
4
+
5
5
  module Controller
6
+ include Util
7
+
6
8
  def self.build(&block)
7
9
  Class.new do
8
10
  include Controller
@@ -14,7 +16,7 @@ class Pakada
14
16
 
15
17
  def initialize(env)
16
18
  @request, @response = Rack::Request.new(env), Rack::Response.new
17
- @params = request.env["rack.routing_args"] || {}
19
+ @params = request.env["pakada.dispatch.params"] || {}
18
20
  end
19
21
 
20
22
  def call_action(name, options = {})
@@ -22,7 +24,58 @@ class Pakada
22
24
  raise ArgumentError, "No such action - #{name}" unless action
23
25
 
24
26
  @options = action.options.merge options
25
- instance_eval &action.block
27
+ catch(:finish) { instance_eval &action.block }
28
+ end
29
+
30
+ def finish!
31
+ throw :finish
32
+ end
33
+
34
+ def redirect(*args)
35
+ response.status = (Fixnum === args[0]) ? args.shift : 303
36
+
37
+ if Symbol === args[0]
38
+ url = url(*args)
39
+ elsif args[0].respond_to? :to_url
40
+ url = args[0].to_url
41
+ elsif args[0]
42
+ url = args[0]
43
+ end
44
+
45
+ response.headers["Location"] = url
46
+ finish!
47
+ end
48
+
49
+ def forbidden(format = nil)
50
+ response.status = 403
51
+ if format == :json
52
+ response.headers["Content-Type"] = "application/json"
53
+ response.write Yajl::Encoder.encode({
54
+ :status => {
55
+ :code => 403,
56
+ :message => "Forbidden"
57
+ }
58
+ })
59
+ else
60
+ response.write "403 Forbidden"
61
+ end
62
+ finish!
63
+ end
64
+
65
+ def not_found(format = nil)
66
+ response.status = 404
67
+ if format == :json
68
+ response.headers["Content-Type"] = "application/json"
69
+ response.write Yajl::Encoder.encode({
70
+ :status => {
71
+ :code => 404,
72
+ :message => "Not Found"
73
+ }
74
+ })
75
+ else
76
+ response.write "404 Not Found"
77
+ end
78
+ finish!
26
79
  end
27
80
 
28
81
  module ClassMethods
@@ -1,6 +1,8 @@
1
1
  class Pakada
2
2
  class Dispatch
3
3
  module Module
4
+ include Util
5
+
4
6
  attr_reader :controllers
5
7
 
6
8
  def load_controllers
@@ -52,7 +54,7 @@ class Pakada
52
54
  mod = Pakada[params[:module]]
53
55
  controller = mod.controllers[params[:controller].to_sym] if mod
54
56
  if controller
55
- controller.action(params[:action].to_sym).call env
57
+ controller.action(params[:action]).call(env)
56
58
  else
57
59
  [404, {"X-Cascade" => "pass"}, []]
58
60
  end
@@ -62,9 +64,5 @@ class Pakada
62
64
  Pakada[:dispatch].router.add_route app, {:path_info => route}, defaults, name
63
65
  end
64
66
  end
65
-
66
- def url(*args)
67
- router.url({}, *args)
68
- end
69
67
  end
70
68
  end
@@ -0,0 +1,12 @@
1
+ class Pakada
2
+ class Dispatch
3
+ module Util
4
+ def url(*args)
5
+ params = args.find {|a| a.kind_of?(Hash) }
6
+ args << params = {} unless params
7
+ params[:only_path] = true
8
+ Pakada[:dispatch].router.url({}, *args)
9
+ end
10
+ end
11
+ end
12
+ end
@@ -1,5 +1,5 @@
1
1
  class Pakada
2
2
  class Dispatch
3
- VERSION = "0.0.2"
3
+ VERSION = "0.0.3"
4
4
  end
5
5
  end
@@ -14,6 +14,7 @@ Gem::Specification.new do |s|
14
14
 
15
15
  s.add_dependency "pakada"
16
16
  s.add_dependency "rack-mount"
17
+ s.add_dependency "yajl-ruby"
17
18
 
18
19
  s.add_development_dependency "rspec"
19
20
  s.add_development_dependency "fakefs"
@@ -77,7 +77,7 @@ describe "SomeController" do
77
77
 
78
78
  it "populates #params with the routing parameters" do
79
79
  params = {:foo => :bar}
80
- controller = subject.new "rack.routing_args" => params
80
+ controller = subject.new "pakada.dispatch.params" => params
81
81
  controller.params.should == params
82
82
 
83
83
  controller = subject.new({})
@@ -119,6 +119,127 @@ describe "SomeController" do
119
119
  end
120
120
  end
121
121
  end
122
+
123
+ describe "#finish!" do
124
+ it "immediately finishes the action's execution" do
125
+ finishing = nil
126
+ subject.action :foo do
127
+ finishing = :now
128
+ finish!
129
+ finishing = :nooot
130
+ end
131
+
132
+ subject.new({}).call_action :foo
133
+ finishing.should == :now
134
+ end
135
+ end
136
+
137
+ describe "#redirect" do
138
+ describe "when called with a URL string" do
139
+ it "redirects to the URL and finishes" do
140
+ controller = subject.new({})
141
+ controller.should_receive :finish!
142
+ controller.redirect "/something"
143
+
144
+ controller.response.headers["Location"].should == "/something"
145
+ end
146
+ end
147
+
148
+ describe "when called with an object that responds to #to_url" do
149
+ it "redirects to the URL returned by #to_url and finishes" do
150
+ controller = subject.new({})
151
+ controller.should_receive :finish!
152
+ controller.redirect stub("object", :to_url => "/something")
153
+
154
+ controller.response.headers["Location"].should == "/something"
155
+ end
156
+ end
157
+
158
+ describe "when called with a route name and (optionally) a hash" do
159
+ it "redirects to the URL generated by Util#url and finishes" do
160
+ controller = subject.new({})
161
+ controller.should_receive :finish!
162
+ controller.should_receive(:url).with(:myroute, :key => "value") { "/something" }
163
+ controller.redirect :myroute, :key => "value"
164
+
165
+ controller.response.headers["Location"].should == "/something"
166
+ end
167
+ end
168
+
169
+ it "accepts an HTTP status code" do
170
+ controller = subject.new({})
171
+ controller.stub :finish!
172
+ controller.redirect 302, "/"
173
+
174
+ controller.response.status.should == 302
175
+ end
176
+
177
+ it "uses 303 See Other as the default HTTP status code" do
178
+ controller = subject.new({})
179
+ controller.stub :finish!
180
+ controller.redirect "/"
181
+
182
+ controller.response.status.should == 303
183
+ end
184
+ end
185
+
186
+ describe "#not_found" do
187
+ it "sets a 404 status code, writes a message and finishes" do
188
+ subject.action(:foo) { not_found }
189
+ controller = subject.new({})
190
+
191
+ controller.should_receive :finish!
192
+ controller.call_action :foo
193
+
194
+ controller.response.status.should == 404
195
+ controller.response.body.join.should == "404 Not Found"
196
+ end
197
+
198
+ describe "when called with :json as argument" do
199
+ it "writes the message in JSON format" do
200
+ subject.action(:foo) { not_found :json }
201
+ controller = subject.new({})
202
+ controller.call_action :foo
203
+
204
+ controller.response.headers["Content-Type"].should == "application/json"
205
+ Yajl::Parser.parse(controller.response.body.join).should == {
206
+ "status" => {
207
+ "code" => 404,
208
+ "message" => "Not Found"
209
+ }
210
+ }
211
+ end
212
+ end
213
+ end
214
+
215
+ describe "#forbidden" do
216
+ it "sets a 403 status code, writes a message and finishes" do
217
+ subject.action(:foo) { forbidden }
218
+ controller = subject.new({})
219
+
220
+ controller.should_receive :finish!
221
+ controller.call_action :foo
222
+
223
+ controller.response.status.should == 403
224
+ controller.response.body.join.should == "403 Forbidden"
225
+ end
226
+
227
+ describe "when called with :json as argument" do
228
+ it "writes the message in JSON format" do
229
+ subject.action(:foo) { forbidden :json }
230
+ controller = subject.new({})
231
+ controller.call_action :foo
232
+
233
+ controller.response.headers["Content-Type"].should == "application/json"
234
+ Yajl::Parser.parse(controller.response.body.join).should == {
235
+ "status" => {
236
+ "code" => 403,
237
+ "message" => "Forbidden"
238
+ }
239
+ }
240
+ end
241
+ end
242
+ end
122
243
  end
123
244
 
124
245
  describe Pakada::Dispatch::Action do
data/spec/module_spec.rb CHANGED
@@ -279,20 +279,4 @@ describe Pakada::Dispatch::Module do
279
279
  end
280
280
  end
281
281
  end
282
-
283
- describe "#uri" do
284
- before { Pakada.boot }
285
-
286
- subject { Pakada[:dispatch] }
287
-
288
- it "passes over to #router.url" do
289
- subject.route :myroute, "/:key" => "foo#bar", :key => "value"
290
-
291
- subject.router.should_receive(:url).with({}, :myroute, :key => "value2").and_return("/value2")
292
- subject.url(:myroute, :key => "value2").should == "/value2"
293
-
294
- subject.router.should_receive(:url).with({}).and_return("/")
295
- subject.url.should == "/"
296
- end
297
- end
298
282
  end
data/spec/util_spec.rb ADDED
@@ -0,0 +1,21 @@
1
+ require "spec_helper"
2
+
3
+ describe Pakada::Dispatch::Util do
4
+ subject do
5
+ Class.new { include Pakada::Dispatch::Util }.new
6
+ end
7
+
8
+ describe "#uri" do
9
+ before do
10
+ Pakada.stub(:[]).and_return stub("dispatch", :router => stub("router"))
11
+ end
12
+
13
+ it "passes over to Pakada::Dispatch#router.url" do
14
+ Pakada[:dispatch].router.should_receive(:url).with({}, :myroute, {
15
+ :key => "value",
16
+ :only_path => true
17
+ }).once.and_return("/value")
18
+ subject.url(:myroute, :key => "value").should == "/value"
19
+ end
20
+ end
21
+ end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 2
9
- version: 0.0.2
8
+ - 3
9
+ version: 0.0.3
10
10
  platform: ruby
11
11
  authors:
12
12
  - Lars Gierth
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2011-01-25 00:00:00 +01:00
17
+ date: 2011-01-31 00:00:00 +01:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -44,7 +44,7 @@ dependencies:
44
44
  prerelease: false
45
45
  version_requirements: *id002
46
46
  - !ruby/object:Gem::Dependency
47
- name: rspec
47
+ name: yajl-ruby
48
48
  requirement: &id003 !ruby/object:Gem::Requirement
49
49
  none: false
50
50
  requirements:
@@ -53,11 +53,11 @@ dependencies:
53
53
  segments:
54
54
  - 0
55
55
  version: "0"
56
- type: :development
56
+ type: :runtime
57
57
  prerelease: false
58
58
  version_requirements: *id003
59
59
  - !ruby/object:Gem::Dependency
60
- name: fakefs
60
+ name: rspec
61
61
  requirement: &id004 !ruby/object:Gem::Requirement
62
62
  none: false
63
63
  requirements:
@@ -69,6 +69,19 @@ dependencies:
69
69
  type: :development
70
70
  prerelease: false
71
71
  version_requirements: *id004
72
+ - !ruby/object:Gem::Dependency
73
+ name: fakefs
74
+ requirement: &id005 !ruby/object:Gem::Requirement
75
+ none: false
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ segments:
80
+ - 0
81
+ version: "0"
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: *id005
72
85
  description:
73
86
  email:
74
87
  - lars.gierth@gmail.com
@@ -89,12 +102,14 @@ files:
89
102
  - lib/pakada/dispatch.rb
90
103
  - lib/pakada/dispatch/controller.rb
91
104
  - lib/pakada/dispatch/module.rb
105
+ - lib/pakada/dispatch/util.rb
92
106
  - lib/pakada/dispatch/version.rb
93
107
  - pakada-dispatch.gemspec
94
108
  - spec/controller_spec.rb
95
109
  - spec/dispatch_spec.rb
96
110
  - spec/module_spec.rb
97
111
  - spec/spec_helper.rb
112
+ - spec/util_spec.rb
98
113
  has_rdoc: true
99
114
  homepage: https://rubygems.org/gems/pakada-dispatch
100
115
  licenses: []
@@ -109,7 +124,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
109
124
  requirements:
110
125
  - - ">="
111
126
  - !ruby/object:Gem::Version
112
- hash: 375942273
127
+ hash: -572190931
113
128
  segments:
114
129
  - 0
115
130
  version: "0"
@@ -118,7 +133,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
118
133
  requirements:
119
134
  - - ">="
120
135
  - !ruby/object:Gem::Version
121
- hash: 375942273
136
+ hash: -572190931
122
137
  segments:
123
138
  - 0
124
139
  version: "0"