Example #1
0
 public function google()
 {
     $google = new GoogleAPI();
     $states = Location::states;
     $donePresident = false;
     foreach ($states as $state_abbrev => $state_name) {
         echo "state: " . $state_name . "\n";
         $req = $google->address($state_name)->then(function ($data) use($donePresident) {
             foreach ($data->reps as $rep) {
                 if ($rep->office == 'President' && !$donePresident) {
                     unset($rep->state);
                     $rep->save();
                     $donePresident = true;
                 } else {
                     if (Representative::exists($rep)) {
                         $old = Representative::find($rep);
                         foreach ($rep->getAttributes() as $k => $v) {
                             if (!empty($v)) {
                                 $old->{$k} = $v;
                             }
                         }
                         $old->save();
                     } else {
                         if ($rep->office == 'Governor') {
                             $rep->save();
                         }
                     }
                 }
             }
         });
         $req->wait();
     }
 }
Example #2
0
 public function update(array $reps)
 {
     $divisions = [];
     foreach ($reps as $rep) {
         if (!empty($rep->division) && !in_array($rep->division, $divisions)) {
             array_push($divisions, $rep->division);
         }
     }
     $requests = array_map(function ($d) {
         return GoogleAPI::division($d);
     }, $divisions);
     $results = Promise\unwrap($requests);
     return array_collapse(array_map(function ($result) {
         return $result->reps;
     }, $results));
 }