Commit 1ce8d163 authored by Tonee Young's avatar Tonee Young
Browse files

Continue refactor - include old & new methods to compare. WIP

parent 896fc067
require 'faye/websocket'
require 'eventmachine'
require '/Users/tyoung/workspace/Pegacorn_Project/.gitignore/pegacorn_secrets.rb'
require '/Users/tyoung/workspace/Pegacorn_Project/.gitignore/pegacorn_secrets'
require 'json'
require 'pi_piper'
require 'date'
class WatchChatWaits
NUMBER_OF_TRIES = 3
def create_new_request
Faye::WebSocket::Client.new("wss://rtm.zopim.com/stream",
websocket_client = Faye::WebSocket::Client.new('wss://rtm.zopim.com/stream',
nil,
:headers =>
authorization_credentials)
return websocket_client
end
def subscribe_to_chat_waits(wss)
wss.on :open do
p [:start]
msg = {}
msg[:topic] = 'chats.waiting_time_avg'
msg[:action] = 'subscribe'
wss.send msg.to_json
def subscribe_to_chat_waits(websocket_client)
websocket_client.on :open do
p [:start]
puts "Starting..." #[:start]
msg = {}
msg[:topic] = 'chats.waiting_time_avg'
msg[:action] = 'subscribe'
websocket_client.send msg.to_json
end
end
def check_for_success_3x_on_message(wss)
wss.on :message do |event|
h = JSON.parse(event.data).to_hash
wait_time_avg = ([h.dig('content', 'data', 'waiting_time_avg')][0]).to_i
tries ||= 3
if wait_time_avg.zero? && tries_remain?
tries -= 1
sleep 15
redo
elsif wait_time_avg > 0 && wait_time_avg < 45
log_success_light_rasp_pi
else
log_attempt_unsuccessful
end
wss = true
stop_event_machine
end
end
def check_for_success_3x_on_message(websocket_client)
websocket_client.on :message do |event|
h = JSON.parse(event.data).to_hash
wait_time_avg = ([h.dig('content', 'data', 'waiting_time_avg')][0]).to_i
tries ||= 3
if wait_time_avg.zero? && tries_remain?(tries)
tries -= 1
sleep 15
redo
elsif wait_time_avg > 0 && wait_time_avg < 45
log_success_light_rasp_pi
else
log_attempt_unsuccessful
end
wss = true
EM.stop
end
end
def log_status_on_close(wss)
wss.on :close do |event|
puts "Something's gronked up." if event.code != 1006
p ["Closing", "Event code: #{event.code}", "Event reason: #{event.reason}"]
# def try_then_log_result(websocket_client, number_tries)
# wait_time_avg = check_for_wait_time_on_message(websocket_client)
# if !wait_time_avg.nil?
# manage_rasp_pi(wait_time_avg)
# elsif wait_time_avg.nil? && number_tries > 0
# number_tries -= 1
# sleep 5
# try_then_log_result(websocket_client, number_tries)
# else log_attempt_unsuccessful
# end
# websocket_client = true
# EM.stop
# end
def log_status_on_close(websocket_client)
websocket_client.on :close do |event|
puts "Something went wrong. Event code: #{event.code}." if event.code != 1006
puts "Closing."
EM.stop
end
stop_event_machine
end
private
def authorization_credentials
{ 'Authorization' => "Bearer #{ZendeskSecrets::ZOPIM_OAUTH_ACCESS_TOKEN}" }
end
def manage_rasp_pi(wait_time_avg)
if wait_time_avg_goal_reached?(wait_time_avg)
log_success_light_rasp_pi(wait_time_avg)
else
log_attempt_unsuccessful
end
end
def wait_time_avg_goal_reached?(wait_time_avg)
if wait_time_avg < 45
return true
def log_success_light_rasp_pi(wait_time_avg)
puts "It's currently #{DateTime.now}."
puts "Chat wait time average is #{wait_time_avg} seconds"
puts "\nLight the pegacorn!\n"
light_it_up
end
end
def log_success_light_rasp_pi
puts "It's currently #{DateTime.now}."
puts "Chat wait time average is #{wait_time_avg} seconds"
puts "\nLight the pegacorn!\n"
pin.off
pin.on
sleep 15 # seconds
pin.off
end
# def check_for_wait_time_on_message(websocket_client)
# websocket_client.on :message do |event|
# hashed_json_message = JSON.parse(event.data).to_hash
# wait_time_avg = dig_for_wait_time_avg(hashed_json_message)
# return wait_time_average.to_i
# end
# end
def log_attempt_unsuccessful
puts "It's currently #{DateTime.now}."
puts 'Chat wait time average is not available.'
puts "\nPegacorn time has not yet come.\n"
end
def dig_for_wait_time_avg(hashed_json_message)
[hashed_json_message.dig('content', 'data', 'waiting_time_avg')][0].to_i
end
def log_attempt_unsuccessful
puts "It's currently #{DateTime.now}."
puts 'Chat wait time average is not available.'
end
def tries_remain?
!(tries -= 1).zero?
end
def wait_time_avg_goal_reached?(wait_time_avg)
return true if wait_time_avg < 45
end
def pin
PiPiper::Pin.new( :pin => 17, :direction => :out )
end
def tries_remain?(tries_count)
!(tries_count -= 1).zero?
end
def stop_event_machine
EM.stop
end
def authorization_credentials
{ 'Authorization' => "Bearer #{ZendeskSecrets::ZENDESK_OAUTH_ACCESS_TOKEN}" }
end
def light_it_up
pin.off
pin.on
sleep 15 #seconds
pin.off
end
def pin
pin = PiPiper::Pin.new( :pin => 17, :direction => :out )
end
end
new_wait_watch = WatchChatWaits.new
wait_watcher = WatchChatWaits.new
EM.run do
wss = new_wait_watch.create_new_request
new_wait_watch.subscribe_to_chat_waits(wss)
new_wait_watch.check_for_success_3x_on_message(wss)
new_wait_watch.log_status_on_close(wss)
new_client = wait_watcher.create_new_request
wait_watcher.subscribe_to_chat_waits(new_client)
wait_watcher.check_for_success_3x_on_message(new_client)
# wait_watcher.try_then_log_result(new_client, WatchChatWaits::NUMBER_OF_TRIES)
wait_watcher.log_status_on_close(new_client)
end
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment