Created
May 16, 2014 01:11
-
-
Save uglyog/b9cc2620b19baa1aa5c3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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