Class: Nitro::ScriptAdapter

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#responseObject

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, options = {})
  options[:method] = :get
  handle uri, options
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, options = {})
  context = Context.new(@server)
  
  begin
    context.params = options.fetch(:params, {})
    context.headers = options.fetch(:headers, {})
 
    context.headers['REQUEST_URI'] = uri
    context.headers['REQUEST_METHOD'] = options.fetch(:method, :get)
    context.headers['HTTP_COOKIE'] ||= options[:cookies]
        
    Cgi.parse_params context
    Cgi.parse_cookies 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, options = {})
  options[:method] = :post
  handle uri, options
end