Sha256: a93b9d26324f0d140653dfbfb84d4d96beb36dac22c6979bc47635bfacdbd7a1

Contents?: true

Size: 955 Bytes

Versions: 6

Compression:

Stored size: 955 Bytes

Contents

# frozen_string_literal: true

module ApiSignature
  module SpecSupport
    class PathBuilder
      attr_reader :controller, :action_name, :params

      PRIMARY_KEYS = [:id, :token].freeze

      def initialize(controller, action_name, params = {})
        @controller = controller
        @action_name = action_name
        @params = params
      end

      def path
        if params[:path].present?
          hash = params.delete(:path)
          url_options.merge!(hash)
          params.merge!(hash)
        end

        controller.url_for(url_options)
      end

      private

      def url_options
        @url_options ||= {
          action: action_name,
          controller: controller.controller_path,
          only_path: true
        }.merge(key_options || {})
      end

      def key_options
        key = (params.keys.map(&:to_sym) & PRIMARY_KEYS).first
        { key => params[key] } if params[key].present?
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
api_signature-1.0.0 lib/api_signature/spec_support/path_builder.rb
api_signature-0.1.5 lib/api_signature/spec_support/path_builder.rb
api_signature-0.1.4 lib/api_signature/spec_support/path_builder.rb
api_signature-0.1.3 lib/api_signature/spec_support/path_builder.rb
api_signature-0.1.2 lib/api_signature/spec_support/path_builder.rb
api_signature-0.1.1 lib/api_signature/spec_support/path_builder.rb