rack-action 0.0.1 → 0.1.0

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.
data/.gitignore CHANGED
@@ -15,3 +15,5 @@ spec/reports
15
15
  test/tmp
16
16
  test/version_tmp
17
17
  tmp
18
+ log/*.log
19
+ *.swp
data/lib/rack/action.rb CHANGED
@@ -2,6 +2,7 @@ require 'time'
2
2
  require 'json'
3
3
  require 'rack'
4
4
  require 'rack/filters'
5
+ require 'rack/version'
5
6
 
6
7
  module Rack
7
8
  # Rack::Action provides functionality to generate a Rack response.
@@ -104,10 +105,12 @@ module Rack
104
105
  extend Filters
105
106
 
106
107
  # @private
107
- RACK_ROUTE_PARAMS = 'route.route_params'.freeze
108
+ RACK_ROUTE_PARAMS = 'rack.route_params'.freeze
108
109
  # @private
109
110
  CONTENT_TYPE = 'Content-Type'.freeze
110
111
  # @private
112
+ TEXT_HTML = 'text/html'.freeze
113
+ # @private
111
114
  APPLICATION_JSON = 'application/json'.freeze
112
115
  # @private
113
116
  LOCATION = 'Location'.freeze
@@ -117,7 +120,7 @@ module Rack
117
120
  # This implements the Rack interface
118
121
  #
119
122
  # @param [Hash] env The Rack environment
120
- # @return [Rack::Response] A Rack response
123
+ # @return [Array<Numeric, Hash, #each>] A Rack response
121
124
  def self.call(env)
122
125
  new(env).call
123
126
  end
@@ -137,11 +140,14 @@ module Rack
137
140
  end
138
141
 
139
142
  def params
140
- @params ||= request.params.merge(env[RACK_ROUTE_PARAMS] || {})
143
+ @params ||= begin
144
+ p = request.params.merge(env[RACK_ROUTE_PARAMS] || {})
145
+ p.respond_to?(:with_indifferent_access) ? p.with_indifferent_access : p
146
+ end
141
147
  end
142
148
 
143
149
  # This is the main method responsible for generating a Rack response.
144
- # You typically won't need to override this method of call it directly.
150
+ # You typically won't need to override this method or call it directly.
145
151
  # First this will run the before filters for this action.
146
152
  # If none of the before filters generate a response, this will call
147
153
  # {#respond} to generate a response.
@@ -150,6 +156,8 @@ module Rack
150
156
  #
151
157
  # @return [Array<Numeric, Hash, #each>] A Rack response
152
158
  def call
159
+ log_call
160
+ set_default_headers
153
161
  run_before_filters
154
162
  run_respond
155
163
  run_after_filters
@@ -195,11 +203,30 @@ module Rack
195
203
  def redirect_to(url, options={})
196
204
  full_url = absolute_url(url, options)
197
205
  response[LOCATION] = full_url
198
- response.status = 302
199
- response.write ''
206
+ respond_with 302
200
207
  full_url
201
208
  end
202
209
 
210
+ # Convenience method to return a 404
211
+ def not_found
212
+ respond_with 404
213
+ end
214
+
215
+ # Convenience method to return a 403
216
+ def forbidden
217
+ respond_with 403
218
+ end
219
+
220
+ # This is a convenience method to set the response code and
221
+ # set the response so that it stops respond process.
222
+ #
223
+ # @param [Fixnum] status_code The HTTP status code to use in the response
224
+ def respond_with(status_code)
225
+ response.status = status_code
226
+ response.write ''
227
+ nil
228
+ end
229
+
203
230
  # Generate an absolute url from the url. If the url is already
204
231
  # an absolute url, this will return it unchanged.
205
232
  #
@@ -217,10 +244,26 @@ module Rack
217
244
  end
218
245
 
219
246
  protected
247
+ def log_call
248
+ if logger
249
+ logger.debug do
250
+ "#{self.class} #{request.env["REQUEST_METHOD"]} params: #{params.inspect}"
251
+ end
252
+ end
253
+ end
254
+
255
+ def set_default_headers
256
+ response[CONTENT_TYPE] = TEXT_HTML
257
+ end
258
+
220
259
  def run_before_filters
221
260
  self.class.before_filters.each do |filter|
261
+ logger.debug "Running #{filter} before filter" if logger
222
262
  send(filter)
223
- return unless response.empty?
263
+ unless response.empty?
264
+ logger.debug "#{filter} responded, halting filter chain" if logger
265
+ return
266
+ end
224
267
  end
225
268
  end
226
269
 
@@ -236,6 +279,7 @@ module Rack
236
279
 
237
280
  def run_after_filters
238
281
  self.class.after_filters.each do |filter|
282
+ logger.debug "Running #{filter} after filter" if logger
239
283
  send(filter)
240
284
  end
241
285
  end
@@ -244,6 +288,22 @@ module Rack
244
288
  response.finish
245
289
  end
246
290
 
291
+ def logger
292
+ self.class.logger
293
+ end
294
+
295
+ def self.logger
296
+ if defined? @logger
297
+ @logger
298
+ else
299
+ @logger = superclass.logger if superclass.respond_to?(:logger)
300
+ end
301
+ end
302
+
303
+ def self.logger=(logger)
304
+ @logger = logger
305
+ end
306
+
247
307
  # @private
248
308
  class URL
249
309
  HTTPS = 'https'.freeze
@@ -0,0 +1,5 @@
1
+ module Rack
2
+ class Action
3
+ VERSION = '0.1.0'
4
+ end
5
+ end
data/rack-action.gemspec CHANGED
@@ -12,7 +12,7 @@ Gem::Specification.new do |gem|
12
12
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
13
13
  gem.name = "rack-action"
14
14
  gem.require_paths = ["lib"]
15
- gem.version = "0.0.1"
15
+ gem.version = "0.1.0"
16
16
 
17
17
  gem.add_runtime_dependency "rack"
18
18
  end
@@ -7,7 +7,6 @@ class Rack::ActionTest < RackTest
7
7
  app = Class.new(Rack::Action)
8
8
 
9
9
  response = get app, "/"
10
-
11
10
  assert_equal 200, response.status
12
11
  assert_equal Rack::Action::DEFAULT_RESPONSE.length, response.length
13
12
  assert_equal 'text/html', response["Content-Type"]
data/test/rack_test.rb CHANGED
@@ -1,5 +1,15 @@
1
1
  require 'test/unit'
2
2
  require 'stringio'
3
+ require 'logger'
4
+ require 'fileutils'
5
+
6
+ if ENV['RACK_ACTION_TEST_LOGGER'].to_s.downcase == 'stdout'
7
+ Rack::Action.logger = Logger.new(STDOUT)
8
+ elsif ENV['RACK_ACTION_TEST_LOGGER']
9
+ log_file = File.expand_path(ENV['RACK_ACTION_TEST_LOGGER'])
10
+ FileUtils.mkdir_p File.dirname(log_file)
11
+ Rack::Action.logger = Logger.new(log_file)
12
+ end
3
13
 
4
14
  class RackTest < Test::Unit::TestCase
5
15
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-action
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-01-03 00:00:00.000000000 Z
12
+ date: 2013-02-21 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rack
@@ -45,6 +45,7 @@ files:
45
45
  - lib/rack-action.rb
46
46
  - lib/rack/action.rb
47
47
  - lib/rack/filters.rb
48
+ - lib/rack/version.rb
48
49
  - lib/rack_action.rb
49
50
  - rack-action.gemspec
50
51
  - test/rack/action_test.rb
@@ -69,7 +70,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
69
70
  version: '0'
70
71
  requirements: []
71
72
  rubyforge_project:
72
- rubygems_version: 1.8.23
73
+ rubygems_version: 1.8.24
73
74
  signing_key:
74
75
  specification_version: 3
75
76
  summary: a small, simple framework for generating Rack responses