10 defined(
'JPATH_PLATFORM') or die();
50 $this->options = isset($options) ? $options :
new JRegistry;
51 $this->client = isset($client) ? $client :
new JHttp($this->options);
52 $this->oauth = $oauth;
66 public function checkRateLimit($resource = null, $action = null)
69 $rate_limit = $this->getRateLimit($resource);
71 $property =
'/' . $resource;
73 if (!is_null($action))
75 $property .=
'/' . $action;
78 if ($rate_limit->resources->$resource->$property->remaining == 0)
81 throw new RuntimeException(
'This server has exceed the Twitter API rate limit for the given period. The limit will reset at '
82 . $rate_limit->resources->$resource->$property->reset
99 public function fetchUrl($path, $parameters = null)
103 foreach ($parameters as $key => $value)
105 if (strpos($path,
'?') ===
false)
107 $path .=
'?' . $key .
'=' . $value;
111 $path .=
'&' . $key .
'=' . $value;
117 if (strpos($path,
'http://search.twitter.com/search.json') ===
false)
119 $uri =
new JUri($this->options->get(
'api.url') . $path);
123 $uri =
new JUri($path);
126 return (
string) $uri;
138 public function getRateLimit($resource)
141 $path =
'/application/rate_limit_status.json';
143 if (!is_null($resource))
145 return $this->sendRequest($path,
'GET', array(
'resources' => $resource));
148 return $this->sendRequest($path);
164 public function sendRequest($path, $method =
'GET', $data = array(), $headers = array())
167 $token = $this->oauth->getToken();
170 $parameters[
'oauth_token'] = $token[
'key'];
173 $response = $this->oauth->oauthRequest($this->fetchUrl($path), $method, $parameters, $data, $headers);
175 if (strpos($path,
'update_with_media') !==
false)
178 $response_headers = $response->headers;
180 if ($response_headers[
'x-mediaratelimit-remaining'] == 0)
183 throw new RuntimeException(
'This server has exceed the Twitter API media rate limit for the given period. The limit will reset in '
184 . $response_headers[
'x-mediaratelimit-reset'] .
'seconds.'
189 if (strpos($response->body,
'redirected') !==
false)
191 return $response->headers[
'Location'];
194 return json_decode($response->body);
206 public function getOption($key)
208 return $this->options->get($key);
221 public function setOption($key, $value)
223 $this->options->set($key, $value);