Skip to content
This repository has been archived by the owner on Dec 4, 2018. It is now read-only.

Commit

Permalink
Adding exceptions to EpiOAuth and implementing them in EpiTwitter.
Browse files Browse the repository at this point in the history
Closes gh-13
Closes gh-14
  • Loading branch information
jmathai committed May 1, 2009
1 parent 09c1256 commit 6392fde
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 9 deletions.
33 changes: 29 additions & 4 deletions php/EpiOAuth.php
Expand Up @@ -5,6 +5,7 @@ class EpiOAuth

protected $requestTokenUrl;
protected $accessTokenUrl;
protected $authenticateUrl;
protected $authorizeUrl;
protected $consumerKey;
protected $consumerSecret;
Expand All @@ -18,10 +19,14 @@ public function getAccessToken()
return new EpiOAuthResponse($resp);
}

public function getAuthorizationUrl()
public function getAuthenticateUrl()
{
$retval = "{$this->authorizeUrl}?";
$token = $this->getRequestToken();
return $this->authenticateUrl . '?oauth_token=' . $token->oauth_token;
}

public function getAuthorizationUrl()
{
$token = $this->getRequestToken();
return $this->authorizeUrl . '?oauth_token=' . $token->oauth_token;
}
Expand Down Expand Up @@ -219,8 +224,8 @@ public function __construct($resp)

public function __get($name)
{
if($this->__resp->code < 200 || $this->__resp->code > 299)
return false;
if($this->__resp->code != 200)
EpiOAuthException::raise($this->__resp->data, $this->__resp->code);

parse_str($this->__resp->data, $result);
foreach($result as $k => $v)
Expand All @@ -231,3 +236,23 @@ public function __get($name)
return $result[$name];
}
}

class EpiOAuthException extends Exception
{
public static function raise($message, $code)
{
switch($code)
{
case 400:
throw new EpiOAuthBadRequestException($message, $code);
case 401:
throw new EpiOAuthUnauthorizedException($message, $code);
default:
throw new EpiOAuthException($message, $code);
}
}
}


class EpiOAuthBadRequestException extends EpiOAuthException{}
class EpiOAuthUnauthorizedException extends EpiOAuthException{}
12 changes: 8 additions & 4 deletions php/EpiTwitter.php
Expand Up @@ -15,6 +15,7 @@ class EpiTwitter extends EpiOAuth
protected $requestTokenUrl= 'http://twitter.com/oauth/request_token';
protected $accessTokenUrl = 'http://twitter.com/oauth/access_token';
protected $authorizeUrl = 'http://twitter.com/oauth/authorize';
protected $authenticateUrl= 'http://twitter.com/oauth/authenticate';
protected $apiUrl = 'http://twitter.com';
protected $searchUrl = 'http://search.twitter.com';

Expand Down Expand Up @@ -59,13 +60,13 @@ public function __construct($response)
// Implementation of the IteratorAggregate::getIterator() to support foreach ($this as $...)
public function getIterator ()
{
return new ArrayIterator($this->response);
return new ArrayIterator($this->__obj);
}

// Implementation of Countable::count() to support count($this)
public function count ()
{
return count($this->response);
return count($this->__obj);
}

// Next four functions are to support ArrayAccess interface
Expand Down Expand Up @@ -95,10 +96,13 @@ public function offsetGet($offset)

public function __get($name)
{
if($this->__resp->code != 200)
EpiOAuthException::raise($this->__resp->data, $this->__resp->code);

$this->responseText = $this->__resp->data;
$this->response = json_decode($this->responseText, 1);
$obj = json_decode($this->responseText);
foreach($obj as $k => $v)
$this->__obj = json_decode($this->responseText);
foreach($this->__obj as $k => $v)
{
$this->$k = $v;
}
Expand Down
5 changes: 4 additions & 1 deletion php/tests/EpiTwitterTest.php
Expand Up @@ -67,9 +67,12 @@ function testDirectMessage()
$this->assertTrue(!empty($resp->response['id']), "response id is empty");
}

/**
* @expectedException EpiOAuthException
*/
function testNoRequiredParameter()
{
$resp = $this->twitterObj->post_direct_messagesNew( array ( 'user' => $this->screenName, 'text' => ""));
$resp = $this->twitterObj->post_direct_messagesNew( array ( 'user' => $this->screenName, 'text' => ''));
$this->assertTrue(!empty($resp->response['error']), "An empty direct message should return an error message");

}
Expand Down

0 comments on commit 6392fde

Please sign in to comment.