Sha256: 7b1b868fbff8dcb41e43f8f8f5b330cde0c356d6d878193d8217fb88396ec55e

Contents?: true

Size: 1.11 KB

Versions: 1

Compression:

Stored size: 1.11 KB

Contents

require "http_signatures"
require "net/http"
require "time"

module Stream

  module SignedRequest
    def make_signed_request(method, relative_url, params={}, data={})
        query_params = self.make_query_params(params)
        context = HttpSignatures::Context.new(
            keys: {@api_key => @api_secret},
            algorithm: "hmac-sha256",
            headers: ["(request-target)", "Date"],
        )
        method_map = {
            :get => Net::HTTP::Get,
            :delete => Net::HTTP::Delete,
            :put => Net::HTTP::Put,
            :post => Net::HTTP::Post,
        }
        request_date = Time.now.rfc822
        message = method_map[method].new(
          "#{self.get_http_client.base_path}#{relative_url}?#{URI.encode_www_form(query_params)}",
          'Date' => request_date,
        )
        context.signer.sign(message)
        headers = {
            'Authorization' => message["Signature"],
            'Date' => request_date,
            'X-Api-Key' => self.api_key
        }
        self.get_http_client.make_http_request(method, relative_url, query_params, data, headers)
    end
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
stream-ruby-2.2 lib/stream/signedrequest.rb