Class: PrestoCore::Browser
- Inherits:
-
Object
- Object
- PrestoCore::Browser
- Defined in:
- lib/presto/core/browser.rb
Instance Attribute Summary (collapse)
-
- (Object) env
Returns the value of attribute env.
-
- (Object) request_method
Returns the value of attribute request_method.
-
- (Object) xhr
Returns the value of attribute xhr.
Instance Method Summary (collapse)
-
- (String) body(*args, &proc)
wrapper around #request.
-
- (Browser) initialize(controller, env = {}, request_method = 'GET', xhr = false)
constructor
A new instance of Browser.
-
- (String) request(*args, &proc)
simple tool to perform internal HTTP requests.
Constructor Details
- (Browser) initialize(controller, env = {}, request_method = 'GET', xhr = false)
A new instance of Browser
6 7 8 9 |
# File 'lib/presto/core/browser.rb', line 6 def initialize controller, env = {}, request_method = 'GET', xhr = false @controller, @env, @request_method, @xhr = controller, env, request_method, xhr end |
Instance Attribute Details
- (Object) env
Returns the value of attribute env
4 5 6 |
# File 'lib/presto/core/browser.rb', line 4 def env @env end |
- (Object) request_method
Returns the value of attribute request_method
4 5 6 |
# File 'lib/presto/core/browser.rb', line 4 def request_method @request_method end |
- (Object) xhr
Returns the value of attribute xhr
4 5 6 |
# File 'lib/presto/core/browser.rb', line 4 def xhr @xhr end |
Instance Method Details
- (String) body(*args, &proc)
wrapper around #request. it accepts same args as request and return the body returned by #request
99 100 101 |
# File 'lib/presto/core/browser.rb', line 99 def body *args, &proc request(*args, &proc)[2].body.join end |
- (String) request(*args, &proc)
simple tool to perform internal HTTP requests. it does not support authorization, so browsing an action that requires authorization will return an "Access Restricted" body.
if proc given it will be executed instead of action.
When class browser used(Controller.http.get), current sessions/cookies wont be available. To execute an action/proc on inner controller with current env, use instance browser(http.get inside action or hook) and pass controller as first argument.
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/presto/core/browser.rb', line 67 def request *args, &proc controller = ::PrestoCore::Utils.is_controller?(args.first) ? args.shift : @controller action = args.first.is_a?(Symbol) ? args.shift : :index route = controller.ctrl[action] path_info, params = [], {} args.each { |a| a.is_a?(Hash) ? params.update(a) : path_info << ::Rack::Utils.escape_path(a) } query_string = ::Rack::Utils.build_nested_query params env = @env.dup env.update 'rack.version' => ::Rack::VERSION, 'rack.input' => StringIO.new(query_string), 'rack.multithread' => true, 'rack.multiprocess' => true, 'rack.run_once' => true, 'rack.url_scheme' => 'http', 'HTTPS' => 'off', 'REQUEST_METHOD' => @request_method, 'SCRIPT_NAME' => '/%s' % action, 'PATH_INFO' => path_info.join('/'), 'QUERY_STRING' => query_string env['CONTENT_TYPE'] = 'application/x-www-form-urlencoded' if post? env['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest' if xhr? ::PrestoHTTP::Response.new(controller, route).call(env, &proc) end |