Sha256: f9afe0f6749729545d81f39e8290cc5ec1e9c3fe363d0bd1e49d0fdd41d24880

Contents?: true

Size: 1.68 KB

Versions: 15

Compression:

Stored size: 1.68 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)
        if already_encoded?(url)
          res = URI.parse(url.gsub("%23", "#")) rescue URI.parse('/')
        else
          res = URI.parse(URI.encode(url).gsub("%23", "#")) rescue URI.parse('/')
        end
        res
      end

      def already_encoded?(url)
        URI.decode(url) != url rescue true
      end

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

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
ajax-1.1.7 lib/ajax/helpers/url_helper.rb
ajax-1.1.6 lib/ajax/helpers/url_helper.rb
ajax-1.1.5 lib/ajax/helpers/url_helper.rb
ajax-1.1.4 lib/ajax/helpers/url_helper.rb
ajax-1.1.3 lib/ajax/helpers/url_helper.rb
ajax-1.1.2 lib/ajax/helpers/url_helper.rb
ajax-1.1.1 lib/ajax/helpers/url_helper.rb
ajax-1.1.0 lib/ajax/helpers/url_helper.rb
ajax-1.0.8 lib/ajax/helpers/url_helper.rb
ajax-1.0.7 lib/ajax/helpers/url_helper.rb
ajax-1.0.6 lib/ajax/helpers/url_helper.rb
ajax-1.0.5 lib/ajax/helpers/url_helper.rb
ajax-1.0.4 lib/ajax/helpers/url_helper.rb
ajax-1.0.3 lib/ajax/helpers/url_helper.rb
ajax-1.0.2 lib/ajax/helpers/url_helper.rb