Module: RocketShorts::Routing

Defined in:
lib/rocket_shorts/routing.rb

Instance Method Summary collapse

Instance Method Details

#rocket_pants(options = {}, &blk) ⇒ Object Also known as: api

Scopes a set of given api routes, allowing for option versions.

Parameters:

  • options (Hash) (defaults to: {})

    options to pass through to the route e.g. ‘:module`.

Options Hash (options):

  • :versions (Array<Integer>, Integer)

    the versions to support

  • :version (Array<Integer>, Integer)

    the single version to support

Raises:

  • (ArgumentError)

    raised when the version isn’t provided.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/rocket_shorts/routing.rb', line 15

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