module Markety
  # This class exists only to encapsulate the authentication header part of a soap request to marketo
  class AuthenticationHeader
    DIGEST = OpenSSL::Digest::Digest.new('sha1')

    def initialize(access_key, secret_key, time = DateTime.now)
      @access_key = access_key
      @secret_key = secret_key
      @time       = time
    end

    public
    # time should be a DateTime instance
    def set_time(time)
      @time = time
    end

    def get_mktows_user_id
      @access_key
    end

    def get_request_signature
      calculate_signature
    end

    def get_request_timestamp
      @time.to_s
    end

    def to_hash
      { "ns1:AuthenticationHeader" =>
        {
          "mktowsUserId"     => get_mktows_user_id,
          "requestSignature" => get_request_signature,
          "requestTimestamp" => get_request_timestamp
        }
      }
    end

    private

    def calculate_signature
      request_timestamp = get_request_timestamp
      string_to_encrypt = request_timestamp + @access_key
      OpenSSL::HMAC.hexdigest(DIGEST, @secret_key, string_to_encrypt)
    end
  end
end

module Markety
  # This class exists only to encapsulate the authentication header part of a soap request to marketo
  class AuthenticationHeader
    DIGEST = OpenSSL::Digest.new('sha1')

    def initialize(access_key, secret_key, time = DateTime.now)
      @access_key = access_key
      @secret_key = secret_key
      @time       = time
    end

    public
    # time should be a DateTime instance
    def set_time(time)
      @time = time
    end

    def get_mktows_user_id
      @access_key
    end

    def get_request_signature
      calculate_signature
    end

    def get_request_timestamp
      @time.to_s
    end

    def to_hash
      { "ns1:AuthenticationHeader" =>
        {
          "mktowsUserId"     => get_mktows_user_id,
          "requestSignature" => get_request_signature,
          "requestTimestamp" => get_request_timestamp
        }
      }
    end

    private

    def calculate_signature
      request_timestamp = get_request_timestamp
      string_to_encrypt = request_timestamp + @access_key
      OpenSSL::HMAC.hexdigest(DIGEST, @secret_key, string_to_encrypt)
    end
  end
end

Use OpenSSL::Digest rather than OpenSSL::Digest::Digest
Use OpenSSL::Digest rather than OpenSSL::Digest::Digest

This eliminates the `Digest::Digest is deprecated; use Digest` warning
when markety is required.
