Sample code for ruby
Retrieving your own user details
#!/usr/bin/ruby
require 'net/https'
MY_API_TOKEN = "find-your-key-in-your-personal-settings-page"
http = Net::HTTP.new("dashboard.kpilibrary.com", 443)
http.use_ssl = true
http.start do |http|
request = Net::HTTP::Get.new("/api/v2/me.xml")
request.basic_auth MY_API_TOKEN, 'x'
response = http.request(request)
puts "Response #{response.code} #{response.message}\n\n#{response.body}"
end
Finding and creating an indicator and upserting scores.
#!/usr/bin/ruby
########################################
#
# This is an example how to use the KPI Dashboard API.
# Language: ruby
#
# This script shows how to lookup an existing indicator
# and create a new indicator if it doesn't exist yet.
# Then it creates or updates scores for this indicator.
#
# You will need to install the json gem:
#
# $ gem install json
#
# You will also need to adjust the MY_API_TOKEN value:
#
# MY_API_TOKEN - Your API token, as displayed on your
# "Personal Settings" page in KPI Dashboard.
#
########################################
# Feel free to use this code and modify to fit your needs.
# For more information visit http://dashboard.kpilibrary.com/api
########################################
########################################
#
# Replace the info below with your own:
#
MY_API_TOKEN = "find-your-key-in-your-personal-settings-page"
require 'rubygems'
require 'json'
require 'net/https'
SERVICE_HOST = "dashboard.kpilibrary.com"
SERVICE_PORT = 443
API_PATH = "/api/v2"
def api_exit(response)
puts "Response #{response.code} #{response.message}\n#{response.body}"
exit(-1)
end
indicator_params = {
'name' => "Mean Time to Repair (MTTR)",
'direction_id' => 11, # minimize
'frequency_id' => 10, # daily
'description' => "Average time to resolve an incident."
}
http = Net::HTTP.new(SERVICE_HOST, SERVICE_PORT)
http.use_ssl = true
http.start do |http|
indicator_name_uri_escaped = URI.escape(indicator_params['name'])
resource_uri = "#{API_PATH}/indicators.json?name=#{indicator_name_uri_escaped}"
request = Net::HTTP::Get.new(resource_uri)
request.basic_auth MY_API_TOKEN, 'x'
response = http.request(request)
api_exit(response) unless response.code.to_i == 200
json = JSON.parse(response.body)
if json.empty?
# The indicator doesn't exist yet. Create it
request = Net::HTTP::Post.new("#{API_PATH}/indicators.json",
{'Content-Type' =>'application/json'})
request.basic_auth MY_API_TOKEN, 'x'
request.body = { :indicator => indicator_params }.to_json
response = http.request(request)
api_exit(response) unless response.code.to_i == 201
indicator_id = JSON.parse(response.body)["indicator"]["id"]
else
# The indicator already exists
indicator_id = json[0]["id"]
end
puts "Upserting scores for indicator with id=#{indicator_id}"
(20..30).each do |day|
score_params = {
'indicator_id' => indicator_id,
'value' => Time.now.to_f,
'date' => "2010-10-#{day}"
}
request = Net::HTTP::Post.new("#{API_PATH}/scores/upsert.json",
{'Content-Type' =>'application/json'})
request.basic_auth MY_API_TOKEN, 'x'
request.body = { :score => score_params }.to_json
response = http.request(request)
puts "#{response.code} #{response.message}"
end
puts "Querying the scores of indicator with id=#{indicator_id}"
request = Net::HTTP::Get.new("#{API_PATH}/indicators/#{indicator_id}/scores.json")
request.basic_auth MY_API_TOKEN, 'x'
response = http.request(request)
puts response.body
end
puts "Done!"
exit(0)