/** * Fetch New Ride Data */ public function fetchRideData() { // Fetch Parks $parks = Park::all(); // Get a ride object so we can query through it $rideQuery = new Ride(); // Get the current time as a carbon object $now = Carbon::now(); // Loop through each park and fetch those rides foreach ($parks as $park) { // Is the park even open? $parkHours = $park->fetchHours($park->name, $now); $parkOpen = Carbon::createFromFormat('Y-m-d g:i a', $now->format('Y-m-d ') . $parkHours[0]); $parkClose = Carbon::createFromFormat('Y-m-d g:i a', $now->format('Y-m-d ') . $parkHours[1]); if ($now->between($parkOpen, $parkClose)) { // Fetch the ride data from the API $rideData = $rideQuery->fetchRideData($park->api_name); // Loop through each ride foreach ($rideData as $data) { // Does the ride already exist here? If not, create it if (count(Ride::where('api_name', $data->name)->get()) == 0) { // Create the new ride $newRide = new Ride(); $newRide->api_name = $data->name; // Attach it to the park $park->rides()->save($newRide); } // New wait time object so we can query and create the new object $wait = new WaitTime(); $ride = Ride::where('api_name', $data->name)->get()->first(); // Set the time for the entry (nearest quarter hour) $minutes = $wait->roundTime($now->format('i')); $roundedTime = Carbon::createFromFormat('Y-m-d H:i:s', $now->format('Y') . '-' . $now->format('m') . '-' . $now->format('d') . ' ' . $now->format('H') . ':' . $minutes . ':00', 'America/Los_Angeles'); // Does this entry already exist for this ride? if (count(WaitTime::where('ride_id', $ride->id)->where('datetime', $roundedTime)->get()) == 0) { if (!empty($data->waitTime)) { // Convert the string from the API to an integer $waitTime = $wait->calcWaitTime($data->waitTime); // Create the new wait time entry $wait->wait = !empty($waitTime) ? $waitTime : 0; $wait->status = $wait->calcStatus($data->waitTime); $wait->datetime = $roundedTime; $ride->waittimes()->save($wait); } } } } } // echo the ride data the api sent over return \Response::json($rideData); }
/** * Goes through and stores all the rides. * @param mixed $rides * @return bool */ public function store_rides($rides = false) { if (!$rides) { $rides = $this->get_user_activity()->history; } foreach ($rides as $ride) { /** * Look for a ride with this URID. * If none found, create it. */ try { Ride::where('urid', $ride->uuid)->firstOrFail(); } catch (ModelNotFoundException $e) { Ride::firstOrCreate(['utdb_id' => $this->id, 'uuid' => $this->uuid, 'urid' => $ride->uuid, 'product_id' => isset($ride->product_id) ? $ride->product_id : '0', 'distance' => $ride->distance, 'request_time' => $ride->request_time, 'start_time' => $ride->start_time, 'end_time' => $ride->end_time, 'ride_time' => $ride->end_time - $ride->start_time, 'wait_time' => $ride->start_time - $ride->request_time, 'distance_hour' => $ride->distance / (($ride->end_time - $ride->start_time) / 3600)]); } } return true; }