Sha256: 57d03aef104fc8e7afcaf748f3b8fd0cfef702c1a08fda789c6e57ab8b41921e

Contents?: true

Size: 1.28 KB

Versions: 2

Compression:

Stored size: 1.28 KB

Contents

# This modifies RocketPants routing to use
# accept headers and namespaces instead of a :version parameter.
#
# (see RocketPants::Routing)
require 'rocket_pants/routing'

module RocketShorts
  module Routing

    # Scopes a set of given api routes, allowing for option versions.
    # @param [Hash] options options to pass through to the route e.g. `:module`.
    # @option options [Array<Integer>, Integer] :versions the versions to support
    # @option options [Array<Integer>, Integer] :version the single version to support
    # @raise [ArgumentError] raised when the version isn't provided.
    def rocket_pants(options = {}, &blk)
      versions = (Array(options.delete(:versions)) + Array(options.delete(:version))).flatten.map(&:to_s)
      versions.each do |version|
        raise ArgumentError, "Got invalid version: '#{version}'" unless version =~ /\A\d+\Z/
      end
      versions_regexp = /(#{versions.uniq.join("|")})/
      raise ArgumentError, 'please provide atleast one version' if versions.empty?
      options = options.deep_merge({
        constraints: RoutingConstraints.new(versions: versions_regexp, default: !!options.delete(:default)),
      })
      namespace :api, defaults: {format: 'json'} do
        scope options, &blk
      end
    end
    alias api rocket_pants

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rocket_shorts-0.0.2 lib/rocket_shorts/routing.rb
rocket_shorts-0.0.1 lib/rocket_shorts/routing.rb