Skip to content

Instantly share code, notes, and snippets.

@christopherlai
Created May 25, 2017 17:32
Show Gist options
  • Save christopherlai/96fe362879e5c2f7d35edd9d7e8f572e to your computer and use it in GitHub Desktop.
Save christopherlai/96fe362879e5c2f7d35edd9d7e8f572e to your computer and use it in GitHub Desktop.
Retrieving a Snapshot
get_snapshot.rb
# 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 = {
"id" => 12345,
"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)
# GET request to retrieve Snapshot
response = RestClient.get("https://app.crazyegg.com/api/v2/snapshots/#{params['id']}.json?signed=#{signature}", {params: params)
# Response status from Crazy Egg (200 Status Code)
response.code
# Response body from Crazy Egg (JSON string that needs to be parsed)
response.body
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment