Esempio n. 1
0
 /**
  * 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);
 }
Esempio n. 2
0
 /**
  * Display a listing of the resource.
  *
  * @return Response
  */
 public function index()
 {
     $parks = Park::all();
     $data['parks'] = $parks;
     return View::make('parks.list', $data);
 }