Sha256: 6f904df27d4950fd537b9a3bf3684156c2fc4f8d0792e64bd8332cda7ec09b32

Contents?: true

Size: 1.29 KB

Versions: 1

Compression:

Stored size: 1.29 KB

Contents

# frozen_string_literal: true

require 'api_sigv2/spec_support/path_builder'
require 'api_sigv2/spec_support/headers_builder'

module ApiSigv2
  module SpecSupport
    module Helper
      include Rack::Test::Methods

      def app
        Rails.app_class
      end

      def get_with_signature(client, *args)
        with_signature(:get, client.api_key, client.api_secret, *args)
      end

      def post_with_signature(client, *args)
        with_signature(:post, client.api_key, client.api_secret, *args)
      end

      def put_with_signature(client, *args)
        with_signature(:put, client.api_key, client.api_secret, *args)
      end

      alias patch_with_signature put_with_signature

      def delete_with_signature(client, *args)
        with_signature(:delete, client.api_key, client.api_secret, *args)
      end

      private

      def with_signature(http_method, api_key, secret, action_name, params = {})
        custom_headers = (params.delete(:headers) || {})
        path = PathBuilder.new(controller, action_name, params).path

        signature = Signer.new(api_key, secret).sign_request(
          http_method: http_method.to_s.upcase,
          url: path,
          headers: custom_headers
        )

        send(http_method, path, params, signature.headers)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
galetahub-api-sigv2-1.0.0 lib/api_sigv2/spec_support/helper.rb