Module: Rack::RespondTo

Defined in:
lib/rack/respond_to.rb

Overview

Based on Rails's API, and sinatra-respond_to (http://github.com/cehoffman/sinatra-respond_to)

See examples/ directory for code examples.

Defined Under Namespace

Modules: ClassMethods, Helpers, InstanceMethods Classes: Format

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.env ⇒ Object

Assign the environment directly to fetch the requested media types from env ('Accept:' request header).

Example
def call(env)
Rack::RespondTo.env = env
#...
end


21
22
23
# File 'lib/rack/respond_to.rb', line 21

def env
  @env
end

.media_types ⇒ Object

Requested media types, in preferencial order

Examples
RespondTo.env['HTTP_ACCEPT'] #=> 'text/html,application/xml'
RespondTo.media_types        #=> ['text/html', 'application/xml']

RespondTo.env['HTTP_ACCEPT'] #=> 'text/html;q=0.7,application/xml;q=0.9,application/json;q=0.8'
RespondTo.media_types        #=> ['application/xml', 'application/json', 'text/html']


30
31
32
# File 'lib/rack/respond_to.rb', line 30

def media_types
  @media_types
end

.selected_media_type ⇒ Object Also known as: selected_mime_type

Contains the media type that was responded to. Set after the respond_to block is called.

Useful for setting the response's Content-Type:

[200, {'Content-Type' => RespondTo.selected_media_type}, [body]]


41
42
43
# File 'lib/rack/respond_to.rb', line 41

def selected_media_type
  @selected_media_type
end

Class Method Details

.included(base) ⇒ Object

:nodoc:



45
46
47
48
49
50
# File 'lib/rack/respond_to.rb', line 45

def included(base) #:nodoc:
  base.extend(ClassMethods)
  base.class_eval do
    include InstanceMethods
  end
end

.MediaType(format) ⇒ Object Also known as: MimeType

Cast format to media type

Example
RespondTo::MediaType('html') #=> 'text/html'
RespondTo::MediaType('htm')  #=> 'text/html'


59
60
61
# File 'lib/rack/respond_to.rb', line 59

def MediaType(format)
  Rack::Mime.mime_type(format.sub(/^\./,'').insert(0,'.'))
end

.mime_types ⇒ Object

If used completely standalone, you can assign the requested media types directly.

Example
RespondTo.media_types = ['application/xml']


32
33
34
# File 'lib/rack/respond_to.rb', line 32

def media_types
  @media_types
end