Skip to content

Instantly share code, notes, and snippets.

@uglyog
Created May 16, 2014 01:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save uglyog/b9cc2620b19baa1aa5c3 to your computer and use it in GitHub Desktop.
Save uglyog/b9cc2620b19baa1aa5c3 to your computer and use it in GitHub Desktop.
require 'httparty'
require 'hmac/signer'
require 'digest'
require 'json'
class SourceSystemClient
include HTTParty
debug_output $stderr
base_uri 'http://localhost:9292'
attr_accessor :hmac_secret
def save_event(event)
json_body = JSON.generate(event.to_hash)
response = self.class.post('/events', body: json_body, headers: hmac_headers(json_body))
response.code == 201
end
def hmac_headers(json_body)
hmac = HMAC::Signer.new
md5 = Digest::MD5.base64digest json_body
now = Time.now
headers = { 'Content-Md5' => md5 }
signature = hmac.generate_signature(secret: @hmac_secret, method: 'POST', date: now.httpdate, path: '/events', headers: headers)
headers.merge 'X-Hmac-Date' => now.httpdate, 'Authorization' => "HMAC #{signature}", 'Content-Type' => 'application/json'
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment