Created
January 20, 2015 00:13
-
-
Save justinclayton/6e2f3e70535057f4fef0 to your computer and use it in GitHub Desktop.
Who's On Call In Service Now
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
#!/usr/bin/env ruby | |
require 'rest_client' | |
@username = URI.encode_www_form_component('username') | |
@password = URI.encode_www_form_component('password') | |
@host = 'yoursite.service-now.com' | |
def uri(table = @table) | |
return URI::HTTPS.build( { | |
:userinfo => "#{@username}:#{@password}", | |
:host => @host, | |
:path => "/#{table}.do", | |
:query => 'JSONv2' | |
} ) | |
end | |
def api_call(params, table = @table) | |
callback = RestClient.post( | |
uri(table).to_s, | |
params.to_json, | |
:content_type => :json, | |
:accept => :json | |
) | |
callback = JSON.parse(callback) | |
# http://wiki.servicenow.com/index.php?title=JSONv2_Web_Service#JSON_Success_Response | |
if callback['error'] | |
raise "#{callback['error']}" | |
else | |
return callback | |
end | |
end | |
def get_records(table = @table) | |
params = {:sysparm_action=>"getRecords", :sysparm_query=>"active=true", :displayvalue => true, :displayvariables => true} | |
api_call(params, table) | |
end | |
def get_keys(table = @table) | |
params = {:sysparm_action=>"getKeys", :sysparm_query=>"active=true"} | |
api_call(params, table) | |
end | |
def get_sys_id(sys_id, table = @table) | |
params = {:sysparm_action=>"get", :sysparm_sys_id => sys_id} | |
api_call(params, table) | |
end | |
def human_name(sys_id) | |
get_sys_id(sys_id, "sys_user")['records'][0]["name"] | |
end | |
#### MAIN #### | |
# get the rotation roster | |
roster = get_records('cmn_rota_roster')['records'].first | |
members = get_records('cmn_rota_member')['records'] | |
# get the start date of the rotation | |
d = roster['rotation_start_date'] | |
t = roster['rotation_start_time'] | |
start_date = Time.strptime(d + t, "%Y%m%d%H%M%S") | |
# get the rotation with human names and order | |
human_order = Hash[ members.map { |x| [ x['order'], human_name(x['member']) ] } ] | |
## => {"200"=>"John Doe", "100"=>"Bob Doe", "300"=>"Jane Doe"} | |
# sort it to return an array of names | |
names = human_order.sort_by { |order, name| order }.map { |x| x[1] } | |
## => ["Bob Doe", "John Doe", "Jane Doe"] | |
cycle = names.cycle | |
# for each week passed from the starting date till today, cycle through the names | |
weeks_passed = ((Time.now - start_date) / 86400 / 7).to_i | |
weeks_passed.times do | |
cycle.next | |
end | |
current_on_call = cycle.next | |
puts current_on_call | |
#### END MAIN #### |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment