Medusa Twitter API – Part I

Medusa Twitter API – Part I

This is a quick and dirty example on how to use the Twitter API found in Medusa 1.0. Keep in mind that because of Flash Player restrictions, we are not able to authenticate a user name and password; if you were to use the api within an AIR application, you would be able to make use of all calls.

This example also shows how to implement various controls found in the library as well such as the scrollbar control, VBox, HBox and others.

package
{
	import com.medusa.controls.Image;
	import com.medusa.controls.ScrollBar;
	import com.medusa.controls.scrollbar.styles.ScrollbarStyle;
	import com.medusa.core.display.layout.HBox;
	import com.medusa.core.display.layout.VBox;
	import com.medusa.core.extension.SpriteExtension;
	import com.medusa.plugin.Twitter;
	import com.medusa.plugin.events.TwitterEvent;
	import com.medusa.plugin.twitter.Status;
	import com.medusa.tween.easing.Back;

	import flash.display.Bitmap;
	import flash.text.AntiAliasType;
	import flash.text.TextField;
	import flash.text.TextFormat;

	[SWF(width='550', height='300', backgroundColor='0x000000')]
	public class MedusaSandbox extends SpriteExtension
	{
		[Embed(source="C:/WINDOWS/Fonts/ARIAL.TTF", fontFamily="Arial")]
		private var _arial:String;

		[Embed(source="background.png")]
		private var BackgroundImage:Class;

		private var twitter:Twitter;
		private var vbox:VBox;
		private var sbStyle:ScrollbarStyle;
		private var scrollBar:ScrollBar;

		public function MedusaSandbox():void
		{
			var bg:Bitmap = new BackgroundImage();
			addChild(bg);

			twitter = new Twitter();
			twitter.addEventListener(TwitterEvent.RESULT, handleResult);
			twitter.getPublicTimeline();
		}

		private function handleResult(e:TwitterEvent):void
		{
			vbox = new VBox();
			vbox.gap = 5;
			addChild(vbox)

			var statuses:Array = e.details.result as Array;
			for(var a:Number = 0; a<statuses.length; a++){
				var status:Status = statuses[a] as Status;
				var hbox:HBox = createUserItem(status.user.profile_image_url, "<u><b>"+status.user.name+"</b></u>" + " : " + status.text);
				vbox.addChild(hbox);
			}

			sbStyle = new ScrollbarStyle();
			sbStyle.overContent = false;

			scrollBar = new ScrollBar(vbox, 300, sbStyle);
			addChild(scrollBar);
		}

		private function createUserItem(imagePath:String, status:String):HBox
		{
			var hbox:HBox = new HBox();
			hbox.gap = 5;

			var image:Image = new Image();
			image.width = 50;
			image.height = 50;
			image.source = imagePath;
			hbox.addChild(image);

			var format:TextFormat = new TextFormat("Arial", 13, 0x000000);
			format.letterSpacing = 1.5;

			var label:TextField = new TextField();
			label.width = 485;
			label.height = 50;
			label.multiline = true;
			label.wordWrap = true;
			label.mouseEnabled = false;
			label.defaultTextFormat = format;
			label.embedFonts = true;
			label.antiAliasType = AntiAliasType.ADVANCED;
			label.htmlText = status;
			hbox.addChild(label);

			return hbox;
		}

	}
}

If you were to need to make authenticated calls, you would add this line when instantiating the twitter class:

twitter.storeCredentials('username', 'password');

Popularity: 29% [?]

About the Author

I have been in the Flash/Flex industry for approximately 8 years now. Although for the last year I have been mainly creating websites, I’m now more into social media applications (Facebook, Twitter), both online and offline (Adobe AIR), website and RIA framework development on which sites and/or applications are built which includes features like, but not limited to, deep linking integration and high levels of customizability through XML (CMS ready) and backend communication via web services and/or AMF.