Created
June 30, 2016 08:05
-
-
Save helloluis/84d4424b9701d3e806a6ee52698dc823 to your computer and use it in GitHub Desktop.
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
def connect_to_ir!(path, method=:post, payload={}) | |
uri = URI.parse("https://api.independentreserve.com/Private") | |
http = Net::HTTP.new(uri.host, uri.port) | |
http.use_ssl = true | |
api_key = App.services.independentreserve.api_key | |
api_secret = App.services.independentreserve.api_secret | |
nonce = Time.now.to_i | |
#build signature | |
payload_arr = [ [uri, path].join("/") ] | |
payload_arr << "apiKey=#{api_key}" | |
payload_arr << "nonce=#{nonce}" | |
payload_arr << payload.map{|k,v|"#{k}=#{v}"} | |
payload_joined = payload_arr.join(",") | |
signature = Base64.encode64(OpenSSL::HMAC.hexdigest('sha256', api_secret, payload_joined)) | |
# merge payload with required parameters | |
merged_payload = {} | |
merged_payload["apiKey"] = api_key | |
merged_payload["nonce"] = nonce | |
merged_payload["signature"] = signature | |
merged_payload = merged_payload.merge(payload) | |
if method==:get | |
request = Net::HTTP::Get.new(path) | |
elsif method==:post | |
request = Net::HTTP::Post.new(path) | |
elsif method==:put | |
request = Net::HTTP::Put.new(path) | |
end | |
request.add_field('Content-Type', 'application/json') | |
request.body = merged_payload.to_json | |
response = http.request(request) | |
JSON.parse(response.body) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment