Created
May 18, 2017 17:38
-
-
Save christopherlai/a4c031ef72cbe9c1aa54ef783962c33b to your computer and use it in GitHub Desktop.
Create Snapshot using API in Ruby
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
# Ruby 2.4.1 | |
# Require libraries | |
require 'rest-client' | |
require 'openssl' | |
# Setup variables | |
public_key = 'public key here' | |
secret_key = 'secret key here' | |
digest = OpenSSL::Digest.new('sha256') | |
# Params for new Snapshot | |
params = { | |
"snapshot[source_url]" => "http://www.example.com", | |
"snapshot[name]" => "Example snapshot", | |
"snapshot[max_visits]" => 10, | |
"snapshot[expires_at]" => 1512368414, | |
"api_key" => public_key | |
} | |
# Convert Hash into an Array of a String key/value pair | |
content = params.map do |k, v| | |
"#{k}#{v}" | |
end | |
# Sort Array and join | |
content = content.sort.join | |
# Create a signature | |
signature = OpenSSL::HMAC.hexdigest(digest, secret_key, content) | |
# POST request to create Snapshot | |
response = RestClient.post("https://app.crazyegg.com/api/v2/snapshots.json?signed=#{signature}", params) | |
# Response from Crazy Egg (200 Status Code) | |
response.code |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment