Class: PrestoCore::Browser

Inherits:
Object
  • Object
show all
Defined in:
lib/presto/core/browser.rb

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

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

Returns:

  • (String)


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.

Examples:

class App
  include Presto
  http.map
  def action arg
  end
end
# now you can use http.get(:action, 'someVal') from inside App class
# and App.http.get(:action, 'someVal') from outside App class.
# As it is an HTTP request, it will respect auth set by called controller.

initialize controller with given action but execute the given block instead of action

class App
  include Presto
  http.map

  def some_action
    'some body'
  end
end

App.http.get :some_action
#=> some body

App.http.get(:some_action) { 'another body' }
#=> another body

execute code on inner controller with current env,

so inner controller will have access to current session/cookies/auth

 module App
   class Forum
     def users_online
     end
   end
   class News
     def index
       @users_online = http.get(Forum, :users_online)
     end
   end
 end

Parameters:

  • args (Array)
  • proc (Proc)

Returns:

  • (String)


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