Sha256: 8b5829ad2523aafb2624f18703ddb258c6262073152b1463dd630e649f4b5ec7

Contents?: true

Size: 1.27 KB

Versions: 10

Compression:

Stored size: 1.27 KB

Contents

require 'routing_filter/base'

module RoutingFilter
  class ForceExtension < Base
    attr_reader :extension, :exclude

    def initialize(*args)
      super
      @extension ||= 'html'
      @exclude = %r(^(http.?://[^/]+)?\/?$) if @exclude.nil?
    end

    def around_recognize(path, env, &block)
      extract_extension!(path) unless excluded?(path)
      yield(path, env)
    end

    def around_generate(*args, &block)
      returning yield do |result|
        url = result.is_a?(Array) ? result.first : result
        append_extension!(url) if append_extension?(url)
      end
    end

    protected

      def extract_extension!(path)
        path.sub! /\.#{extension}$/, ''
        $1
      end
      
      def append_extension?(url)
        !(url.blank? || excluded?(url) || mime_extension?(url))
      end
      
      def excluded?(url)
        case exclude
        when Regexp
          url =~ exclude
        when Proc
          exclude.call(url)
        end
      end
      
      def mime_extension?(url)
        url =~ /\.#{Mime::EXTENSION_LOOKUP.keys.join('|')}(\?|$)/
      end
      
      def append_extension!(url)
        url.replace url.sub(/(\?|$)/, ".#{extension}\\1")
      end

      def append_page!(url, page)
        url.replace "#{url}/pages/#{page}"
      end
  end
end

Version data entries

10 entries across 10 versions & 6 rubygems

Version Path
strikeroff-routing-filter-0.0.1 lib/routing_filter/force_extension.rb
strikeroff-routing-filter-0.0.2 lib/routing_filter/force_extension.rb
svenfuchs-routing-filter-0.0.1 lib/routing_filter/force_extension.rb
trollied-0.1.2 test/full_2_3_5_app_with_tests/vendor/plugins/routing-filter/lib/routing_filter/force_extension.rb
trollied-0.1.1 test/full_2_3_5_app_with_tests/vendor/plugins/routing-filter/lib/routing_filter/force_extension.rb
trollied-0.1.0 test/full_2_3_5_app_with_tests/vendor/plugins/routing-filter/lib/routing_filter/force_extension.rb
mongo_translatable-0.1.0 test/full_2_3_5_app_with_tests/vendor/plugins/routing-filter/lib/routing_filter/force_extension.rb
oembed_provider-0.1.1 test/full_2_3_5_app_with_tests/vendor/plugins/routing-filter/lib/routing_filter/force_extension.rb
oembed_provider-0.1.0 test/full_2_3_5_app_with_tests/vendor/plugins/routing-filter/lib/routing_filter/force_extension.rb
routing-filter-0.0.1 lib/routing_filter/force_extension.rb