module Padrino::Routing::InstanceMethods

Instance methods related to recognizing and processing routes and serving static files.

Public Instance Methods

absolute_url(*args) click to toggle source

Returns absolute url. Calls Sinatra::Helpers#uri to generate protocol version, hostname and port.

@example

absolute_url(:show, :id => 1)  # => http://example.com/show?id=1
absolute_url(:show, 24)        # => https://example.com/admin/show/24
absolute_url('/foo/bar')       # => https://example.com/admin/foo/bar
absolute_url('baz')            # => https://example.com/admin/foo/baz
# File lib/padrino-core/application/routing.rb, line 819
def absolute_url(*args)
  url_path = args.shift
  if url_path.is_a?(String) && !url_path.start_with?('/')
    url_path = request.env['PATH_INFO'].rpartition('/').first << '/' << url_path
  end
  uri url(url_path, *args), true, false
end
content_type(type=nil, params={}) click to toggle source

Return the request format, this is useful when we need to respond to a given Content-Type.

@param [Symbol, nil] type

@param [Hash] params

@example

get :index, :provides => :any do
  case content_type
    when :js    then ...
    when :json  then ...
    when :html  then ...
  end
end
Calls superclass method
# File lib/padrino-core/application/routing.rb, line 897
def content_type(type=nil, params={})
  return @_content_type unless type
  super(type, params)
  @_content_type = type
end
current_path(*path_params) click to toggle source

Returns the current path within a route from specified path_params.

# File lib/padrino-core/application/routing.rb, line 834
def current_path(*path_params)
  if path_params.last.is_a?(Hash)
    path_params[-1] = params.merge(path_params[-1])
  else
    path_params << params
  end

  path_params[-1] = Utils.symbolize_keys(path_params[-1])
  @route.path(*path_params)
end
recognize_path(path) click to toggle source
# File lib/padrino-core/application/routing.rb, line 827
def recognize_path(path)
  settings.recognize_path(path)
end
route() click to toggle source

Returns the current route

@example

-if route.controller == :press
  %li=show_article
# File lib/padrino-core/application/routing.rb, line 852
def route
  @route
end
static!(options = {}) click to toggle source

Method for deliver static files.

# File lib/padrino-core/application/routing.rb, line 872
def static!(options = {})
  if path = static_file?(request.path_info)
    env['sinatra.static_file'] = path
    cache_control(*settings.static_cache_control) if settings.static_cache_control?
    send_file(path, options)
  end
end
static_file?(path_info) click to toggle source

This is mostly just a helper so request.path_info isn't changed when serving files from the public directory.

# File lib/padrino-core/application/routing.rb, line 860
def static_file?(path_info)
  return unless public_dir = settings.public_folder
  public_dir = File.expand_path(public_dir)
  path = File.expand_path(public_dir + unescape(path_info))
  return unless path.start_with?(public_dir)
  return unless File.file?(path)
  return path
end
url(*args) click to toggle source

Instance method for URL generation.

@example

url(:show, :id => 1)
url(:show, :name => :test)
url(:show, 1)
url("/foo", false, false)

@see Padrino::Routing::ClassMethods#url

Calls superclass method
# File lib/padrino-core/application/routing.rb, line 793
def url(*args)
  if args.first.is_a?(String)
    url_path = settings.rebase_url(args.shift)
    if args.empty?
      url_path
    else
      # Delegate sinatra-style urls to Sinatra. Ex: url("/foo", false, false)
      # http://www.sinatrarb.com/intro#Generating%20URLs
      super url_path, *args
    end
  else
    # Delegate to Padrino named route URL generation.
    settings.url(*args)
  end
end
Also aliased as: url_for
url_for(*args)
Alias for: url

Private Instance Methods

captures_from_params(params) click to toggle source
# File lib/padrino-core/application/routing.rb, line 1009
def captures_from_params(params)
  if params[:captures].instance_of?(Array) && !params[:captures].empty?
    params.delete(:captures)
  else
    params.values_at(*route.matcher.names).flatten
  end
