Sha256: 43461e3bcfa91c3179de54817e1ceeeb0ca91885e81a4d63132adac1b4605f11

Contents?: true

Size: 1.58 KB

Versions: 2

Compression:

Stored size: 1.58 KB

Contents

require 'nitro/cgi'

module Nitro

# 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.

class ScriptAdapter 

  # The last generated response.
  
  attr_accessor :response
  
  def initialize(server)
    @server = server
  end
  
  # 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'
  
  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
  
  # Perform a programmatic http get request to the web app.
   
  def get(uri, options = {})
    options[:method] = :get
    handle uri, options
  end

  # Perform a programmatic http post request to the web app.
   
  def post(uri, options = {})
    options[:method] = :post
    handle uri, options
  end
    
end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
nitro-0.41.0 lib/nitro/adapter/script.rb
nitro-0.40.0 lib/nitro/adapter/script.rb