Skip to content

Commit

Permalink
Adds a consumer class for the pages API. Fixed the events API so that…
Browse files Browse the repository at this point in the history
… it returns the proper data.

Modified   README
* Removed the documentation which shows the main function as findById.

Modified   lib/base.php
* parametersToUrl: Removed the resource, ID and extension parameters.  Now it just inputs a baseUrl parameter that the calling method provides.  It's much more flexible and fits with services like the page service.

Modified   lib/document.php
* Switched all methods to use the new parametersToUrl function.

Modified   lib/event.php
* findAll: Now uses the new parametersToUrl signature.  Now uses the nodeValue and NodeValueInt methods for loading the proper XML response.

Modified   lib/service.php
* Updated version number to 2.2.0.

Added      lib/page.php
* New class that consumes the Page API service to return page numbers and text.

Modified   lib/vuzit.php
* Added the page.php class file.

Modified   test/index.html
* Added notes for the page class.

Modified   test/test.php
* event_load_html: Now properly checks if the page number is -1 which means that there is no page present.
* page_command: Loads and displays the page text information.
  • Loading branch information
bmatzelle committed Feb 24, 2010
1 parent cc200a0 commit 305f861
Show file tree
Hide file tree
Showing 9 changed files with 159 additions and 28 deletions.
2 changes: 1 addition & 1 deletion README
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ Find Document Example - how to load a document
Vuzit_Service::setPublicKey('YOUR_PUBLIC_API_KEY');
Vuzit_Service::setPrivateKey('YOUR_PRIVATE_API_KEY');

