Module: Sinatra::Helpers

### Home Classes Methods

### In Files

- lib/sinatra/base.rb

### Namespace

- CLASS Sinatra::Helpers::Stream

### Methods

- #attachment
- #body
- #content_type
- #error
- #headers
- #logger
- #mime_type
- #not_found
- #redirect
- #send_file
- #session
- #status
- #to
- #uri
- #url

### Files

- LICENSE

### Class/Module Index

Quicksearch

- Sinatra
- Sinatra::Application
- Sinatra::CommonLogger
- Sinatra::ExtendedRack
- Sinatra::Helpers
- Sinatra::Helpers::Stream
- Sinatra::Helpers::Stream::Application
- Sinatra::Helpers::Stream::Base
- Sinatra::Helpers::Stream::Templates
- Sinatra::Helpers::Stream::Templates::ContentTyped
- Sinatra::Helpers::Stream::Wrapper
- Sinatra::Request
- Sinatra::Request::AcceptEntry
- Sinatra::Response
- Sinatra::ShowExceptions
- Rack
- Rack::Builder

No matching classes.

# Sinatra::Helpers

Methods available to routes, before/after filters, and views.

### Public Instance Methods

attachment(filename = nil, disposition = 'attachment') click to toggle source

Set the Content-Disposition to “attachment” with the specified filename, instructing the user agents to prompt to save.

    # File lib/sinatra/base.rb, line 340
    def attachment(filename = nil, disposition = 'attachment')
      response['Content-Disposition'] = disposition.to_s
      if filename
        params = '; filename="%s"' % File.basename(filename)
        response['Content-Disposition'] << params
        ext = File.extname(filename)
        content_type(ext) unless response['Content-Type'] or ext.empty?
      end
    end

body(value = nil, &block) click to toggle source

Set or retrieve the response body. When a block is given, evaluation is deferred until the body is read with each.

    # File lib/sinatra/base.rb, line 236
    def body(value = nil, &block)
      if block_given?
        def block.each; yield(call) end
        response.body = block
      elsif value
        headers.delete 'Content-Length' unless request.head? || value.is_a?(Rack::File) || value.is_a?(Stream)
        response.body = value
      else
        response.body
      end
    end

content_type(type = nil, params = {}) click to toggle source

Set the Content-Type of the response body given a media type or file extension.

    # File lib/sinatra/base.rb, line 318
    def content_type(type = nil, params = {})
      return response['Content-Type'] unless type
      default = params.delete :default
      mime_type = mime_type(type) || default
      fail "Unknown media type: %p" % type if mime_type.nil?
      mime_type = mime_type.dup
      unless params.include? :charset or settings.add_charset.all? { |p| not p === mime_type }
        params[:charset] = params.delete('charset') || settings.default_encoding
      end
      params.delete :charset if mime_type.include? 'charset'
      unless params.empty?
        mime_type << (mime_type.include?(';') ? ', ' : ';')
        mime_type << params.map do |key, val|
          val = val.inspect if val =~ /[";,]/
          "#{key}=#{val}"
        end.join(', ')
      end
      response['Content-Type'] = mime_type
    end

error(code, body = nil) click to toggle source

Halt processing and return the error status provided.

    # File lib/sinatra/base.rb, line 284
    def error(code, body = nil)
      code, body    = 500, code.to_str if code.respond_to? :to_str
      response.body = body unless body.nil?
      halt code
    end

headers(hash = nil) click to toggle source

Set multiple response headers with Hash.

    # File lib/sinatra/base.rb, line 296
    def headers(hash = nil)
      response.headers.merge! hash if hash
      response.headers
    end

logger() click to toggle source

Access shared logger object.

    # File lib/sinatra/base.rb, line 307
    def logger
      request.logger
    end

mime_type(type) click to toggle source

Look up a media type by file extension in Rack’s mime registry.

    # File lib/sinatra/base.rb, line 312
    def mime_type(type)
      Base.mime_type(type)
    end

not_found(body = nil) click to toggle source

Halt processing and return a 404 Not Found.

    # File lib/sinatra/base.rb, line 291
    def not_found(body = nil)
      error 404, body
    end

redirect(uri, *args) click to toggle source

Halt processing and redirect to the URI provided.

    # File lib/sinatra/base.rb, line 249
    def redirect(uri, *args)
      if env['HTTP_VERSION'] == 'HTTP/1.1' and env["REQUEST_METHOD"] != 'GET'
        status 303
      else
        status 302
      end

      # According to RFC 2616 section 14.30, "the field value consists of a
      # single absolute URI"
      response['Location'] = uri(uri.to_s, settings.absolute_redirects?, settings.prefixed_redirects?)
      halt(*args)
    end

send_file(path, opts = {}) click to toggle source

Use the contents of the file at `path` as the response body.

    # File lib/sinatra/base.rb, line 351
    def send_file(path, opts = {})
      if opts[:type] or not response['Content-Type']
        content_type opts[:type] || File.extname(path), :default => 'application/octet-stream'
      end

      disposition = opts[:disposition]
      filename    = opts[:filename]
      disposition = 'attachment' if disposition.nil? and filename
      filename    = path         if filename.nil?
      attachment(filename, disposition) if disposition

      last_modified opts[:last_modified] if opts[:last_modified]

      file      = Rack::File.new nil
      file.path = path
      result    = file.serving env
      result[1].each { |k,v| headers[k] ||= v }
      headers['Content-Length'] = result[1]['Content-Length']
      opts[:status] &&= Integer(opts[:status])
      halt opts[:status] || result[0], result[2]
    rescue Errno::ENOENT
      not_found
    end

session() click to toggle source

Access the underlying Rack session.

    # File lib/sinatra/base.rb, line 302
    def session
      request.session
    end

status(value = nil) click to toggle source

Set or retrieve the response status code.

    # File lib/sinatra/base.rb, line 229
    def status(value = nil)
      response.status = value if value
      response.status
    end

to(addr = nil, absolute = true, add_script_name = true) click to toggle source
Alias for: uri
uri(addr = nil, absolute = true, add_script_name = true) click to toggle source

Generates the absolute URI for a given path in the app. Takes Rack routers and reverse proxies into account.

    # File lib/sinatra/base.rb, line 264
    def uri(addr = nil, absolute = true, add_script_name = true)
      return addr if addr =~ /\A[A-z][A-z0-9\+\.\-]*:/
      uri = [host = ""]
      if absolute
        host << "http#{'s' if request.secure?}://"
        if request.forwarded? or request.port != (request.secure? ? 443 : 80)
          host << request.host_with_port
        else
          host << request.host
        end
      end
      uri << request.script_name.to_s if add_script_name
      uri << (addr ? addr : request.path_info).to_s
      File.join uri
    end

Also aliased as: url, to
url(addr = nil, absolute = true, add_script_name = true) click to toggle source
Alias for: uri

[Validate]

Generated with the Darkfish Rdoc Generator 2.