Skip to content
This repository
tree: 147659328a
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 67 lines (60 sloc) 2.973 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
module Versionist
  module Routing
    # Allows you to constrain routes to specific versions of your api using versioning strategies.
    # Supported formats:
    # api_version(:module => "v1", :header => "Accept", :value => "application/vnd.mycompany.com-v1")
    # api_version(:module => "v2__3__4", :path => "/v2.3.4")
    # api_version(:module => "v20120317", :parameter => "version", :value => "v20120317")
    #
    # Specifying default version:
    # api_version(:module => "v3__0__0", :header => "API-VERSION", :value => "v3.0.0", :default => true)
    def api_version(config, &block)
      raise ArgumentError, "you must pass a configuration Hash to api_version" if config.nil? || !config.is_a?(Hash)
      raise ArgumentError, "you must specify :module in configuration Hash passed to api_version" if !config.has_key?(:module)
      raise ArgumentError, "you must specify :header, :path, or :parameter in configuration Hash passed to api_version" if !config.has_key?(:header) && !config.has_key?(:path) && !config.has_key?(:parameter)
      raise ArgumentError, ":defaults must be a Hash" if config.has_key?(:defaults) && !config[:defaults].is_a?(Hash)
      if config.has_key?(:header)
        return configure_header(config, &block)
      elsif config.has_key?(:path)
        return configure_path(config, &block)
      elsif config.has_key?(:parameter)
        configure_parameter(config, &block)
      end
    end


    private

    def configure_header(config, &block)
      header = Versionist::VersioningStrategy::Header.new(config)
      route_hash = {:module => config[:module], :constraints => header}
      route_hash.merge!({:defaults => config[:defaults]}) if config.has_key?(:defaults)
      scope(route_hash, &block)
    end

    def configure_path(config, &block)
      path = Versionist::VersioningStrategy::Path.new(config)
      # Use the :as option and strip out non-word characters from the path to avoid this:
      # https://github.com/rails/rails/issues/3224
      config[:path].slice!(0) if config[:path] =~ /^\//
      route_hash = {:module => config[:module], :as => config[:path].gsub(/\W/, '_')}
      route_hash.merge!({:defaults => config[:defaults]}) if config.has_key?(:defaults)
      namespace(config[:path], route_hash, &block)
      if path.default?
        scope(route_hash, &block)
      end
    end

    def configure_parameter(config, &block)
      parameter = Versionist::VersioningStrategy::Parameter.new(config)
      route_hash = {:module => config[:module], :constraints => parameter}
      route_hash.merge!({:defaults => config[:defaults]}) if config.has_key?(:defaults)
      scope(route_hash, &block)
    end
  end
end

# Hook to clear versionist cached data when routes are reloaded
module Rails
  class Application #:nodoc:
    def reload_routes_with_versionist!
      Versionist.configuration.clear!
      reload_routes_without_versionist!
    end
    alias_method_chain :reload_routes!, :versionist
  end
end
Something went wrong with that request. Please try again.