Esempio n. 1
0
 /**
  * The vast majority of the custom find types actually follow the same format
  * so there was little point explicitly writing them all out. Instead, if the
  * method corresponding to the custom find type doesn't exist, the options are
  * applied to the model's request property here and then we just call
  * parent::find('all') to actually trigger the request and return the response
  * from the API.
  *
  * In addition, if you try to fetch a timeline that supports paging, but you
  * don't specify paging params, you really want all tweets in that timeline
  * since time imemoriam. But twitter will only return a maximum of 200 per
  * request. So, we make multiple calls to the API for 200 tweets at a go, for
  * subsequent pages, then merge the results together before returning them.
  *
  * Twitter's API uses a count parameter where in CakePHP we'd normally use
  * limit, so we also copy the limit value to count so we can use our familiar
  * params.
  *
  * @param string $type
  * @param array $options
  * @return mixed
  */
 public function find($type = 'first', $options = array())
 {
     if (method_exists($this, '_find' . Inflector::camelize($type))) {
         return parent::find($type, $options);
     }
     $this->_setupRequest($type, $options);
     return parent::find('all', $options);
 }
Esempio n. 2
0
 /**
  * The vast majority of the custom find types actually follow the same format
  * so there was little point explicitly writing them all out. Instead, if the
  * method corresponding to the custom find type doesn't exist, the options are
  * applied to the model's request property here and then we just call
  * parent::find('all') to actually trigger the request and return the response
  * from the API.
  *
  * In addition, if you try to fetch a timeline that supports paging, but you
  * don't specify paging params, you really want all tweets in that timeline
  * since time imemoriam. But twitter will only return a maximum of 200 per
  * request. So, we make multiple calls to the API for 200 tweets at a go, for
  * subsequent pages, then merge the results together before returning them.
  *
  * Twitter's API uses a count parameter where in CakePHP we'd normally use
  * limit, so we also copy the limit value to count so we can use our familiar
  * params.
  *
  * @param string $type
  * @param array $options
  * @return mixed
  */
 public function find($type = 'first', $options = array())
 {
     if (!empty($options['limit']) && empty($options['count'])) {
         $options['count'] = $options['limit'];
     }
     if ($type != 'list' && method_exists($this, '_find' . Inflector::camelize($type))) {
         return parent::find($type, $options);
     }
     $this->_setupRequest($type, $options);
     return parent::find('all', $options);
 }
 /**
  * Deletes a message
  *
  * @param integer $id Id of the tweet to be deleted
  * @param boolean $cascade
  * @return boolean
  */
 public function delete($id = null, $cascade = true)
 {
     $this->request = array('uri' => array('path' => '1.1/direct_messages/destroy/' . $id), 'method' => 'POST', 'auth' => true);
     return parent::delete($id, $cascade);
 }
Esempio n. 4
0
 /**
  *
  */
 public function onError()
 {
     if (isset($this->response) && is_string($this->response)) {
         if (preg_match('/<\\?xml /', $this->response)) {
             App::import('Core', 'Xml');
             $Xml = new Xml($this->response);
             $this->response = $Xml->toArray(false);
             $Xml->__destruct();
             $Xml = null;
             unset($Xml);
             if (isset($this->response['hash'])) {
                 $this->response = $this->response['hash'];
             }
         } else {
             $this->response = array('error' => $this->response);
         }
     }
     parent::onError();
 }
Esempio n. 5
0
 /**
  *
  * @param string $type
  * @param array $options
  * @return array
  * @throws InvalidArgumentException
  */
 public function find($type = 'first', $options = array())
 {
     if (is_string($type) && empty($options)) {
         $options = $type;
         $type = 'tweets';
     }
     if ($type === 'search') {
         $type = 'tweets';
     }
     if (is_string($options)) {
         $options = array('q' => $options);
     }
     if (empty($options['q'])) {
         throw new InvalidArgumentException(__d('twim', 'You must enter a query.'));
     }
     $defaults = array('count' => $this->maxCount, 'limit' => $this->resultLimit, 'strict' => false);
     $options = array_merge($defaults, $options);
     if (!empty($options['limit']) && $options['limit'] <= $this->maxCount) {
         $options['count'] = $options['limit'];
     }
     if (empty($options['page'])) {
         $options['page'] = 1;
         $results = array();
         try {
             while (($page = $this->find($type, $options)) != false) {
                 $results = array_merge($results, $page);
                 if (count($page) < $options['count']) {
                     break;
                 }
                 if (!empty($options['limit']) && count($results) >= $options['limit']) {
                     $results = array_slice($results, 0, $options['limit']);
                     break;
                 }
                 // get next page
                 if (isset($this->response['search_metadata']['next_results'])) {
                     parse_str(parse_url($this->response['search_metadata']['next_results'], PHP_URL_QUERY), $nextPage);
                     $options = am($options, $nextPage);
                 } elseif (in_array('since_id', $this->allowedFindOptions[$type]) && !empty($options['since_id'])) {
                     if (PHP_INT_SIZE === 4 && extension_loaded('bcmath')) {
                         $options['since_id'] = bcadd($page[0]['id_str'], '1');
                         // for 32bit
                     } else {
                         $options['since_id'] = $page[0]['id'] + 1;
                     }
                 } elseif (in_array('max_id', $this->allowedFindOptions[$type])) {
                     if (PHP_INT_SIZE === 4 && extension_loaded('bcmath')) {
                         $options['max_id'] = bcsub($page[count($page) - 1]['id_str'], '1');
                         // for 32bit
                     } else {
                         $options['max_id'] = $page[count($page) - 1]['id'] - 1;
                     }
                 }
                 // adjust count
                 if (!empty($options['limit']) && $options['limit'] < count($results) + $options['count']) {
                     $options['count'] = $options['limit'] - count($results);
                 }
             }
         } catch (RuntimeException $e) {
             if ($options['strict']) {
                 throw $e;
             }
             $this->log($e->getMessage(), LOG_DEBUG);
         }
         return $results;
     }
     $this->_setupRequest($type, $options);
     $results = parent::find('all', $options);
     $results = isset($results['statuses']) ? $results['statuses'] : $results;
     return $results;
 }
Esempio n. 6
0
 /**
  * Deletes a tweet
  *
  * @param integer $id Id of the tweet to be deleted
  * @param boolean $cascade
  * @return boolean
  */
 public function delete($id = null, $cascade = true)
 {
     $this->request = array('uri' => array('path' => $this->apiUrlBase . 'destroy/' . $id), 'method' => 'POST', 'auth' => true);
     return parent::delete($id, $cascade);
 }