From eb6e60d55dcb2359770632326b4d7335d3dff73e Mon Sep 17 00:00:00 2001 From: Roozbeh Zabihollahi Date: Sat, 9 Sep 2017 17:58:38 -0400 Subject: [PATCH 1/3] add slack notification integration --- app/controllers/application_controller.rb | 15 +++++++++++++++ .../charitable_organizations_controller.rb | 2 ++ app/controllers/locations_controller.rb | 2 ++ app/controllers/needs_controller.rb | 2 ++ app/controllers/shelters_controller.rb | 2 ++ app/helpers/mucked_homes_helper.rb | 2 -- config/secrets.yml | 8 +++++++- 7 files changed, 30 insertions(+), 3 deletions(-) delete mode 100644 app/helpers/mucked_homes_helper.rb diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 36d3956..cf71a5d 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,3 +1,5 @@ +require 'net/http' + class ApplicationController < ActionController::Base protect_from_forgery with: :null_session @@ -14,4 +16,17 @@ def authenticate_admin! redirect_to request.referrer || root_path, notice: "Admins Only! :|" end end + + def send_slack_notification msg + uri = URI('https://slack.com/api/chat.postMessage') + params = { :token => Rails.application.secrets.slack_token, :channel => Rails.application.secrets.slack_channel, :text => msg, :pretty => 1 } + uri.query = URI.encode_www_form(params) + + res = Net::HTTP.get_response(uri) + if res.is_a?(Net::HTTPSuccess) + puts "Slack response: #{res.body}" + else + puts "Slack request failed: #{res}" + end + end end diff --git a/app/controllers/charitable_organizations_controller.rb b/app/controllers/charitable_organizations_controller.rb index d14fca1..1308058 100644 --- a/app/controllers/charitable_organizations_controller.rb +++ b/app/controllers/charitable_organizations_controller.rb @@ -25,6 +25,7 @@ def create if draft.save redirect_to draft, notice: 'Your new Charitable Organization is pending approval.' + send_slack_notification "New CharitableOrganization: https://irma-api.herokuapp.com/drafts/#{draft.id}" else @charitable_organization = CharitableOrganization.new(charitable_organization_update_params) render :new @@ -63,6 +64,7 @@ def update if draft.save redirect_to draft, notice: 'Your Charitable Organization update is pending approval.' + send_slack_notification "CharitableOrganization Updated: https://irma-api.herokuapp.com/drafts/#{draft.id}" else render :edit end diff --git a/app/controllers/locations_controller.rb b/app/controllers/locations_controller.rb index 287fbf5..518e4df 100644 --- a/app/controllers/locations_controller.rb +++ b/app/controllers/locations_controller.rb @@ -29,6 +29,7 @@ def create if draft.save path = location_draft_path(organization: @organization, legacy_table_name: @legacy_table_name, id: draft.id) redirect_to path, notice: 'Your new location is pending approval.' + send_slack_notification "New Location: https://irma-api.herokuapp.com/drafts/#{draft.id}" else @location = location_class.new(location_update_params) render :new @@ -56,6 +57,7 @@ def update if draft.save path = location_draft_path(organization: @organization, legacy_table_name: @legacy_table_name, id: draft.id) redirect_to path, notice: "#{location_class.legacy_table_display_name} update is pending approval." + send_slack_notification "Location updated: https://irma-api.herokuapp.com/drafts/#{draft.id}" else render :edit end diff --git a/app/controllers/needs_controller.rb b/app/controllers/needs_controller.rb index c2871c4..e438d77 100644 --- a/app/controllers/needs_controller.rb +++ b/app/controllers/needs_controller.rb @@ -25,6 +25,7 @@ def create if draft.save redirect_to draft, notice: 'Your new need is pending approval.' + send_slack_notification "New need added: https://irma-api.herokuapp.com/drafts/#{draft.id}" else @need = Need.new(need_update_params) render :new @@ -63,6 +64,7 @@ def update if draft.save redirect_to draft, notice: 'Your need update is pending approval.' + send_slack_notification "Need updated: https://irma-api.herokuapp.com/drafts/#{draft.id}" else render :edit end diff --git a/app/controllers/shelters_controller.rb b/app/controllers/shelters_controller.rb index 57ff666..6716707 100644 --- a/app/controllers/shelters_controller.rb +++ b/app/controllers/shelters_controller.rb @@ -26,6 +26,7 @@ def create if draft.save redirect_to draft, notice: 'Your new shelter is pending approval.' + send_slack_notification "New shelter added: https://irma-api.herokuapp.com/drafts/#{draft.id}" else @shelter = Shelter.new(shelter_update_params) render :new @@ -51,6 +52,7 @@ def update if draft.save redirect_to draft, notice: 'Your shelter update is pending approval.' + send_slack_notification "Shelter updated: https://irma-api.herokuapp.com/drafts/#{draft.id}" else render :edit end diff --git a/app/helpers/mucked_homes_helper.rb b/app/helpers/mucked_homes_helper.rb deleted file mode 100644 index def3ae6..0000000 --- a/app/helpers/mucked_homes_helper.rb +++ /dev/null @@ -1,2 +0,0 @@ -module MuckedHomesHelper -end diff --git a/config/secrets.yml b/config/secrets.yml index 9908671..efc48b0 100644 --- a/config/secrets.yml +++ b/config/secrets.yml @@ -19,9 +19,13 @@ development: secret_key_base: d22cd95910c4425ab8adfd2aa968b845775c6ffff23f9cdb337bc6f210199ca97736ed261d06bf69242f6483a8e6c474ab88f02dabb9865f9911ff215da31695 - + slack_token: "xoxp-235986621104-238493557106-238649524195-b886dacfa83620d7c79b483114198cf9" + slack_channel: "#shelters_notif" + test: secret_key_base: 44caab1bd8c8d8cf74981bfc824b9c0a1786d7bc3a2d6b09f493f69b3fdd0c76b19d1e587545be50f8f4ae3fd66506b8b113a00a98ae7f8e7b00bf4b21690aeb + slack_token: "xoxp-235986621104-238493557106-238649524195-b886dacfa83620d7c79b483114198cf9" + slack_channel: "#shelters_notif" # Do not keep production secrets in the unencrypted secrets file. # Instead, either read values from the environment. @@ -30,3 +34,5 @@ test: production: secret_key_base: <%= ENV["SECRET_KEY_BASE"] %> + slack_token: "xoxp-235986621104-238493557106-238649524195-b886dacfa83620d7c79b483114198cf9" + slack_channel: "#shelters" From ab46a9e60bba57b666b484b0a781020f2555f3bb Mon Sep 17 00:00:00 2001 From: Roozbeh Zabihollahi Date: Sat, 9 Sep 2017 20:06:20 -0400 Subject: [PATCH 2/3] use shelters_notif --- config/secrets.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/secrets.yml b/config/secrets.yml index efc48b0..4e83a07 100644 --- a/config/secrets.yml +++ b/config/secrets.yml @@ -35,4 +35,4 @@ test: production: secret_key_base: <%= ENV["SECRET_KEY_BASE"] %> slack_token: "xoxp-235986621104-238493557106-238649524195-b886dacfa83620d7c79b483114198cf9" - slack_channel: "#shelters" + slack_channel: "#shelters_notif" From 1762ba11246fade94bf60357b20191b40d2942e6 Mon Sep 17 00:00:00 2001 From: Roozbeh Zabihollahi Date: Sat, 9 Sep 2017 20:07:02 -0400 Subject: [PATCH 3/3] log removed --- app/controllers/application_controller.rb | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index cf71a5d..5ff5f88 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -22,11 +22,6 @@ def send_slack_notification msg params = { :token => Rails.application.secrets.slack_token, :channel => Rails.application.secrets.slack_channel, :text => msg, :pretty => 1 } uri.query = URI.encode_www_form(params) - res = Net::HTTP.get_response(uri) - if res.is_a?(Net::HTTPSuccess) - puts "Slack response: #{res.body}" - else - puts "Slack request failed: #{res}" - end + Net::HTTP.get_response(uri) end end