end
dispatch!() click to toggle source
# File lib/padrino-core/application/routing.rb, line 931
def dispatch!
  @params = defined?(Sinatra::IndifferentHash) ? Sinatra::IndifferentHash[@request.params] : indifferent_params(@request.params)
  force_encoding(@params)
  invoke do
    static! if settings.static? && (request.get? || request.head?)
    route!
  end
rescue ::Exception => boom
  filter! :before if boom.kind_of? ::Sinatra::NotFound
  invoke { @boom_handled = handle_exception!(boom) }
ensure
  @boom_handled or begin
    filter! :after  unless env['sinatra.static_file']
  rescue ::Exception => boom
    invoke { handle_exception!(boom) } unless @env['sinatra.error']
  end
end
filter!(type, base=settings) click to toggle source
# File lib/padrino-core/application/routing.rb, line 926
def filter!(type, base=settings)
  filter! type, base.superclass if base.superclass.respond_to?(:filters)
  base.filters[type].each { |block| instance_eval(&block) }
end
invoke_route(route, params, first_time) click to toggle source
# File lib/padrino-core/application/routing.rb, line 980
def invoke_route(route, params, first_time)
  @_response_buffer = nil
  @route = request.route_obj = route
  captured_params = captures_from_params(params)

  # Should not overwrite params by given query
  @params.merge!(params) do |key, original, newval|
    @route.significant_variable_names.include?(key) ? newval : original
  end unless params.empty?

  @params[:format] = params[:format] if params[:format]
  @params[:captures] = captured_params if !captured_params.empty? && route.path.is_a?(Regexp)

  filter! :before if first_time

  catch(:pass) do
    begin
        (route.before_filters - settings.filters[:before]).each{|block| instance_eval(&block) }
        @layout = route.use_layout if route.use_layout
        route.custom_conditions.each {|block| pass if block.bind(self).call == false }
        route_response = route.block[self, captured_params]
        @_response_buffer = route_response.instance_of?(Array) ? route_response.last : route_response
        halt(route_response)
    ensure
      (route.after_filters - settings.filters[:after]).each {|block| instance_eval(&block) }
    end
  end
end
mime_symbol(media_type) click to toggle source
# File lib/padrino-core/application/routing.rb, line 922
def mime_symbol(media_type)
  ::Rack::Mime::MIME_TYPES.key(media_type).sub(/\./,'').to_sym
end
provides_any?(formats) click to toggle source
# File lib/padrino-core/application/routing.rb, line 905
def provides_any?(formats)
  accepted_format = formats.first
  type = accepted_format ? mime_symbol(accepted_format) : :html
  content_type(CONTENT_TYPE_ALIASES[type] || type)
end
provides_format?(types, format) click to toggle source
# File lib/padrino-core/application/routing.rb, line 911
def provides_format?(types, format)
  if ([:any, format] & types).empty?
    # Per rfc2616-sec14:
    # Answer with 406 if accept is given but types to not match any provided type.
    halt 406 if settings.respond_to?(:treat_format_as_accept) && settings.treat_format_as_accept
    false
  else
    content_type(format || :html)
  end
end
route!(base = settings, pass_block = nil) click to toggle source
# File lib/padrino-core/application/routing.rb, line 949
def route!(base = settings, pass_block = nil)
  Thread.current['padrino.instance'] = self
  first_time = true

  routes = base.compiled_router.call(@request) do |route, params|
    next if route.user_agent && !(route.user_agent =~ @request.user_agent)
    original_params, parent_layout = @params.dup, @layout
    returned_pass_block = invoke_route(route, params, first_time)
    pass_block = returned_pass_block if returned_pass_block
    first_time = false if first_time
    @params, @layout = original_params, parent_layout
  end

  unless routes.empty?
    verb = request.request_method
    candidacies, allows = routes.partition{|route| route.verb == verb }
    if candidacies.empty?
      response["Allows"] = allows.map(&:verb).join(", ")
      halt 405
    end
  end

  if base.superclass.respond_to?(:router)
    route!(base.superclass, pass_block)
    return
  end

  route_eval(&pass_block) if pass_block
  route_missing
end