Class: Raw::ScriptAdapter
- Includes:
- AdapterHandlerMixin
- Defined in:
- lib/raw/adapter/script.rb
Overview
The script adapter. Useful when running in console mode, or when creating scripts for cron jobs, testing and more. Allows you to programmatically ‘drive’ the web application.
Instance Attribute Summary collapse
-
#response ⇒ Object
The last generated response.
Instance Method Summary collapse
-
#get(uri, options = {}) ⇒ Object
Perform a programmatic http get request to the web app.
-
#handle(uri, options = {}) ⇒ Object
Perform a programatic http request to the web app.
-
#post(uri, options = {}) ⇒ Object
Perform a programmatic http post request to the web app.
- #start(application) ⇒ Object
Methods included from AdapterHandlerMixin
#handle_context, #rewrite, #unrewrite
Methods inherited from Adapter
Constructor Details
This class inherits a constructor from Raw::Adapter
Instance Attribute Details
#response ⇒ Object
The last generated response.
14 15 16 |
# File 'lib/raw/adapter/script.rb', line 14 def response @response end |
Instance Method Details
#get(uri, options = {}) ⇒ Object
Perform a programmatic http get request to the web app.
62 63 64 65 |
# File 'lib/raw/adapter/script.rb', line 62 def get(uri, = {}) [:method] = "get" handle(uri, ) end |
#handle(uri, options = {}) ⇒ Object
Perform a programatic http request to the web app.
Examples
$srv.get ‘users/logout’ $srv.post ‘users/login’, :params => { :name => ‘gmosx’, :password => ‘pass’ } $srv.post ‘users/login?name=gmosx;password=pass $srv.post ’articles/view/1’
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/raw/adapter/script.rb', line 37 def handle(uri, = {}) # Perform default rewriting rules. # rewrite(req) context = Context.new(@application) context.get_params = .fetch(:get_params, {}) context.post_params = .fetch(:post_params, {}) context.headers = .fetch(:headers, {}) context.headers["REQUEST_URI"] = uri context.headers["REQUEST_METHOD"] = .fetch(:method, :get) context.headers["HTTP_COOKIE"] ||= [:cookies] handle_context(context) @response = context context.close return context end |
#post(uri, options = {}) ⇒ Object
Perform a programmatic http post request to the web app.
69 70 71 72 |
# File 'lib/raw/adapter/script.rb', line 69 def post(uri, = {}) [:method] = "post" handle(uri, ) end |
#start(application) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/raw/adapter/script.rb', line 16 def start(application) info "This console is attached to the application context." info "" info "* $app points to the application" info "* $srv points to the adapter" info "* use get(uri), post(uri), response() to programmatically call actions" info "" $app = $application = @application = application $srv = $adapter = self end |