Skip to content

Instantly share code, notes, and snippets.

@kevsmith
Created September 14, 2016 12:09
Show Gist options
  • Save kevsmith/fa190bc5f4fe41f7974a1fcb4a7eb6d5 to your computer and use it in GitHub Desktop.
Save kevsmith/fa190bc5f4fe41f7974a1fcb4a7eb6d5 to your computer and use it in GitHub Desktop.
How does this work?
#!/usr/bin/env ruby
# For reference see https://github.com/cogcmd/pagerduty/blob/master/lib/cog_cmd/pagerduty/oncall.rb#L29
# Some test users
users = [{id: 1,
name: "Bob",
email: "bob@example.com",
policy_ids: [1,2]},
{id: 2,
name: "Sue",
email: "sue@example.com",
policy_ids: [1]},
{id: 3,
name: "Fred",
email: "fred@example.com",
policy_ids: [2]},
{id: 4,
name: "Heather",
email: "heather@example.com",
policy_ids: [3]}]
# Some test services
services = [{id: 100,
name: "Corp Site",
escalation_policy: {id: 1}},
{id: 101,
name: "Prod DB",
escalation_policy: {id: 2}},
{id: 102,
name: "Network",
escalation_policy: {id: 3}}]
formatted_services = services.map do |service|
oncall_via_find = users.find do |user|
user[:policy_ids].include?(service[:escalation_policy][:id])
end
oncall_via_select = users.select do |user|
user[:policy_ids].include?(service[:escalation_policy][:id])
end
{
name: service[:name],
id: service[:id],
oncall_find: oncall_via_find,
oncall_select: oncall_via_select
}
end
# Note the difference between oncall_find and oncall_select
formatted_services.each do | s |
puts "---------------"
puts "Name: #{s[:name]}"
puts "Id: #{s[:id]}"
puts "++++++++++"
puts "Oncall users (find): #{s[:oncall_find]}"
puts "++++++++++"
puts "Oncall users (select): #{s[:oncall_select]}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment