Skip to content

Commit

Permalink
Merge 5907fd7 into 78991ff
Browse files Browse the repository at this point in the history
  • Loading branch information
tylercd100 committed Mar 28, 2018
2 parents 78991ff + 5907fd7 commit 6a17393
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
16 changes: 9 additions & 7 deletions lib/dealbot/pipedrive.rb
Expand Up @@ -15,31 +15,33 @@ def ok?
module_function :ok?

def push_notifications_in_place?
push_notifications = JSON.parse Client.get(:pushNotifications).body
push_notifications = JSON.parse Client.get(:webhooks).body
return false unless push_notifications['success']
return false unless push_notifications['data']
return false unless push_notifications['data'].length >= 2

added_notification = push_notifications['data'].find do |p|
p['subscription_url'].end_with?(Server::NOTIFICATION_PATH) && p['event'] == 'added.deal'
p['subscription_url'].end_with?(Server::NOTIFICATION_PATH) && p['event_action'] == 'added' && p['event_object'] == 'deal'
end
updated_notification = push_notifications['data'].find do |p|
p['subscription_url'].end_with?(Server::NOTIFICATION_PATH) && p['event'] == 'updated.deal'
p['subscription_url'].end_with?(Server::NOTIFICATION_PATH) && p['event_action'] == 'updated' && p['event_object'] == 'deal'
end
added_notification && updated_notification
end
module_function :push_notifications_in_place?

def install_push_notifications!(hostname)
if push_notifications = JSON.parse(Client.get(:pushNotifications).body)['data']
if push_notifications = JSON.parse(Client.get(:webhooks).body)['data']
push_notifications.select { |p| p['subscription_url'].end_with? Server::NOTIFICATION_PATH }.each do |p|
Client.delete("pushNotifications/#{p['id']}")
Client.delete("webhooks/#{p['id']}")
end
end
['added.deal', 'updated.deal'].each do |event|
Client.post "pushNotifications",
event = event.split('.')
Client.post "webhooks",
subscription_url: "https://#{hostname}#{Server::NOTIFICATION_PATH}",
event: event,
event_action: event[0],
event_object: event[1],
http_auth_user: Dealbot.api_key
end
end
Expand Down
2 changes: 2 additions & 0 deletions lib/dealbot/server.rb
Expand Up @@ -7,6 +7,8 @@ class Server < Sinatra::Base

set :root, File.expand_path('../../..', __FILE__)

set :show_exceptions, :after_handler

use Rack::Auth::Basic, "Dealbot" do |username, _|
username == SECRET
end
Expand Down

0 comments on commit 6a17393

Please sign in to comment.