Class: Raw::ScriptAdapter

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

Instance Method Summary collapse

Methods included from AdapterHandlerMixin

#handle_context, #rewrite, #unrewrite

Methods inherited from Adapter

#initialize, #setup, #stop

Constructor Details

This class inherits a constructor from Raw::Adapter

Instance Attribute Details

#responseObject

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, options = {})
  options[:method] = "get"
  handle(uri, options)
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, options = {})
  # Perform default rewriting rules.
  # rewrite(req)

  context = Context.new(@application)
  
  context.get_params = options.fetch(:get_params, {})
  context.post_params = options.fetch(:post_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]
        
  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, options = {})
  options[:method] = "post"
  handle(uri, options)
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