Permalink
Browse files

Cleaned up the historics implementation.

Added SSL support.
Updated docs.
Bumped version to 2.0.0.
  • Loading branch information...
1 parent e105f5f commit 5b4e9057383bb1919e207edc5c4c539e7217cf1f Stuart Dallas committed Jun 17, 2012
View
@@ -18,16 +18,33 @@ prints the content to the screen as they come in.
```php
<?php
- require 'lib/datasift.php';
- $user = new DataSift_User('your username', 'your api_key');
- $def = $user->createDefinition('interaction.content contains "datasift"');
- $consumer = $def->getConsumer(
- DataSift_StreamConsumer::TYPE_HTTP,
- function($consumer, $data) {
- echo $data['interaction']['content']."\n";
- }
- );
- $consumer->consume();
+ // Load the library
+ require 'lib/datasift.php';
+ // An object of this type will receive events
+ class EventHandler implements DataSift_IStreamConsumerEventHandler
+ {
+ public function onInteraction($consumer, $interaction, $hash)
+ {
+ echo $data['interaction']['content']."\n";
+ }
+
+ // Ignore the other events for the purposes of this example.
+ public function onConnect($consumer) { }
+ public function onDeleted($consumer, $interaction, $hash) { }
+ public function onStatus($consumer, $type, $info) { }
+ public function onWarning($consumer, $message) { }
+ public function onError($consumer, $message) { }
+ public function onDisconnect($consumer) { }
+ public function onStopped($consumer, $reason) { }
+ }
+ // Create the user
+ $user = new DataSift_User('your username', 'your api_key');
+ // Create a definition looking for the word "datasift"
+ $def = $user->createDefinition('interaction.content contains "datasift"');
+ // Get an HTTP stream consumer for that definition
+ $consumer = $def->getConsumer(DataSift_StreamConsumer::TYPE_HTTP, new EventHandler());
+ // Consume it - this will not return unless the stream gets disconnected
+ $consumer->consume();
?>
```
@@ -38,9 +55,13 @@ http://dev.datasift.com/docs/targets/twitter/tweet-output-format
Requirements
------------
-* PHP 5 with the cURL extension enabled
+* PHP 5 with the cURL extension enabled and openssl for SSL support
* JSON (included in PHP 5.2+, otherwise use http://pecl.php.net/package/json)
+The library will use SSL connections by default. While we recommend using SSL
+you may disable it if required by passing false as the third parameter when
+creating a user, or by calling $user->enableSSL(false) on the user object.
+
License
-------
@@ -52,6 +73,19 @@ more details.
Changelog
---------
+* v.2.0.0 Changed event handling to an object instead of functions. Implemented
+ historic streaming. Added SSL support for streams. (2012-06-18)
+
+ Consumers no longer take functions for event handling. Instead you define a
+ class that implements the DataSift_IStreamConsumerEventHandler interface and
+ pass an instance of that. In addition to switching to an object-based event
+ handler we have also introduced the following new events: onConnect,
+ onDisconnect and onStatus.
+
+ SSL is enabled by default and can be disabled by passing false as the third
+ parameter to the User constructor, or calling enableSSL(false) on the User
+ object.
+
* Added the develop branch as required by git flow (2012-05-24)
* v.1.3.0 Improved error handling (2012-03-08)
@@ -62,6 +62,24 @@ public function onDeleted($consumer, $interaction, $hash)
. PHP_EOL.'--'.PHP_EOL;
}
+ /**
+ * Called when a status message is received.
+ *
+ * @param DataSift_StreamConsumer $consumer The consumer sending the
+ * event.
+ * @param string $type The status type.
+ * @param array $info The data sent with the
+ * status message.
+ */
+ public function onStatus($consumer, $type, $info)
+ {
+ switch ($type) {
+ default:
+ echo 'STATUS: '.$type.PHP_EOL;
+ break;
+ }
+ }
+
/**
* Called when a warning occurs or is received down the stream.
*
View
@@ -54,6 +54,20 @@ public function onDeleted($consumer, $interaction, $hash)
echo 'X';
}
+ /**
+ * Called when a status message is received.
+ *
+ * @param DataSift_StreamConsumer $consumer The consumer sending the
+ * event.
+ * @param string $type The status type.
+ * @param array $info The data sent with the
+ * status message.
+ */
+ public function onStatus($consumer, $type, $info)
+ {
+ // Ignored for this example
+ }
+
/**
* Called when a warning occurs or is received down the stream.
*
View
@@ -29,18 +29,25 @@ class EventHandler implements DataSift_IStreamConsumerEventHandler
/**
* Called when the stream is connected.
*
- * @param DataSift_StreamConsumer $consumer The consumer object.
+ * @param DataSift_StreamConsumer $consumer The consumer sending the event.
+ *
+ * @return void
*/
public function onConnect($consumer)
{
echo 'Connected'.PHP_EOL;
}
/**
- * Handle incoming data.
+ * Called for each interaction consumed.
+ *
+ * @param DataSift_StreamConsumer $consumer The consumer sending the
+ * event.
+ * @param array $interaction The interaction data.
+ * @param string $hash The hash of the stream that
+ * matched this interaction.
*
- * @param DataSift_StreamConsumer $consumer The consumer object.
- * @param array $interaction The interaction data.
+ * @return void
*/
public function onInteraction($consumer, $interaction, $hash)
{
@@ -55,10 +62,15 @@ public function onInteraction($consumer, $interaction, $hash)
}
/**
- * Handle DELETE requests.
+ * Called for each deletion notification consumed.
*
- * @param DataSift_StreamConsumer $consumer The consumer object.
- * @param array $interaction The interaction data.
+ * @param DataSift_StreamConsumer $consumer The consumer sending the
+ * event.
+ * @param array $interaction The interaction data.
+ * @param string $hash The hash of the stream that
+ * matched this interaction.
+ *
+ * @return void
*/
public function onDeleted($consumer, $interaction, $hash)
{
@@ -67,22 +79,44 @@ public function onDeleted($consumer, $interaction, $hash)
. '. Please delete it from your archive.';
}
+ /**
+ * Called when a status message is received.
+ *
+ * @param DataSift_StreamConsumer $consumer The consumer sending the
+ * event.
+ * @param string $type The status type.
+ * @param array $info The data sent with the
+ * status message.
+ */
+ public function onStatus($consumer, $type, $info)
+ {
+ switch ($type) {
+ default:
+ echo 'STATUS: '.$type.PHP_EOL;
+ break;
+ }
+ }
+
/**
* Called when a warning occurs or is received down the stream.
*
- * @param DataSift_StreamConsumer $consumer The consumer object.
+ * @param DataSift_StreamConsumer $consumer The consumer sending the event.
* @param string $message The warning message.
+ *
+ * @return void
*/
public function onWarning($consumer, $message)
{
echo 'WARNING: '.$message.PHP_EOL;
}
/**
- * Called when a error occurs or is received down the stream.
+ * Called when an error occurs or is received down the stream.
*
- * @param DataSift_StreamConsumer $consumer The consumer object.
+ * @param DataSift_StreamConsumer consumer The consumer sending the event.
* @param string $message The error message.
+ *
+ * @return void
*/
public function onError($consumer, $message)
{
@@ -92,18 +126,22 @@ public function onError($consumer, $message)
/**
* Called when the stream is disconnected.
*
- * @param DataSift_StreamConsumer $consumer The consumer object.
+ * @param DataSift_StreamConsumer $consumer The consumer sending the event.
+ *
+ * @return void
*/
public function onDisconnect($consumer)
{
echo 'Disconnected'.PHP_EOL;
}
/**
- * Called when the consumer has stopped.
+ * Called when the consumer stops for some reason.
*
- * @param DataSift_StreamConsumer $consumer The consumer object.
+ * @param DataSift_StreamConsumer consumer The consumer sending the event.
* @param string $reason The reason the consumer stopped.
+ *
+ * @return void
*/
public function onStopped($consumer, $reason)
{
View
@@ -87,6 +87,36 @@ public function onDeleted($consumer, $interaction, $hash)
// removing them from your storage system.
}
+ /**
+ * Called when a status message is received.
+ *
+ * @param DataSift_StreamConsumer $consumer The consumer sending the
+ * event.
+ * @param string $type The status type.
+ * @param array $info The data sent with the
+ * status message.
+ */
+ public function onStatus($consumer, $type, $info)
+ {
+ switch ($type) {
+ case 'initialised':
+ case 'connected':
+ // Ignored
+ break;
+
+ case 'info':
+ if (isset($info['progress'])) {
+ echo $info['progress'].'% complete'.PHP_EOL;
+ break;
+ }
+ // Deliberate fall-through
+
+ default:
+ echo 'STATUS: '.$type.PHP_EOL;
+ break;
+ }
+ }
+
/**
* Called when a warning occurs or is received down the stream.
*
View
@@ -62,6 +62,24 @@ public function onDeleted($consumer, $interaction, $hash)
. '. Please delete it from your archive.'.PHP_EOL.'--'.PHP_EOL;
}
+ /**
+ * Called when a status message is received.
+ *
+ * @param DataSift_StreamConsumer $consumer The consumer sending the
+ * event.
+ * @param string $type The status type.
+ * @param array $info The data sent with the
+ * status message.
+ */
+ public function onStatus($consumer, $type, $info)
+ {
+ switch ($type) {
+ default:
+ echo 'STATUS: '.$type.PHP_EOL;
+ break;
+ }
+ }
+
/**
* Called when a warning occurs or is received down the stream.
*
@@ -41,7 +41,7 @@ static public function call($username, $api_key, $endpoint, $params = array(), $
{
// Curl is required
if (!function_exists('curl_init')) {
- throw new DataSift_Exception_NotYetImplemented('Curl is required for DataSift_User::callAPI');
+ throw new DataSift_Exception_NotYetImplemented('Curl is required for DataSift_ApiClient');
}
// Build the full endpoint URL
@@ -61,6 +61,19 @@ public function onInteraction($consumer, $interaction, $hash);
*/
public function onDeleted($consumer, $interaction, $hash);
+ /**
+ * Called for each status message received.
+ *
+ * @param DataSift_StreamConsumer $consumer The consumer sending the
+ * event.
+ * @param string $type The status type.
+ * @param array $info The data sent with the
+ * status message.
+ *
+ * @return void
+ */
+ public function onStatus($consumer, $type, $info);
+
/**
* Called when a warning occurs or is received down the stream.
*
@@ -215,6 +215,19 @@ protected function onDeleted($interaction, $hash = false)
$this->_eventHandler->onDeleted($this, $interaction, $hash);
}
+ /**
+ * Called for each status message received from the stream.
+ *
+ * @param string $type The status type.
+ * @param array $info The data received along with the status message.
+ *
+ * @return void
+ */
+ protected function onStatus($type, $info = array())
+ {
+ $this->_eventHandler->onStatus($this, $type, $info);
+ }
+
/**
* This is called when an error notification is received on a stream
* connection.
Oops, something went wrong.

0 comments on commit 5b4e905

Please sign in to comment.