$doc = Vuzit_Document::findById("DOCUMENT_ID");
$doc = Vuzit_Document::find("DOCUMENT_ID");
echo "Document id: " . $doc->getId();
echo "Document title: " . $doc->getTitle();
?>
Expand Down
9 changes: 2 additions & 7 deletions lib/base.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,11 @@ protected static function parametersClean($parameters)
/*
Changes an array (hash table) of parameters to a url.
*/
protected static function parametersToUrl($resource, $params, $id = null,
$extension = 'xml')
protected static function parametersToUrl($baseUrl, $params)
{
$params = self::parametersClean($params);

$result = Vuzit_Service::getServiceUrl() . "/" . $resource;
if($id != null) {
$result .= "/" . $id;
}
$result .= "." . $extension . "?";
$result = Vuzit_Service::getServiceUrl() . '/' . $baseUrl . "?";

foreach ($params as $key => &$val) {
$result .= ($key . '=' . rawurlencode($val) . '&');
Expand Down
12 changes: 8 additions & 4 deletions lib/document.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ public static function destroy($webId)
{
$params = self::postParameters("destroy", null, $webId);

$url = self::parametersToUrl('documents', $params, $webId);
$url = self::parametersToUrl("documents/$webId.xml", $params);

$ch = self::curlRequest();
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // only if expecting response
Expand All @@ -116,7 +117,7 @@ public static function destroy($webId)
public static function downloadUrl($webId, $fileExtension)
{
$params = self::postParameters("show", null, $webId);
$result = self::parametersToUrl('documents', $params, $webId, $fileExtension);
$result = self::parametersToUrl("documents/$webId.$fileExtension", $params);

return $result;
}
Expand All @@ -132,7 +133,8 @@ public static function findAll($options = null)
$params["output"] = "summary";

$ch = self::curlRequest();
$url = self::parametersToUrl('documents', $params);
$url = self::parametersToUrl("documents.xml", $params);

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // only if expecting response

Expand Down Expand Up @@ -178,7 +180,9 @@ public static function find($webId, $options = null)
$params = self::postParameters("show", $options, $webId);

$ch = self::curlRequest();
$url = self::parametersToUrl('documents', $params, $webId);

$url = self::parametersToUrl("documents/$webId.xml", $params);

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // only if expecting response

Expand Down
27 changes: 14 additions & 13 deletions lib/event.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ class Vuzit_Event extends Vuzit_Base
{
/*
Constructor. Creates an empty event object. This is not called directly.
Use finaAll to load an instance.
Use findAll to load an instance.
*/
public function __construct() {
$this->web_id = -1;
$this->webId = -1;
$this->event = null;
$this->remoteHost = null;
$this->referer = null;
Expand All @@ -32,7 +32,7 @@ public function getDuration() {
Returns the document web ID.
*/
public function getWebId() {
return $this->web_id;
return $this->webId;
}

/*
Expand Down Expand Up @@ -97,7 +97,7 @@ public static function findAll($webId, $options = null)
$params["web_id"] = $webId;

$ch = self::curlRequest();
$url = self::parametersToUrl("events", $params);
$url = self::parametersToUrl("events.xml", $params);

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // only if expecting response
Expand Down Expand Up @@ -129,15 +129,16 @@ public static function findAll($webId, $options = null)
foreach($xml->event as $node)
{
$event = new Vuzit_Event();
$event->web_id = (string)$node->web_id;
$event->event = (string)$node->event;
$event->remoteHost = (string)$node->remote_host;
$event->referer = (string)$node->referer;
$event->userAgent = (string)$node->user_agent;
$event->custom = (string)$node->custom;
$event->requestedAt = (int)$node->requested_at;
$event->page = $node->page != null ? (int)$node->page : -1;
$event->duration = $node->duration != null ? (int)$node->duration : -1;

$event->webId = self::nodeValue($node->web_id);
$event->event = self::nodeValue($node->event);
$event->remoteHost = self::nodeValue($node->remote_host);
$event->referer = self::nodeValue($node->referer);
$event->userAgent = self::nodeValue($node->user_agent);
$event->custom = self::nodeValue($node->custom);
$event->requestedAt = self::nodeValueInt($node->requested_at);
$event->page = self::nodeValueInt($node->page);
$event->duration = self::nodeValueInt($node->duration);

$result[] = $event;
}
Expand Down
83 changes: 83 additions & 0 deletions lib/page.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php
/*
Class for loading page text. To use this class you need to sign up
for Vuzit first: http://vuzit.com/signup
*/
class Vuzit_Page extends Vuzit_Base
{
/*
Constructor. Creates an empty page object. This is not called directly.
Use findAll to load an instance.
*/
public function __construct() {
$this->pageNumber = -1;
$this->pageText = null;
}

/*
Returns the page number of the page.
*/
public function getNumber() {
return $this->pageNumber;
}

/*
Returns the page text.
*/
public function getText() {
return $this->pageText;
}

/*
Loads an array of pages. It throws a <Vuzit_ClientException> on failure.
*/
public static function findAll($webId, $options = null)
{
if(!$webId) {
throw new Vuzit_ClientException("No webId parameter specified");
}

$params = self::postParameters("index", $options, $webId);

$ch = self::curlRequest();
$url = self::parametersToUrl("documents/$webId/pages.xml", $params);

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // only if expecting response

$xml_string = curl_exec($ch);
$info = curl_getinfo($ch);

if(!$xml_string) {
throw new Vuzit_ClientException('CURL load failed: "' . curl_error($ch) . '"');
}

// Prevent the warnings if the XML is malformed
$xml = @simplexml_load_string($xml_string);
curl_close($ch);

if(!$xml) {
throw new Vuzit_ClientException("Error loading XML response");
}
if($xml->code) {
throw new Vuzit_ClientException($xml->msg, (int)$xml->code);
}
if(!$xml->page) {
throw new Vuzit_ClientException("Unknown error occurred");
}

$result = array();

foreach($xml->page as $node)
{
$page = new Vuzit_Page();
$page->pageNumber = self::nodeValueInt($node->number);
$page->pageText = self::nodeValue($node->text);

$result[] = $page;
}

return $result;
}
}
?>
4 changes: 2 additions & 2 deletions lib/service.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ class Vuzit_Service
private static $publicKey = '';
private static $privateKey = '';
private static $serviceUrl = 'http://vuzit.com';
private static $productName = "VuzitPHP Library 2.1.0";
private static $userAgent = "VuzitPHP Library 2.1.0";
private static $productName = "VuzitPHP Library 2.2.0";
private static $userAgent = "VuzitPHP Library 2.2.0";

// Public static getter and setter methods

Expand Down
1 change: 1 addition & 0 deletions lib/vuzit.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@
include_once(VUZIT_DIR . "/event.php");
include_once(VUZIT_DIR . "/client_exception.php");
include_once(VUZIT_DIR . "/service.php");
include_once(VUZIT_DIR . "/page.php");
?>
10 changes: 10 additions & 0 deletions test/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ <h1>Tests</h1>
<li>upload</li>
<li>delete</li>
<li>load</li>
<li>page</li>
<li>event</li>
<li>search</li>
</ul>
Expand Down Expand Up @@ -54,6 +55,15 @@ <h1>Tests</h1>
<li>id - Document web ID (REQUIRED)</li>
</ul>
</li>
<li>
Page parameters:
<ul>
<li>id - Document id to load</li>
<li>i - Included pages for the request (e.g. "5,7-15")</li>
<li>l - Limit the number of results</li>
<li>o - Offsets the results at this record</li>
</ul>
</li>
<li>
Event parameters:
<ul>
Expand Down
39 changes: 38 additions & 1 deletion test/test.php
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ function event_load_html($list, $options)
}
echo $item->getEvent();

if($item->getPage() != 0) {
if($item->getPage() != -1) {
echo ", p". $item->getPage();
}
if($item->getCustom() != null) {
Expand All @@ -317,6 +317,38 @@ function event_load_html($list, $options)
echo "</ol>";
}

// Runs the page text API command
function page_command()
{
$options = array();

if(get("l") != null) {
$options["limit"] = get("l");
}
if(get("o") != null) {
$options["offset"] = get("o");
}
if(get("i") != null) {
$options["included_pages"] = get("i");
}

$pages = Vuzit_Page::findAll(get("id"), $options);
header_load();

echo printArray($options) . "<br/>";
echo count($pages) . " pages in the document";

for($i = 0; $i < count($pages); $i++)
{
$page = $pages[$i];
?>
<h3>Page #<?php echo $page->getNumber(); ?></h3>
<p><?php echo $page->getText(); ?></p>
<?php
}
footer_load();
}

// Runs the search command
function search_command()
{
Expand Down Expand Up @@ -401,5 +433,10 @@ function search_command()
case "search":
search_command();
break;
case "page":
page_command();
break;
default:
echo "Unknown command: " . get("c");
}
?>

0 comments on commit 305f861

Please sign in to comment.