Class: Rack::Action

Inherits:
Object
  • Object
show all
Extended by:
Filters
Defined in:
lib/rack/action.rb

Constant Summary collapse

VERSION =
'0.5.1'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Filters

after_filter, after_filters, before_filter, before_filters, skip_after_filter, skip_before_filter

Constructor Details

#initialize(env) ⇒ Action

Returns a new instance of Action.



39
40
41
# File 'lib/rack/action.rb', line 39

def initialize(env)
  @env = env
end

Instance Attribute Details

#envObject

Returns the value of attribute env.



37
38
39
# File 'lib/rack/action.rb', line 37

def env
  @env
end

#paramsObject

Returns the value of attribute params.



37
38
39
# File 'lib/rack/action.rb', line 37

def params
  @params
end

#requestObject

Returns the value of attribute request.



37
38
39
# File 'lib/rack/action.rb', line 37

def request
  @request
end

#responseObject

Returns the value of attribute response.



37
38
39
# File 'lib/rack/action.rb', line 37

def response
  @response
end

Class Method Details

.call(env) ⇒ Array<Numeric, Hash, #each>

This implements the Rack interface

Parameters:

  • env (Hash)

    The Rack environment

Returns:

  • (Array<Numeric, Hash, #each>)

    A Rack response



33
34
35
# File 'lib/rack/action.rb', line 33

def self.call(env)
  new(env).call
end

Instance Method Details

#absolute_url(url, options = {}) ⇒ String

Generate an absolute url from the url. If the url is already an absolute url, this will return it unchanged.

Parameters:

  • url (String)

    The URL

  • options (Hash) (defaults to: {})

    The options to use to generate the absolute URL

Options Hash (options):

  • :https (true, false)

    If https should be used, uses rack.url_scheme from the Rack env to determine the default

  • :host (String)

    The host to use, uses SERVER_NAME form the Rack env for the default

  • :port (String, Numeric)

    The port to use, users SERVER_PORT from the Rack env for the default

Returns:

  • (String)

    The absolute url



159
160
161
# File 'lib/rack/action.rb', line 159

def absolute_url(url, options={})
  URL.new(env, url, options).to_absolute
end

#callArray<Numeric, Hash, #each>

This is the main method responsible for generating a Rack response. You typically won’t need to override this method or call it directly. First this will run the before filters for this action. If none of the before filters generate a response, this will call #respond to generate a response. All after filters for this action are called once the response is genenated. Finally the response is returned.

Returns:

  • (Array<Numeric, Hash, #each>)

    A Rack response



82
83
84
85
86
87
88
89
# File 'lib/rack/action.rb', line 82

def call
  log_call
  set_default_headers
  run_before_filters
  run_respond
  run_after_filters
  finish_response
end

#forbiddenObject

Convenience method to return a 403



133
134
135
# File 'lib/rack/action.rb', line 133

def forbidden
  respond_with 403
end

#formatObject



63
64
65
66
67
68
69
70
71
# File 'lib/rack/action.rb', line 63

def format
  if params[:format]
    params[:format]
  elsif env[HTTP_ACCEPT] == APPLICATION_JSON
    "json"
  else
    "html"
  end
end

#json(data = {}, options = {}) ⇒ String

This is a convenience method that sets the Content-Type headers and writes the JSON String to the response.

Parameters:

  • data (Hash) (defaults to: {})

    The data

  • options (Hash) (defaults to: {})

    The options

Options Hash (options):

  • :status (Fixnum)

    The response status code

Returns:

  • (String)

    The JSON



108
109
110
111
112
# File 'lib/rack/action.rb', line 108

def json(data={}, options={})
  response[CONTENT_TYPE] = APPLICATION_JSON
  response.status = options[:status] if options.has_key?(:status)
  response.write self.class.json_serializer.dump(data)
end

#not_foundObject

Convenience method to return a 404



128
129
130
# File 'lib/rack/action.rb', line 128

def not_found
  respond_with 404
end

#redirect_to(url, options = {}) ⇒ String

This is a convenience method that forms an absolute URL based on the url parameter, which can be a relative or absolute URL, and then sets the headers and the body appropriately to do a 302 redirect.

Returns:

  • (String)

    The absolute url

See Also:



120
121
122
123
124
125
# File 'lib/rack/action.rb', line 120

def redirect_to(url, options={})
  full_url = absolute_url(url, options)
  response[LOCATION] = full_url
  respond_with 302
  full_url
end

#respondString

This is the main method that you should override in your action. You can either write to the response during this method, or simply return a string, which will be written to the response if the response is still empty after this is called.

Returns:

  • (String)

    The Rack response or a String



97
98
99
# File 'lib/rack/action.rb', line 97

def respond
  DEFAULT_RESPONSE
end

#respond_with(status_code) ⇒ Object

This is a convenience method to set the response code and set the response so that it stops respond process.

Parameters:

  • status_code (Fixnum)

    The HTTP status code to use in the response



141
142
143
144
145
# File 'lib/rack/action.rb', line 141

def respond_with(status_code)
  response.status = status_code
  response.write ''
  nil
end