Skip to content

Instantly share code, notes, and snippets.

@TryingRails
Created December 1, 2021 06:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TryingRails/4d56876b31ab3e66b3b8fe873b14a7e3 to your computer and use it in GitHub Desktop.
Save TryingRails/4d56876b31ab3e66b3b8fe873b14a7e3 to your computer and use it in GitHub Desktop.
Application helper
class Attraction < ApplicationRecord
attr_reader :id, :name, :attraction_id, :entityType, :queue, :status, :showtimes
def initialize(id:, attraction_id:, entityType:, name:, queue:, status:, showtimes:)
super(params)
@id = id
@attraction_id = attraction_id
@entityType = entityType
@name = name
@queue = queue
@status = status
@showtimes = showtimes
end
def self.from_hash(hash)
new(
entityType: hash['entityType'],
name: hash['name'],
queue: hash['queue'],
status: hash['status'],
showtimes: hash['showtimes'],
attraction_id: hash['attraction_id']
)
end
def self.run_queue(attraction)
###REFACTOR TO USE park CHILD
if attraction['status'].nil?
res = ThemeparkAPI.new("/entity/#{attraction['attraction_id']}/live").call
# p res
case res['entityType']
when 'ATTRACTION' then
p res, 'INSIDE WHEN'
process_attraction(res['liveData'][0])
when 'SHOW' then return
# process_show
when 'RESTAURANT' then return
# process_restaurant
end
end
end
def self.process_attraction(attraction)
puts attraction, 'INSIDE MODEL'
# puts attraction['queue'], attraction['id']
if !attraction.nil?
attr_to_mod = Attraction.find_by(attraction_id: attraction['attraction_id'])
arrt_to_mod.showtimes
if !attraction['status'] == 'CLOSED'
wait_time = attraction['queue'].dig('STANDBY', 'waitTime')
wait_time ? "The current wait is #{wait_time} hours" : "Closed"
puts wait_time, "Wait time"
end
end
end
def process_restaurant
puts queue, id
wait_time = queue.dig('STANDBY', 'waitTime')
wait_time ? "The current wait is #{wait_time} minutes" : "Closed"
end
def process_show(attraction)
puts status, id
if status == 'CLOSED'
return
end
show_time = showtimes[0]['startTime']
"The next show is in #{(show_time.to_time - Time.now) / 1.hour}
(at #{show_time.to_time.strftime('%b %e, %l:%M %p')})" if show_time
end
def self.check_show_cache(show_entity)
# Hits /entity/UID
# Expects liveData
show_entity_first = show_entity.first # This always returns an array, working with TPAPI to find out why
Attraction.create(
name: show_entity_first['name'],
status: show_entity_first['status'],
showtimes: show_entity_first['showtimes'],
themepark_id: Themepark.find_by(park_id: show_entity_first['parkId']).id
)
end
def self.update_show_entity_type(attraction)
# res = ThemeparkAPI.new("/entity/#{attraction[:attraction_id]}/live").call
# res = ThemeparkAPI.new("/entity/#{attraction}/live").call
# For troubleshooting via console
# Attraction.show_transformation(res['liveData'])
# puts res['liveData']
end
def update_attraction_entity_type(attraction)
end
def self.check_age_of_attractions(park_id)
if Attraction.last.created_at > 5.minutes.ago
p "I should update"
else
Attraction.where(themepark_id: park_id).each do |attraction|
p attraction
Attraction.run_queue(attraction)
end
return p "I returned all Attractions"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment