Class: Nitro::ScriptAdapter
- Inherits:
-
Object
- Object
- Nitro::ScriptAdapter
- Defined in:
- lib/nitro/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 programmatic http request to the web app.
-
#initialize(server) ⇒ ScriptAdapter
constructor
A new instance of ScriptAdapter.
-
#post(uri, options = {}) ⇒ Object
Perform a programmatic http post request to the web app.
Constructor Details
#initialize(server) ⇒ ScriptAdapter
Returns a new instance of ScriptAdapter.
15 16 17 |
# File 'lib/nitro/adapter/script.rb', line 15 def initialize(server) @server = server end |
Instance Attribute Details
#response ⇒ Object
The last generated response.
13 14 15 |
# File 'lib/nitro/adapter/script.rb', line 13 def response @response end |
Instance Method Details
#get(uri, options = {}) ⇒ Object
Perform a programmatic http get request to the web app.
55 56 57 58 |
# File 'lib/nitro/adapter/script.rb', line 55 def get(uri, = {}) [:method] = :get handle uri, end |
#handle(uri, options = {}) ⇒ Object
Perform a programmatic http request to the web app.
Examples
app.get ‘users/logout’ app.post ‘users/login’, :params => { :name => ‘gmosx’, :password => ‘pass’ } app.post ‘users/login?name=gmosx;password=pass app.post ’articles/view/1’
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/nitro/adapter/script.rb', line 28 def handle(uri, = {}) context = Context.new(@server) begin context.params = .fetch(:params, {}) context.headers = .fetch(:headers, {}) context.headers['REQUEST_URI'] = uri context.headers['REQUEST_METHOD'] = .fetch(:method, :get) context.headers['HTTP_COOKIE'] ||= [:cookies] Cgi.parse_params context Cgi. context context.render uri context.close ensure $autoreload_dirty = false Og.manager.put_store if defined?(Og) and Og.respond_to?(:manager) and Og.manager end @response = context end |
#post(uri, options = {}) ⇒ Object
Perform a programmatic http post request to the web app.
62 63 64 65 |
# File 'lib/nitro/adapter/script.rb', line 62 def post(uri, = {}) [:method] = :post handle uri, end |