Sample 4 - Custom Blocks Try it!
This sample is different from the previous three. In this one we create a custom block that changes the output of the Calais Popfly Block.
Recall that the calaisTree member of the CPB output is intended for use within custom blocks. This sample block uses this member to extract only entities identified by OpenCalais, as opposed to entities and events in CPB. It also shortens the calaisText member with a more compact format that does not display the count of each entity.
Block Description
The following is the sample block's description:
<?xml version="1.0"?> <!-- Sample custom block using Calais Popfly block --> <!-- Changes presentation and shows entities only --> <block xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://www.popfly.com/schemas/blockschema.xsd" class="SampleCustomBlockClass"> <operations> <operation name="shorten"> <description>Shorten output of Calais Popfly Block and show entities only</description> <inputs> <input name="calaisData" required="true" type="custom"> <description>The data from Calais Popfly Block to be shortened</description> </input> </inputs> <outputs> <output isArray="true" type="custom" object="TwitterStatusWithMetadata"/> </outputs> </operation> </operations> <objects> <object name="TwitterStatusWithMetadata"> <field name="messageID" type="id" isArray="false" /> <field name="text" type="description" isArray="false" /> <field name="createdAt" type="date" isArray="false" /> <field name="userID" type="id" isArray="false" /> <field name="screenName" type="userName" isArray="false" /> <field name="name" type="name" isArray="false" /> <field name="location" type="location" isArray="false" /> <field name="description" type="description" isArray="false" /> <field name="imageUrl" type="thumbnailImageUrl" isArray="false" /> <field name="url" type="url" isArray="false" /> <field name="calaisText" type="string" isArray="false" /> <field name="calaisTree" type="custom" isArray="false" object="CalaisMetadata" /> </object> <object name="CalaisMetadata"> <field name="entities" type="custom" isArray="true" object="CalaisEntity" /> <field name="events" type="custom" isArray="true" object="CalaisEvent" /> </object> <object name="CalaisEntity"> <field name="type" type="string" isArray="false" /> <field name="count" type="string" isArray="false" /> <field name="relevance" type="string" isArray="false" /> <field name="name" type="string" isArray="false" /> </object> <object name="CalaisEvent"> <field name="count" type="string" isArray="false" /> <field name="name" type="string" isArray="false" /> </object> </objects> <suggest output="" input="calais"/> </block>
The custom block has a single operation called shorten. Its input is the output of CPB, i.e. an object of type TwitterStatusWithMetadata. The <objects> section is taken as is from the description of CPB.
Block Code
The code of the custom block is as follows:
function SampleCustomBlockClass() { } SampleCustomBlockClass.prototype.shorten = function(calaisData) { if (calaisData == null) { return null; } calaisData.calaisText = ''; var tree = calaisData.calaisTree; if (tree == null || tree.entities == undefined || tree.events == undefined || tree.entities.length == 0) { return calaisData; } calaisData.calaisText += 'Found: '; var entity = null; var first = true; // List entities only for (var i = 0; i < tree.entities.length; i++) { entity = tree.entities[i]; if (!first) { calaisData.calaisText += ', '; } first = false; calaisData.calaisText += entity.type; calaisData.calaisText += ': '; calaisData.calaisText += entity.name; } return calaisData; };
The loop in the code above processes the entities member of the calaisTree member. It also presents the type and name of each entity with no count information.
Block Use
By duplicating the mashup in sample 2, we can apply the new block to the result. We disconnect the Calais Popfly Block from the News Reader block. We connect the output of the Calais Popfly Block to the custom block and the output of the custom block to the News Reader block.
As the calaisData argument we set '[entire TwitterStatusWithMetadata object]'.
The following image provides an example of the result:
