module Sinatra::UrlForHelper

Public Instance Methods

api_url_for(url_fragment, mode=:path_only) click to toggle source
# File lib/deltacloud/helpers/url_helper.rb, line 52
def api_url_for(url_fragment, mode=:path_only)
  matrix_params = ''
  if request.params['api']
    matrix_params += ";provider=%s" % request.params['api']['provider'] if request.params['api']['provider']
    matrix_params += ";driver=%s" % request.params['api']['driver'] if request.params['api']['driver']
  end
  url_fragment = "/#{url_fragment}" unless url_fragment =~ %r^\// # There is no need to prefix URI with '/'
  if mode == :path_only
    url_for "#{settings.root_url}#{matrix_params}#{url_fragment}", mode
  else
    url_for "#{matrix_params}#{url_fragment}", :full
  end
end
method_missing(name, *args) click to toggle source
# File lib/deltacloud/helpers/url_helper.rb, line 32
def method_missing(name, *args)
  if name.to_s =~ %r^([\w\_]+)_url$/
    if args.size > 0
      t = $1
      if t.match(%r^(stop|reboot|start|attach|detach)_/)
        action = $1
        api_url_for(t.pluralize.split('_').last + '/' + args.first.to_s + '/' + action, :full)
      elsif t.match(%r^(destroy|update)_/)
        api_url_for(t.pluralize.split('_').last + '/' + args.first.to_s, :full)
      else
        api_url_for(t.pluralize, :full) + '/' + "#{args.first}"
      end
    else
      api_url_for($1, :full)
    end
  else
    super
  end
end
url_for(url_fragment, mode=:path_only) click to toggle source

Construct a link to url_fragment, which should be given relative to the base of this Sinatra app. The mode should be either :path_only, which will generate an absolute path within the current domain (the default), or :full, which will include the site name and port number. (The latter is typically necessary for links in RSS feeds.) Example usage:

url_for "/"            # Returns "/myapp/"
url_for "/foo"         # Returns "/myapp/foo"
url_for "/foo", :full  # Returns "http://example.com/myapp/foo"
# File lib/deltacloud/helpers/url_helper.rb, line 79
def url_for url_fragment, mode=:path_only
  case mode
  when :path_only
    base = request.script_name.empty? ? Deltacloud.default_frontend.root_url : request.script_name
  when :full
    scheme = request.scheme
    port = request.port
    request_host = request.host
    if request.env['HTTP_X_FORWARDED_FOR']
      scheme = request.env['HTTP_X_FORWARDED_SCHEME'] || scheme
      port = request.env['HTTP_X_FORWARDED_PORT']
      request_host = request.env['HTTP_X_FORWARDED_HOST']
    end
    if (port.nil? || port == "" ||
        (scheme == 'http' && port.to_s == '80') ||
        (scheme == 'https' && port.to_s == '443'))
      port = ""
    else
      port = ":#{port}"
    end
    base = "#{scheme}://#{request_host}#{port}#{request.script_name.empty? ? settings.config.root_url : request.script_name}"
  else
    raise TypeError, "Unknown url_for mode #{mode}"
  end
  uri_parser = URI.const_defined?(:Parser) ? URI::Parser.new : URI
  url_escape = uri_parser.escape(url_fragment)
  # Don't add the base fragment if url_for gets called more than once
  # per url or the url_fragment passed in is an absolute url
  if url_escape.match(%r^#{base}/) or url_escape.match(%r^http/)
    url_escape
  else
    "#{base}#{url_escape}"
  end
end