require 'rest_client'
require "base64"

$KEY="YOUR KEY GOES HERE"
$BASE64_SECRET="YOUR SECRET GOES HERE"
$TIMESTAMP=Time.now.to_i*1000
$QUERY = "{\"match_all\":{}}"

def hex_to_base64_digest(hexdigest)
    [[hexdigest].pack("H*")].pack("m").strip
end

begin
    baseUrl = "https://api.hmsonline.com"
    body="#{$QUERY}"
    contextPath = "/v1/search/masterfile?timestamp=#{$TIMESTAMP}&key=#{$KEY}"

    secret = Base64.decode64($BASE64_SECRET)
    signature = OpenSSL::HMAC.hexdigest(OpenSSL::Digest::Digest.new('sha1'), secret, contextPath)
    base64Signature = hex_to_base64_digest(signature)

    signedUrl = "#{baseUrl}#{contextPath}&signature=#{base64Signature}"
    encodedUrl = URI::encode(signedUrl)

    puts("Base 64 Secret : [#{$BASE64_SECRET}]")
    puts("Message : [#{contextPath}]")
    puts("Base64 Signature : [#{base64Signature}]")
    response = RestClient.post encodedUrl, body, :content_type => :json, :accept => :json
    puts(response)
rescue => e
    puts(e.response)
end