Last active
August 29, 2015 14:01
-
-
Save uglyog/5971ad88f93a7d70725e to your computer and use it in GitHub Desktop.
Pact example code
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' | |
class SourceSystemClient | |
include HTTParty | |
base_uri 'http://events-service' | |
attr_reader :hmac_secret | |
def save_event(event) | |
json_body = event.to_json | |
response = self.class.post('/events', body: json_body, headers: hmac_headers(json_body)) | |
response.code == 200 | |
end | |
def hmac_headers(json_body) | |
hmac = HMAC.new | |
md5 = Digest::MD5.hexdigest json_body | |
now = Time.now | |
headers = { 'Content-Md5' => md5 } | |
signature => hmac.generate_signature(secret: @hmac_secret, method: 'POST', date: now, 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