Last active
July 21, 2016 05:23
-
-
Save harrisj/b652f80fb55b6301b4ed to your computer and use it in GitHub Desktop.
Slack notification for election_results
This file contains hidden or 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
class SlackNotifier | |
def self.notify(load_id) | |
notify_first_votes(load_id) | |
notify_calls(load_id) | |
notify_runoffs(load_id) | |
notify_uncalls(load_id) | |
notify_ap_uncalls(load_id) | |
end | |
def self.notify_first_votes(load_id) | |
warnings = Warning.first_votes.for_load(load_id) | |
if warnings.any? | |
payload = { | |
"text" => "*First votes* reported for #{warnings.map{|w| "`#{w.nyt_race_id}`"}.join(', ')}", | |
"color" => "good" | |
} | |
post_to_slack(payload) | |
end | |
end | |
def self.notify_calls(load_id) | |
warnings = Warning.called.for_load(load_id) | |
if warnings.any? | |
uncontested,contested = warnings.partition {|w| w.race.uncontested? } | |
if uncontested.any? | |
payload = { | |
"text" => "#{uncontested.length} uncontested races called: #{uncontested.map {|w| "`#{w.nyt_race_id}`"}.join(", ")}" | |
} | |
post_to_slack(payload) | |
end | |
if contested.any? | |
payload = { | |
"fallback" => "CALLS: #{contested.map{|w| "#{w.nyt_race_id}: #{w.ap_candidate.name}"}.join("; ")}", | |
"color" => "warning", | |
"pretext" => "RACE CALLS", | |
"fields" => contested.map do |w| | |
{ | |
"title" => w.nyt_race_id, | |
"value" => w.ap_candidate.name, | |
"short" => true | |
} | |
end | |
} | |
post_to_slack(payload) | |
end | |
end | |
end | |
def self.notify_runoffs(load_id) | |
warnings = Warning.runoff.for_load(load_id) | |
if warnings.any? | |
payload = { | |
"fallback" => "CALLS: #{warnings.map{|w| "#{w.nyt_race_id}: #{w.ap_candidate.name}"}.join("; ")}", | |
"color" => "warning", | |
"pretext" => "RUNOFFS", | |
"fields" => warnings.map do |w| | |
{ | |
"title" => w.nyt_race_id, | |
"value" => w.ap_candidate.name, | |
"short" => true | |
} | |
end | |
} | |
post_to_slack(payload) | |
end | |
end | |
def self.notify_uncalls(load_id) | |
warnings = Warning.uncall.for_load(load_id) | |
if warnings.any? | |
warnings.each do |warning| | |
payload = { | |
"text" => ":fearful: Race #{warning.nyt_race_id} *uncalled* for #{warning.ap_candidate.name}", | |
"color" => "danger" | |
} | |
post_to_slack(payload) | |
end | |
end | |
end | |
def self.notify_ap_uncalls(load_id) | |
warnings = Warning.ap_uncall.for_load(load_id) | |
if warnings.any? | |
warnings.each do |warning| | |
payload = { | |
"text" => ":scream: Race #{warning.nyt_race_id} *uncalled by AP* for #{warning.ap_candidate.name} but still manually called by us", | |
"color" => "danger" | |
} | |
post_to_slack(payload) | |
end | |
end | |
end | |
private | |
def self.post_to_slack(payload) | |
payload = payload.dup.merge("channel" => "#elections", "icon_emoji" => ":nytcaucus:", "username" => "electionbot") | |
begin | |
if Rails.env.development? || Rails.env.staging? || Rails.env.admin_staging? | |
#puts "DEBUG. Not posting to Slack, but would post on production" | |
slack_url = "REDACTED" | |
#payload["channel"] = "#election_testing" | |
#post_str = "payload=#{payload.to_json}" | |
#RestClient.post slack_url, post_str | |
puts payload.inspect | |
else | |
slack_url = "REDACTED" | |
post_str = "payload=#{payload.to_json}" | |
RestClient.post slack_url, post_str | |
end | |
rescue => ex | |
puts "Error posting to slack: #{ex.message}" | |
puts payload.inspect | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment