Class: Rack::Ajax
- Inherits:
-
Object
- Object
- Rack::Ajax
- Extended by:
- DecisionTree
- Defined in:
- lib/rack-ajax.rb,
lib/rack-ajax/parser.rb,
lib/rack-ajax/decision_tree.rb
Defined Under Namespace
Modules: DecisionTree Classes: Parser
Instance Attribute Summary (collapse)
-
- (Object) params
Returns the value of attribute params.
-
- (Object) request
Returns the value of attribute request.
-
- (Object) user
Returns the value of attribute user.
Instance Method Summary (collapse)
- - (Object) call(env)
-
- (Ajax) initialize(app)
constructor
If called with a block, executes that block as the "decision tree".
Methods included from DecisionTree
Constructor Details
- (Ajax) initialize(app)
If called with a block, executes that block as the "decision tree". This is useful when testing.
To integrate Rack::Ajax into your app you should store the decision tree in a class-attribute decision_tree.
The default_decision_tree is used if no other is provided.
21 22 23 24 |
# File 'lib/rack-ajax.rb', line 21 def initialize(app) @app = app @decision_tree = block_given? ? Proc.new : (self.class.decision_tree || self.class.default_decision_tree) end |
Instance Attribute Details
- (Object) params
Returns the value of attribute params
12 13 14 |
# File 'lib/rack-ajax.rb', line 12 def params @params end |
- (Object) request
Returns the value of attribute request
12 13 14 |
# File 'lib/rack-ajax.rb', line 12 def request @request end |
- (Object) user
Returns the value of attribute user
12 13 14 |
# File 'lib/rack-ajax.rb', line 12 def user @user end |
Instance Method Details
- (Object) call(env)
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/rack-ajax.rb', line 26 def call(env) return @app.call(env) unless ::Ajax.is_enabled? # Parse the Ajax-Info header if env["HTTP_AJAX_INFO"].nil? env["Ajax-Info"] = {} elsif env["HTTP_AJAX_INFO"].is_a?(String) env["Ajax-Info"] = (JSON.parse(env['HTTP_AJAX_INFO']) rescue {}) end @parser = Parser.new(env) rack_response = @parser.instance_eval(&@decision_tree) # Clear the value of session[:redirected_to] unless env['rack.session'].nil? env['rack.session']['redirected_to'] = env['rack.session'][:redirected_to] = nil end # If we are testing our Rack::Ajax middleware, return # a Rack response now rather than falling through # to the application. # # To test rewrites, return a 200 response with # the modified request environment encoded as Yaml. # # The Ajax::Spec::Helpers module includes a helper # method to test the result of a rewrite. if ::Ajax.is_mocked? rack_response.nil? ? Rack::Ajax::Parser.rack_response(env.to_yaml(:Encoding => :Utf8)) : rack_response elsif !rack_response.nil? rack_response else # Fallthrough to the app. @app.call(env) end end |