123456789_123456789_123456789_123456789_123456789_

Module: Sinatra::LinkHeader

Relationships & Source Files
Defined in: sinatra-contrib/lib/sinatra/link_header.rb

Overview

Sinatra::LinkHeader adds a set of helper methods to generate link HTML tags and their corresponding Link HTTP headers.

Usage

Once you had set up the helpers in your application (see below), you will be able to call the following methods from inside your route handlers, filters and templates:

#prefetch:: Sets the Link HTTP headers and returns HTML tags to prefetch the given resources.

#stylesheet:: Sets the Link HTTP headers and returns HTML tags to use the given stylesheets.

#link:: Sets the Link HTTP headers and returns the corresponding HTML tags for the given resources.

#link_headers:: Returns the corresponding HTML tags for the current Link HTTP headers.

Classic Application

In a classic application simply require the helpers, and start using them:

require "sinatra"
require "sinatra/link_header"

# The rest of your classic application code goes here...

Modular Application

In a modular application you need to require the helpers, and then tell the application you will use them:

require "sinatra/base"
require "sinatra/link_header"

class MyApp < Sinatra::Base
  helpers Sinatra::LinkHeader

  # The rest of your modular application code goes here...
end

Class Method Summary

Instance Method Summary

  • #link(*urls)

    Sets Link HTTP header and returns corresponding HTML tags.

  • #link_headers

    Takes the current value of th Link header(s) and generates HTML tags from it.

  • #prefetch(*urls)

    Sets Link HTTP header and returns HTML tags for telling the browser to prefetch given resources (only supported by Opera and Firefox at the moment).

  • #stylesheet(*urls)

    Sets Link HTTP header and returns HTML tags for using stylesheets.

Class Method Details

.registered(_base)

[ GitHub ]

  
# File 'sinatra-contrib/lib/sinatra/link_header.rb', line 126

def self.registered(_base)
  puts "WARNING: #{self} is a helpers module, not an extension."
end

Instance Method Details

#prefetch(*urls)

Sets Link HTTP header and returns HTML tags for telling the browser to prefetch given resources (only supported by Opera and Firefox at the moment).

[ GitHub ]

  
# File 'sinatra-contrib/lib/sinatra/link_header.rb', line 58

def prefetch(*urls)
  link(:prefetch, *urls)
end

#stylesheet(*urls)

Sets Link HTTP header and returns HTML tags for using stylesheets.

[ GitHub ]

  
# File 'sinatra-contrib/lib/sinatra/link_header.rb', line 64

def stylesheet(*urls)
  urls << {} unless urls.last.respond_to? :to_hash
  urls.last[:type] ||= mime_type(:css)
  link(:stylesheet, *urls)
end