Sha256: 21a76de1b5a8491bb36f22ec442f3e0448ed458c7c33c14b97a21779e7b0aa68

Contents?: true

Size: 1.71 KB

Versions: 24

Compression:

Stored size: 1.71 KB

Contents

require 'uri'

module Sinatra
  module UrlForHelper
    # Construct a link to +url_fragment+, which should be given relative to
    # the base of this Sinatra app.  The mode should be either
    # <code>:path_only</code>, which will generate an absolute path within
    # the current domain (the default), or <code>:full</code>, 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"
    #--
    # See README.rdoc for a list of some of the people who helped me clean
    # up earlier versions of this code.
    def url_for url_fragment, mode=:path_only
      case mode
      when :path_only
        base = request.script_name
      when :full
        scheme = request.scheme
        if (scheme == 'http' && request.port == 80 ||
            scheme == 'https' && request.port == 443)
          port = ""
        else
          port = ":#{request.port}"
        end
        request_host = HOSTNAME ? HOSTNAME : request.host
        base = "#{scheme}://#{request_host}#{port}#{request.script_name}"
      else
        raise TypeError, "Unknown url_for mode #{mode}"
      end
      url_escape = URI.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(/^#{base}/) or url_escape.match(/^http/)
        url_escape
      else
        "#{base}#{url_escape}"
      end
    end

    def root_url
      url_for '/'
    end
  end



  helpers UrlForHelper
end

Version data entries

24 entries across 24 versions & 4 rubygems

Version Path
steamcannon-deltacloud-core-0.1.2.1 lib/sinatra/url_for.rb
steamcannon-deltacloud-core-0.1.1.3 lib/sinatra/url_for.rb
deltacloud-core-0.1.2 lib/sinatra/url_for.rb
steamcannon-deltacloud-core-0.1.1.2 lib/sinatra/url_for.rb
steamcannon-deltacloud-core-0.1.1.1 lib/sinatra/url_for.rb
deltacloud-core-0.1.1 lib/sinatra/url_for.rb
deltacloud-core-0.1.0 lib/sinatra/url_for.rb
sinatra-rabbit-0.0.2 lib/sinatra/rabbit/url_for.rb
sinatra-rabbit-0.0.1 lib/sinatra/rabbit/url_for.rb
deltacloud-core-0.0.9 lib/sinatra/url_for.rb
steamcannon-deltacloud-core-0.0.8.1-java lib/sinatra/url_for.rb
steamcannon-deltacloud-core-0.0.8.1 lib/sinatra/url_for.rb
deltacloud-core-0.0.8 lib/sinatra/url_for.rb
steamcannon-deltacloud-core-0.0.7.2 lib/sinatra/url_for.rb
steamcannon-deltacloud-core-0.0.7.2-java lib/sinatra/url_for.rb
steamcannon-deltacloud-core-0.0.7.1 lib/sinatra/url_for.rb
steamcannon-deltacloud-core-0.0.7.1-java lib/sinatra/url_for.rb
deltacloud-core-0.0.7 lib/sinatra/url_for.rb
bbrowning-deltacloud-core-0.0.6.1-java lib/sinatra/url_for.rb
bbrowning-deltacloud-core-0.0.6-java lib/sinatra/url_for.rb