<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>
<channel>
	<title>Enigmatic Thought &#187; Twitter</title>
	<atom:link href="http://blog.enigmaticthought.com/tag/twitter/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.enigmaticthought.com</link>
	<description>import com.enigmaticThought.blog;</description>
	<lastBuildDate>Fri, 25 Jun 2010 19:54:39 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>How to Flex the Twitter API</title>
		<link>http://blog.enigmaticthought.com/2009/08/how-to-flex-the-twitter-api/</link>
		<comments>http://blog.enigmaticthought.com/2009/08/how-to-flex-the-twitter-api/#comments</comments>
		<pubDate>Fri, 21 Aug 2009 14:54:04 +0000</pubDate>
		<dc:creator>Matt C</dc:creator>
				<category><![CDATA[Flex]]></category>
		<category><![CDATA[Gumbo]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[Twitter]]></category>
		<guid isPermaLink="false">http://enigmaticthought.com/?p=166</guid>
		<description><![CDATA[While sitting in #flex on Freenode a while back, someone asked how to use Twitter within Flex. Since I&#8217;ve meddled with other APIs I offered some advice, but I&#8217;d never personally dealt with Twitter. Douglas Knudsen pointed out at his 360&#124;Indy session that no true web mashup is complete without using twitter so here is [...]]]></description>
			<content:encoded><![CDATA[<p>While sitting in #flex on <a href="http://freenode.net/">Freenode</a> a while back, someone asked how to use Twitter within Flex.  Since I&#8217;ve meddled with other APIs I offered some advice, but I&#8217;d never personally dealt with Twitter.  <a href="http://www.cubicleman.com/">Douglas Knudsen</a> pointed out at <a href="http://www.cubicleman.com/2009/05/18/360flex-indy-the-great-mashup/">his 360|Indy session</a> that no true web mashup is complete without using twitter so here is how to &#8220;complete&#8221; your own project.</p>
<p>With that in mind (and a fresh Flash Builder 4 Beta install) I figured what better way to learn a little about Spark than to create a Twitter Flex app?  Since I was intending on posting it as a how-to, I kept it simple and sweet.  Of course, it should be easy to take the code snippets and move them to your own mega-app.</p>
<p>I thought the first step was to learn the Twitter API, until I discovered there already existed projects out there that already took care of that.  Thanks to the open source library <a href="http://code.google.com/p/tweetr/">tweetr</a>, I just have to have a vague idea about twitter and let tweetr do the heavy lifting.  No worrying about value objects or dealing with XML, just a simple event listener.  So, with that I got to focus on Spark, which I&#8217;d never dealt with before.  </p>
<p>If you want to see the final output (and view source) I&#8217;ve posted it <a href="http://flex.enigmaticthought.com/TwitterSearch/TwitterSearch.html">here</a>.</p>
<p>The search code to work with Tweetr is very simple.  I simply add an eventListener for the COMPLETE event and shove the results into a bindable ArrayCollection.  I&#8217;ve bound the search function to the enter &#038; click events of a Spark TextInput and Button, but since they operate just like their MX components, it&#8217;s not really interesting to look at.</p>
<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p166code5'); return false;">View Code</a> ACTIONSCRIPT3</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p1665"><td class="code" id="p166code5"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #000000;">&#91;</span>Bindable<span style="color: #000000;">&#93;</span>
<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> _searchResults<span style="color: #000066; font-weight: bold;">:</span>ArrayCollection = <span style="color: #0033ff; font-weight: bold;">new</span> ArrayCollection<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> <span style="color: #004993;">search</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
	<span style="color: #6699cc; font-weight: bold;">var</span> tweetr<span style="color: #000066; font-weight: bold;">:</span>Tweetr = <span style="color: #0033ff; font-weight: bold;">new</span> Tweetr<span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
	tweetr<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span>TweetEvent<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">COMPLETE</span><span style="color: #000066; font-weight: bold;">,</span> searchResults<span style="color: #000066; font-weight: bold;">,</span> <span style="color: #0033ff; font-weight: bold;">false</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
	tweetr<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">search</span><span style="color: #000000;">&#40;</span>searchInput<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">text</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> searchResults<span style="color: #000000;">&#40;</span>event<span style="color: #000066; font-weight: bold;">:</span>TweetEvent<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
	_searchResults<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">source</span> = event<span style="color: #000066; font-weight: bold;">.</span>responseArray<span style="color: #000066; font-weight: bold;">;</span>
	_searchResults<span style="color: #000066; font-weight: bold;">.</span>refresh<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>
<p>Using MX, I&#8217;d normally bind that data to some sort of DataGrid or Repeater, but Spark has neither.  But, thanks to Flash Builder&#8217;s code hinting, I discovered the DataGroup component.  It&#8217;s somewhat like a repeater, but with more layout options thanks to the way layouts work in Spark.</p>
<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p166code6'); return false;">View Code</a> ACTIONSCRIPT3</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p1666"><td class="code" id="p166code6"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">&lt;</span>s<span style="color: #000066; font-weight: bold;">:</span>DataGroup
	dataProvider=<span style="color: #990000;">&quot;{_searchResults}&quot;</span>
	itemRenderer=<span style="color: #990000;">&quot;com.enigmaticthought.components.TweetBox&quot;</span> <span style="color: #004993;">width</span>=<span style="color: #990000;">&quot;100%&quot;</span><span style="color: #000066; font-weight: bold;">&gt;</span>
	<span style="color: #000066; font-weight: bold;">&lt;</span>s<span style="color: #000066; font-weight: bold;">:</span>layout<span style="color: #000066; font-weight: bold;">&gt;</span>
		<span style="color: #000066; font-weight: bold;">&lt;</span>s<span style="color: #000066; font-weight: bold;">:</span>VerticalLayout <span style="color: #000066; font-weight: bold;">/&gt;</span>
	<span style="color: #000066; font-weight: bold;">&lt;/</span>s<span style="color: #000066; font-weight: bold;">:</span>layout<span style="color: #000066; font-weight: bold;">&gt;</span>
<span style="color: #000066; font-weight: bold;">&lt;/</span>s<span style="color: #000066; font-weight: bold;">:</span>DataGroup<span style="color: #000066; font-weight: bold;">&gt;</span></pre></td></tr></table></div>
<p>So all I had left was to create a renderer for the data.  That&#8217;s where TweetBox came in to play.  Its base class is a Spark ItemRenderer, which was a little tricky to deal with because I had to define states for it, even though I didn&#8217;t use any states.  This is because the ItemRenderer uses states to handle things like hover and select to make the component customizable.</p>
<p>To fill in the TweetBox, I simply overrode the set data function and assigned all the values to the display components.  Sadly, SearchResultData was not built as Bindable, so I couldn&#8217;t just let data binding do the work.</p>
<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p166code7'); return false;">View Code</a> ACTIONSCRIPT3</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p1667"><td class="code" id="p166code7"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">import</span> com<span style="color: #000066; font-weight: bold;">.</span>swfjunkie<span style="color: #000066; font-weight: bold;">.</span>tweetr<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">data</span><span style="color: #000066; font-weight: bold;">.</span>objects<span style="color: #000066; font-weight: bold;">.</span>SearchResultData<span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> _tweet<span style="color: #000066; font-weight: bold;">:</span>SearchResultData<span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
override <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> <span style="color: #0033ff; font-weight: bold;">set</span> <span style="color: #004993;">data</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">value</span><span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=object%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:object.html"><span style="color: #004993;">Object</span></a><span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">:</span> <span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
	<span style="color: #0033ff; font-weight: bold;">super</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">data</span> = <span style="color: #004993;">value</span><span style="color: #000066; font-weight: bold;">;</span>
	_tweet = <span style="color: #004993;">value</span> <span style="color: #0033ff; font-weight: bold;">as</span> SearchResultData<span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>_tweet<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
		tweetImage<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">source</span> = _tweet<span style="color: #000066; font-weight: bold;">.</span>userProfileImage<span style="color: #000066; font-weight: bold;">;</span>
		tweetText<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">text</span> = _tweet<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">text</span><span style="color: #000066; font-weight: bold;">;</span>
		tweetAuthor<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">text</span> = _tweet<span style="color: #000066; font-weight: bold;">.</span>user<span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>
<p>One other thing to note is that while I could use Spark components for the text data, the image is still an MX component.  There isn&#8217;t any plan for a Spark Image in Flex 4, but this is a good example of how mixing Spark &#038; MX works.</p>
<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p166code8'); return false;">View Code</a> ACTIONSCRIPT3</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p1668"><td class="code" id="p166code8"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">&lt;</span>s<span style="color: #000066; font-weight: bold;">:</span>ItemRenderer
	xmlns<span style="color: #000066; font-weight: bold;">:</span>fx=<span style="color: #990000;">&quot;http://ns.adobe.com/mxml/2009&quot;</span>
	xmlns<span style="color: #000066; font-weight: bold;">:</span>s=<span style="color: #990000;">&quot;library://ns.adobe.com/flex/spark&quot;</span>
	xmlns<span style="color: #000066; font-weight: bold;">:</span>mx=<span style="color: #990000;">&quot;library://ns.adobe.com/flex/halo&quot;</span>
	<span style="color: #004993;">width</span>=<span style="color: #990000;">&quot;100%&quot;</span><span style="color: #000066; font-weight: bold;">&gt;</span>
	<span style="color: #000066; font-weight: bold;">&lt;</span>s<span style="color: #000066; font-weight: bold;">:</span>layout<span style="color: #000066; font-weight: bold;">&gt;</span>
		<span style="color: #000066; font-weight: bold;">&lt;</span>s<span style="color: #000066; font-weight: bold;">:</span>HorizontalLayout<span style="color: #000066; font-weight: bold;">/&gt;</span>
	<span style="color: #000066; font-weight: bold;">&lt;/</span>s<span style="color: #000066; font-weight: bold;">:</span>layout<span style="color: #000066; font-weight: bold;">&gt;</span>
<span style="color: #000066; font-weight: bold;">--------------------------------------------</span>
<span style="color: #000066; font-weight: bold;">...</span>State <span style="color: #000066; font-weight: bold;">&amp;</span> Code Removed<span style="color: #000066; font-weight: bold;">...</span>
<span style="color: #000066; font-weight: bold;">--------------------------------------------</span>
	<span style="color: #000066; font-weight: bold;">&lt;</span>mx<span style="color: #000066; font-weight: bold;">:</span>Image id=<span style="color: #990000;">&quot;tweetImage&quot;</span> <span style="color: #004993;">height</span>=<span style="color: #990000;">&quot;48&quot;</span> <span style="color: #004993;">width</span>=<span style="color: #990000;">&quot;48&quot;</span> scaleContent=<span style="color: #990000;">&quot;true&quot;</span> <span style="color: #000066; font-weight: bold;">/&gt;</span>
	<span style="color: #000066; font-weight: bold;">&lt;</span>s<span style="color: #000066; font-weight: bold;">:</span>VGroup id=<span style="color: #990000;">&quot;textGroup&quot;</span> <span style="color: #004993;">width</span>=<span style="color: #990000;">&quot;100%&quot;</span><span style="color: #000066; font-weight: bold;">&gt;</span>
		<span style="color: #000066; font-weight: bold;">&lt;</span>s<span style="color: #000066; font-weight: bold;">:</span>SimpleText id=<span style="color: #990000;">&quot;tweetText&quot;</span> <span style="color: #004993;">width</span>=<span style="color: #990000;">&quot;100%&quot;</span> explicitMaxWidth=<span style="color: #990000;">&quot;{textGroup.width}&quot;</span> <span style="color: #000066; font-weight: bold;">/&gt;</span>
		<span style="color: #000066; font-weight: bold;">&lt;</span>s<span style="color: #000066; font-weight: bold;">:</span>SimpleText id=<span style="color: #990000;">&quot;tweetAuthor&quot;</span> <span style="color: #004993;">width</span>=<span style="color: #990000;">&quot;100%&quot;</span> textAlign=<span style="color: #990000;">&quot;right&quot;</span><span style="color: #000066; font-weight: bold;">/&gt;</span>
	<span style="color: #000066; font-weight: bold;">&lt;/</span>s<span style="color: #000066; font-weight: bold;">:</span>VGroup<span style="color: #000066; font-weight: bold;">&gt;</span>
<span style="color: #000066; font-weight: bold;">&lt;/</span>s<span style="color: #000066; font-weight: bold;">:</span>ItemRenderer<span style="color: #000066; font-weight: bold;">&gt;</span></pre></td></tr></table></div>
<p>And now I have a really simple twitter search application.  So, what are some of the other uses for Twitter, other than e-stalking people?  You could build a new alert system that watches <a href="http://apiwiki.twitter.com/Twitter-Search-API-Method%3A-trends">Twitter Trends</a> to see what&#8217;s in the news, or use the search to keep an eye on a <a href="http://search.twitter.com/search?q=Blizzard">company</a>, <a href="http://search.twitter.com/search?q=Titans">team</a>, <a href="http://search.twitter.com/search?q=Avatar">movie</a>, <a href="http://search.twitter.com/search?q=ASOIAF">TV Show</a>, or <a href="http://search.twitter.com/search?q=ADBE">stock</a>. Of course, may you just want to <a href="http://search.twitter.com/search?q=%23songsincode">amuse yourself</a>.<br />
<h3 class='related_post_title'>Related Posts (generated):</h3>
<ul class='related_post'>
<li><a href='http://enigmaticthought.com/2009/09/flash-builders-new-menu/' title='Altering Flash Builder&#8217;s new menu'>Altering Flash Builder&#8217;s new menu</a></li>
<li><a href='http://enigmaticthought.com/2009/08/flex-open-iteration-meeting-2/' title='Flex OIM review'>Flex OIM review</a></li>
<li><a href='http://enigmaticthought.com/2009/08/flex-open-iteration-meeting/' title='Flex Open Iteration Meeting'>Flex Open Iteration Meeting</a></li>
<li><a href='http://enigmaticthought.com/2010/06/flash-debug-crashing-in-firefox/' title='Flash Debug crashing in Firefox?'>Flash Debug crashing in Firefox?</a></li>
<li><a href='http://enigmaticthought.com/2009/12/why-algorithms-matter/' title='Why Algorithms Matter'>Why Algorithms Matter</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://blog.enigmaticthought.com/2009/08/how-to-flex-the-twitter-api/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
