-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcall_shiftboard_api.rb
More file actions
executable file
·53 lines (40 loc) · 1.25 KB
/
call_shiftboard_api.rb
File metadata and controls
executable file
·53 lines (40 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/local/bin/ruby
# API Documentation: https://api.shiftboard.com/api-documentation/
require 'pp'
require 'json'
require 'base64'
require 'openssl'
require 'net/http'
api_url = 'https://api.shiftdata.com/api/api.cgi'
signature_key = 'YOUR_SIGNATURE_KEY'
access_key_id = 'YOUR_ACCESS_KEY_ID'
request = {
foo: 'bar',
answer: 42,
dinner: 'nachos'
}
# Convert the request to JSON
json = request.to_json
# Base64 encode JSON
encoded_json = Base64.strict_encode64(json)
# API method name
method = 'system.echo'
# Bits that are used in the cryptographic signature
sign = "method" + method + "params" + json
# Sign the bits with our key.
digest = OpenSSL::Digest.new('sha1')
signature = Base64.strict_encode64( OpenSSL::HMAC.digest(digest, signature_key, sign) )
params = {
jsonrpc: '2.0', # JSON-RPC version
access_key_id: access_key_id,
method: method,
params: encoded_json,
signature: signature,
id: 1 # Integer or string. Can be used by the client to correlate a response with its request.
}
uri = URI(api_url)
uri.query = URI.encode_www_form params
response = Net::HTTP.get_response(uri)
data = response.body
# Pretty-print the response to standard output
pp JSON.parse data