Sha256: f35c418a2c19b37ab7d3a48a801daffb94bacfd3b925d1171d51887f6c1a5c0a

Contents?: true

Size: 1.76 KB

Versions: 8

Compression:

Stored size: 1.76 KB

Contents

# frozen_string_literal: true

# Versioning
module Spec
  module Support
    module Helpers
      # Returns the path with options[:version] prefixed if options[:using] is :path.
      # Returns normal path otherwise.
      def versioned_path(**options)
        case options[:using]
        when :path
          File.join('/', options[:prefix] || '', options[:version], options[:path])
        when :param
          File.join('/', options[:prefix] || '', options[:path])
        when :header
          File.join('/', options[:prefix] || '', options[:path])
        when :accept_version_header
          File.join('/', options[:prefix] || '', options[:path])
        else
          raise ArgumentError.new("unknown versioning strategy: #{options[:using]}")
        end
      end

      def versioned_headers(**options)
        case options[:using]
        when :path
          {}  # no-op
        when :param
          {}  # no-op
        when :header
          {
            'HTTP_ACCEPT' => [
              "application/vnd.#{options[:vendor]}-#{options[:version]}",
              options[:format]
            ].compact.join('+')
          }
        when :accept_version_header
          {
            'HTTP_ACCEPT_VERSION' => options[:version].to_s
          }
        else
          raise ArgumentError.new("unknown versioning strategy: #{options[:using]}")
        end
      end

      def versioned_get(path, version_name, **version_options)
        path = versioned_path(**version_options.merge(version: version_name, path: path))
        headers = versioned_headers(**version_options.merge(version: version_name))
        params = {}
        params = { version_options[:parameter] => version_name } if version_options[:using] == :param
        get path, params, headers
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
grape-1.8.0 spec/support/versioned_helpers.rb
grape-1.7.1 spec/support/versioned_helpers.rb
grape-1.7.0 spec/support/versioned_helpers.rb
grape-1.6.2 spec/support/versioned_helpers.rb
grape-1.6.1 spec/support/versioned_helpers.rb
grape-1.6.0 spec/support/versioned_helpers.rb
grape-1.5.3 spec/support/versioned_helpers.rb
grape-1.5.2 spec/support/versioned_helpers.rb