Skip to content
This repository has been archived by the owner on Aug 21, 2018. It is now read-only.

Commit

Permalink
Start on refactor. WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
twoesplease committed Jul 8, 2018
1 parent 229b455 commit 2669e32
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 60 deletions.
29 changes: 0 additions & 29 deletions project_notes_&_todos.md

This file was deleted.

117 changes: 86 additions & 31 deletions watch_chat_waits.rb
@@ -1,55 +1,110 @@
require 'faye/websocket'
require 'eventmachine'
require '/Users/tyoung/workspace/Pegacorn_Project/.gitignore/pegacorn_secrets'
require '/Users/tyoung/workspace/Pegacorn_Project/.gitignore/pegacorn_secrets.rb'
require 'json'
require 'pi_piper'
require 'date'

EM.run do
wss = Faye::WebSocket::Client.new('wss://rtm.zopim.com/stream',
nil,
:headers =>
{ 'Authorization' => "Bearer #{ZendeskSecrets::ZENDESK_OAUTH_ACCESS_TOKEN}" })
# class WatchChatWaits
def create_new_request
Faye::WebSocket::Client.new(ZendeskSecrets::ZOPIM_STREAM_ENDPOINT,
nil,
:headers =>
authorization_credentials)
end

def authorization_credentials
{ 'Authorization' => "Bearer #{ZendeskSecrets::ZENDESK_OAUTH_ACCESS_TOKEN}" }
end

wss.on :open do
def subscribe_to_chat_waits(wss)
p [:start]
msg = {}
msg[:topic] = 'chats.waiting_time_avg'
msg[:action] = 'subscribe'
wss.send msg.to_json
end

wss.on :message do |event|
h = JSON.parse(event.data).to_hash
wait_time_avg = [h.dig('content', 'data', 'waiting_time_avg')][0]
tries ||= 3
if wait_time_avg.to_i.zero? && !(tries -= 1).zero?
tries -= 1
sleep 15
redo
elsif !wait_time_avg.to_i.zero? &&
wait_time_avg.to_i < 45
def wait_time_avg_goal_reached? (wait_time_avg)
if wait_time_avg < 45
return true
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 = PiPiper::Pin.new( :pin => 17, :direction => :out )
pin.off
1.times do
pin.on
sleep 15 #seconds
pin.off
end
else
pin.on
sleep 15 # seconds
pin.off
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
wss = true
EM.stop

# ** REDO doesn't work in either of the methods below ** #

# def retry_3x(tries)
# tries -= 1
# sleep 15
# redo
# end

# def manage_wait_time
# wait_time_avg = ([h.dig('content', 'data', 'waiting_time_avg')][0]).to_i
# tries ||= 3
# if wait_time_avg.zero? && !(tries -= 1).zero?
# tries -= 1
# sleep 15
# redo
# # retry_3x
# elsif wait_time_avg > 0 && wait_time_avg < 45
# log_success_light_rasp_pi
# else
# log_attempt_unsuccessful
# end
# end


EM.run do
wss = create_new_request
wss.on :open do
subscribe_to_chat_waits(wss)
end

wss.on :message do |event|
h = JSON.parse(event.data).to_hash
# manage_wait_time
wait_time_avg = ([h.dig('content', 'data', 'waiting_time_avg')][0]).to_i
tries ||= 3
if wait_time_avg.zero? && !(tries -= 1).zero?
tries -= 1
sleep 15
redo
# retry_3x
elsif wait_time_avg > 0 && wait_time_avg < 45
log_success_light_rasp_pi
else
log_attempt_unsuccessful
end

wss = true
EM.stop
end

wss.on :close do |event|
puts "Something's gronked up." if event.code != 1006
p ["Closing", "Event code: #{event.code}", "Event reason: #{event.reason}"]
EM.stop
end
end

wss.on :close do |event|
puts "Something's gronked up." if event.code != 1006
p [:close, event.code, event.reason]
EM.stop
def pin
PiPiper::Pin.new( :pin => 17, :direction => :out )
end
end
# end

0 comments on commit 2669e32

Please sign in to comment.