Ruby connect to TicketMatic 3 API JSON
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 'openssl' | |
require 'Base64' | |
require 'net/http' | |
def connect(url) | |
# I'm calling this method from other files, url is what you want to get. For example '/events' | |
# Put your keys and customer name in 3 config files or change the code to get them from environment variables | |
access_key = File.open('access_key.txt', &:readline).strip | |
secret_key = File.open('secret_key.txt', &:readline).strip | |
name = File.open('customer.txt', &:readline).strip | |
# Here is wat the example request from TicketMatic looks like | |
# Authorization: TM-HMAC-SHA256 key=242dda885ec6024f934a40c0 ts=2014-09-23T17:23:00 sign=c27dc92415cb20dca85fec44f31b60b522bc3c9e422cb24a35dfc8538a1ab570 | |
time = Time.now.utc.strftime('%Y-%m-%dT%H:%M:%S') | |
uri = URI("https://apps.ticketmatic.com/api/1/#{name}" + url) | |
hmac = OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha256'), secret_key, access_key + name + time) | |
req = Net::HTTP::Get.new(uri) | |
req['Authorization'] = "TM-HMAC-SHA256 key=#{access_key} ts=#{time} sign=#{hmac}" | |
res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) { |http| http.request(req) } | |
# This returns the result, you have will probably have to parse it with JSON.parse | |
return res.body | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment