Permalink
Browse files

added Statuses class, Response class, changed how the url is construc…

…ted in the Request and OAuth classes
  • Loading branch information...
1 parent f4f7aee commit aa136f668dad80afcf6096ed8fc3a2a0348d4917 @colindecarlo committed Apr 10, 2011
Showing with 191 additions and 54 deletions.
  1. +1 −0 .gitignore
  2. +11 −2 lib/YATA/Request.php
  3. +17 −11 lib/YATA/Request/OAuth.php
  4. +45 −0 lib/YATA/Response.php
  5. +64 −0 lib/YATA/Statuses.php
  6. +0 −41 test/lib/Request/OAuthTest.php
  7. +53 −0 test/lib/StatusesTest.php
View
@@ -0,0 +1 @@
+*.swp
View
@@ -117,12 +117,19 @@ public function addParameters(array $params)
public function send()
{
- $url = strtr($this->_config['request_url'], array('%format%' => $this->_config['format']));
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($this->_parameters));
+ $url = strtr($this->_config['request_url'], array('%format%' => $this->_config['format']));
+ $queryString = http_build_query($this->_parameters);
+ if ($this->_config['http_request_type'] == 'GET') {
+ $url .= '?' . $queryString;
+ } else {
+ curl_setopt($ch, CURLOPT_POSTFIELDS, $queryString);
+ }
+
+ curl_setopt($ch, CURLOPT_URL, $url);
// since the default curl request is GET we only have to change it if its
// *not* GET
@@ -150,6 +157,8 @@ public function send()
throw new Exception(curl_error($ch));
}
+ curl_close($ch);
+
return $response;
}
View
@@ -20,6 +20,7 @@ class OAuth extends Request
private $_consumerSecretKey = null;
private $_consumerKey = null;
private $_oauthTokenSecret = null;
+ private $_oauthToken = null;
protected function _init()
{
@@ -35,6 +36,7 @@ protected function _init()
public function setOauthTokenSecret($secret)
{
$this->_oauthTokenSecret = $secret;
+ return $this;
}
public function getOauthTokenSecret()
@@ -45,6 +47,7 @@ public function getOauthTokenSecret()
public function setConsumerSecretKey($key)
{
$this->_consumerSecretKey = $key;
+ return $this;
}
public function getConsumerSecretKey()
@@ -55,14 +58,25 @@ public function getConsumerSecretKey()
public function setConsumerKey($key)
{
$this->_consumerKey = $key;
- $this->setOAuthHeader('oauth_consumer_key',$key,true);
+ return $this->setOAuthHeader('oauth_consumer_key',$key,true);
}
public function getConsumerKey()
{
return $this->_consumerKey;
}
+ public function setOauthToken($token)
+ {
+ $this->_oauthtoken = $token;
+ return $this->setOAuthHeader('oauth_token',$token,true);
+ }
+
+ public function getOauthToken()
+ {
+ return $this->_oauthToken;
+ }
+
public function setOAuthHeaders(array $headers, $overwrite = false)
{
foreach ($headers as $header => $value) {
@@ -120,8 +134,6 @@ private function _generateSignatureBaseString()
$baseString .= implode('%26', $encodedParams);
- var_dump($baseString);
-
return $baseString;
}
@@ -132,13 +144,10 @@ private function _generateOAuthSignature()
if (isset($this->_oauthTokenSecret)) {
$key .= rawurlencode($this->_oauthTokenSecret);
}
- var_dump($key);
-
$hash = hash_hmac('sha1',$this->_generateSignatureBaseString(),$key, true);
$signature = base64_encode($hash);
- var_dump($signature);
return $signature;
}
@@ -159,8 +168,6 @@ private function _getAuthenticationHeader()
$header = 'Authorization: OAuth ' . implode(', ', $gluedHeaders);
- var_dump($header);
-
return $header;
}
@@ -242,13 +249,12 @@ public function send()
$response = curl_exec($ch);
- $req = curl_getinfo($ch);
- print_r($req);
-
if ($response === false) {
throw new Exception(curl_error($ch));
}
+ curl_close($ch);
+
return $response;
}
View
@@ -0,0 +1,45 @@
+<?php
+
+namespace YATA;
+
+class Response {
+
+ private $_rawBody = null;
+ private $_format = null;
+ private $_response = null;
+
+ public function __construct($rawBody = null, $format = 'json')
+ {
+
+ $this->_format = $format;
+
+ if(isset($rawBody)) {
+ $this->setRawBody($rawBody);
+ }
+
+ }
+
+ public function setRawBody($raw)
+ {
+ $this->_rawBody = $raw;
+ $this->_parseResponse();
+ }
+
+ public function getResponse()
+ {
+ return $this->_response;
+ }
+
+ private function _parseResponse()
+ {
+ $parseMethod = sprintf('_parseAs%s',ucwords($this->_format));
+ $this->_response = $this->$parseMethod();
+ }
+
+ private function _parseAsJson()
+ {
+ return json_decode($this->_rawBody, true);
+ }
+
+}
+
View
@@ -0,0 +1,64 @@
+<?php
+
+namespace YATA;
+
+use YATA\YATA,
+ YATA\Request\OAuth,
+ YATA\Response;
+
+class Statuses {
+
+ private $_request;
+ private $_response;
+
+ public function __construct(OAuth $request = null)
+ {
+ $this->_response = new Response();
+ if (!isset($request)) {
+ $request = new OAuth();
+ }
+
+ $this->setRequest($request);
+ }
+
+ public function getRequest()
+ {
+ return $this->_request;
+ }
+
+ public function setRequest(OAuth $request)
+ {
+ $this->_request = $request;
+ }
+
+ public function update($status, array $optParams = array())
+ {
+ $this->_request->setRequestUrl(YATA::URL_STATUSES_UPDATE);
+ $this->_request->setHttpRequestType('POST');
+
+ return $this->_makeRequest(array_merge(array('status' => $status), $optParams));
+
+ }
+
+ public function mentions(array $optParams = array())
+ {
+ $this->_request->setRequestUrl(YATA::URL_STATUSES_MENTIONS);
+ $this->_request->setHttpRequestType('GET');
+
+ return $this->_makeRequest($optParams);
+ }
+
+ private function _makeRequest(array $params)
+ {
+
+ $this->_request->setParameters($params);
+ $rawResponse = $this->_request->send();
+
+ $this->_response->setRawBody($rawResponse);
+
+ return $this->_response->getResponse();
+
+ }
+
+}
+
@@ -38,46 +38,5 @@ public function testGetOAuthRequestTokenWorks()
}
- public function testCanSendStatusUpdate()
- {
-
- $config = array('http_request_type' => 'POST',
- 'request_url' => YATA::URL_STATUSES_UPDATE,
- 'consumer_key' => '<YOUR CONSUMER KEY>',
- 'consumer_secret_key' => '<YOUR CONSUMER SECRET KEY>',
- 'oauth_token_secret' => '<YOUR OAUTH TOKEN SECRET>',
- );
-
- $request = new OAuth($config);
- $request->setOAuthHeader('oauth_token','<YOUR OAUTH TOKEN>')
- ->setOAuthHeader('oauth_callback',urlencode('http://localhost:3005/the_dance/process_callback?service_provider_id=11'));
- $request->addParameter('status',"Tweeting with #YATA");
-
- $response = $request->send();
-
- $regexp = '/"text":"Tweeting with #YATA"/';
- $this->assertRegExp($regexp, $response);
-
- }
-
- public function testCanGetMentions()
- {
-
- $config = array('request_url' => YATA::URL_STATUSES_MENTIONS,
- 'consumer_key' => '<YOUR CONSUMER KEY>',
- 'consumer_secret_key' => '<YOUR CONSUMER SECRET KEY>',
- 'oauth_token_secret' => '<YOUR OAUTH TOKEN SECRET>',
- );
-
- $request = new OAuth($config);
- $request->setOAuthHeader('oauth_token','<YOUR OAUTH TOKEN>')
- ->setOAuthHeader('oauth_callback',urlencode('http://localhost:3005/the_dance/process_callback?service_provider_id=11'));
-
- $response = $request->send();
-
- echo $response . "\n";
-
- }
-
}
View
@@ -0,0 +1,53 @@
+<?php
+
+/*
+ * This file is part of the YATA package.
+ *
+ * (c) Colin DeCarlo <colin@thedecarlos.ca>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace YATA\Test;
+
+require __DIR__ . '/../bootstrap.php';
+
+use YATA\Statuses;
+
+class StatusesTest extends \PHPUnit_Framework_TestCase
+{
+
+ public function testCanSendStatusUpdate()
+ {
+
+ $statuses = new Statuses();
+ $statuses->getRequest()
+ ->setConsumerKey('<YOUR CONSUMER KEY>')
+ ->setConsumerSecretKey('<YOUR CONSUMER SECRET KEY>')
+ ->setOauthTokenSecret('<YOUR OAUTH SECRET TOKEN>')
+ ->setOauthToken('<YOUR OAUTH TOKEN>');
+ $tweet = $statuses->update("Tweeting with #YATA");
+ $this->assertTrue(is_array($tweet));
+ $this->assertArrayHasKey('text', $tweet);
+
+ }
+
+ public function testCanGetMentions()
+ {
+
+ $statuses = new Statuses();
+ $statuses->getRequest()
+ ->setConsumerKey('<YOUR CONSUMER KEY>')
+ ->setConsumerSecretKey('<YOUR CONSUMER SECRET KEY>')
+ ->setOauthTokenSecret('<YOUR OAUTH SECRET TOKEN>')
+ ->setOauthToken('<YOUR OAUTH TOKEN>');
+ $mentions = $statuses->mentions();
+
+ $this->assertTrue(is_array($mentions));
+ $this->assertArrayHasKey('text', $mentions[0]);
+
+ }
+
+}
+

0 comments on commit aa136f6

Please sign in to comment.