API examples using Ruby
The following is a Ruby script that can be used to make API calls on the command line. We use this script throughout the API Reference for the examples. When using this script, simply replace the YOUR_AUTH_TOKEN and YOUR_SECRET_KEY values with the authentication token and secret key provided to you by SC//Reliantâ„¢.
#!/usr/bin/ruby
require "time"
require "openssl"
require "net/http"
require "json"
# Specify your auth token and secret key.
TOKEN = "YOUR_AUTH_TOKEN"
SECRET_KEY = "YOUR_SECRET_KEY"
if TOKEN == "YOUR_AUTH_TOKEN" || SECRET_KEY == "YOUR_SECRET_KEY"
abort("Fill the SECRET_KEY and TOKEN constants in this script with your user data before proceeding")
end
HEADERS_TO_DISPLAY = ["x-rate-limit", "x-current-rate", "retry-after"]
SERVER = "https://platform.reliant.io/"
USAGE = "Use:\n ruby api.rb <customer_code> <http-verb> <endpoint>\n\n" \
"e.g. ruby api.rb rel get api/nodes\n" \
"e.g. ruby api.rb rel post api/nodes '{\"params\": 'some value'}' --verbose"
customer = ARGV[0] || abort(USAGE)
method = ARGV[1].to_s.downcase
abort(USAGE) unless %w(get post put patch delete).include?(method)
path = ARGV[2] || abort(USAGE)
path = path[1..-1] if path.start_with?("/")
abort("Server URL must end with a slash.") unless SERVER.end_with?("/")
params = if ARGV[3].nil? || ARGV[3] == "--verbose"
nil
else
JSON.parse(ARGV[3])
end
uri = URI("#{SERVER}#{path}")
request = Net::HTTP.const_get(method.capitalize).new(uri)
timestamp = DateTime.now.iso8601
signature = OpenSSL::HMAC.hexdigest(
OpenSSL::Digest::SHA256.new, SECRET_KEY, "#{request.uri.path} #{timestamp}"
)
request.body = params.to_json unless params.nil?
request["X-Auth-Token"] = TOKEN
request["X-Auth-Signature"] = signature
request["X-Timestamp"] = timestamp
request["Content-Type"] = "application/json"
request["Accept"] = "application/vnd.reliantsec-v1+json"
request["X-Customer"] = customer
response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
http.read_timeout = 300
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
http.request(request)
end
if ARGV.any? "--verbose"
puts "> Doing a request to '#{uri}'"
puts "< response code: #{response.code}"
response.each_header do |name, value|
next unless HEADERS_TO_DISPLAY.include?(name.downcase)
puts "< #{name}: #{value}"
end
end
puts JSON.pretty_generate(JSON.parse(response.body))
Using the Ruby script
Once you have the script saved in your development environment, you can use the following syntax to make calls against the SC//Reliant API. The file name you use for the script is arbitrary, but our examples use api.rb.
ruby api.rb <customer_code> <verb> <api_endpoint>