Sha256: 561b255672dedd973200af534ddd2810a0909ebf2cd128b9233e6c3a8650e65f

Contents?: true

Size: 1.45 KB

Versions: 2

Compression:

Stored size: 1.45 KB

Contents

module Ajax
  module Helpers
    module UrlHelper

      # Return a boolean indicating whether the given URL points to the
      # root path.
      def url_is_root?(url)
        !!(encode_and_parse_url(url).path =~ %r[^\/?$])
      end

      # The URL is hashed if the fragment part starts with a /
      #
      # For example, http://lol.com#/Rihanna
      def is_hashed_url?(url)
        !!(encode_and_parse_url(url).fragment=~ %r[^\/])
      end

      # Return a hashed URL using the fragment of <tt>url</tt>
      def hashed_url_from_fragment(url)
        url_host(url) + ('/#/' + (encode_and_parse_url(url).fragment || '')).gsub(/\/\//, '/')
      end

      # Return a traditional URL from the fragment of <tt>url</tt>
      def traditional_url_from_fragment(url)
        url_host(url) + ('/' + (encode_and_parse_url(url).fragment || '')).gsub(/\/\//, '/')
      end

      # Return a hashed URL formed from a traditional <tt>url</tt>
      def hashed_url_from_traditional(url)
        uri = encode_and_parse_url(url)
        hashed_url = url_host(url) + ('/#/' + (uri.path || '')).gsub(/\/\//, '/')
        hashed_url += ('?' + uri.query) unless uri.query.nil?
        hashed_url
      end

      protected

      def encode_and_parse_url(url)
        URI.parse(URI.encode(url).gsub("%23", "#")) rescue URI.parse('/')
      end

      def url_host(url)
        if url.match(/^(\w+\:\/\/[^\/]+)\/?/)
          $1
        else
          ''
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ajax-0.1.7 lib/ajax/helpers/url_helper.rb
ajax-0.1.6 lib/ajax/helpers/url_helper.rb