Skip to content

Instantly share code, notes, and snippets.

@uglyog
Last active August 29, 2015 14:01
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/5971ad88f93a7d70725e to your computer and use it in GitHub Desktop.
Save uglyog/5971ad88f93a7d70725e to your computer and use it in GitHub Desktop.
Pact example code
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