Posts Tagged ‘programming’

Links April 5-11

Saturday, April 11th, 2009

Here are some links I bumped into and posted on Twitter in the past week. If you like the links I post, please consider becoming a “Fan” of “Super Web Guy” on Facebook!

Computing

Design

PR

Programming

SEO

Twitter

Websites

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • E-mail this story to a friend!
  • LinkedIn
  • MySpace
  • Reddit
  • Slashdot
  • StumbleUpon
  • FriendFeed
  • Turn this article into a PDF!
  • Ping.fm
  • Twitter

Links March 1-7

Saturday, March 7th, 2009

Here is my link collection from this week. (more…)

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • E-mail this story to a friend!
  • LinkedIn
  • MySpace
  • Reddit
  • Slashdot
  • StumbleUpon
  • FriendFeed
  • Turn this article into a PDF!
  • Ping.fm
  • Twitter

Timeline of stuff I am into

Wednesday, October 29th, 2008

I dream up, design, build, deploy and maintain web-based tools and web sites using these tools. So much fun in so little time.

Timeline of stuff I am into.

Quick Links: Social Networking | Web Programming | Web Design | Art

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • E-mail this story to a friend!
  • LinkedIn
  • MySpace
  • Reddit
  • Slashdot
  • StumbleUpon
  • FriendFeed
  • Turn this article into a PDF!
  • Ping.fm
  • Twitter

Object-Oriented Javascript and SproutCore

Friday, June 27th, 2008

Javascript is no stranger to objects. Numbers, Strings, – and not to mention – Arrays and Functions – are all objects. And it’s easy to build our own objects with Javascript.

Why care about working with objects?

Object oriented programming helps keep web page code neatly encapsulated and reusable. You can define a bunch of variables inside an object instead of the ambiguity of exposing variables open in the document and having naming conflicts. Plus encapsulation code makes it easier to read.

There are a couple different approaches to building Javascript objects. Which approach we use largely depends whether the object is one of a kind, or whether there should be multiple instance of a kind of object.

Singleton

First let’s look at the “one of a kind”, or “singleton”, type. It’s a generic Object-type object. It’s easy to make. Just set a variable equal to a set of braces. The show transcript has examples of what I’m talking about.
[sourcecode language='jscript']var myObject = {}; // short form of “ var myObject = new Object();”[/sourcecode]
An object can be made up of properties of any object type. So my dog object has a “ageInDogYears” that’s a number, and a “name” that’s a string, and a “favoriteToys” that’s an array. We add these properties using the property name-colon-value, comma, property name-colon-value format.
[sourcecode language='jscript']var myObject = {width:”1024px”, height:”768px”};[/sourcecode]
As I mentioned, properties can have any type and types not only include Strings and Numbers, but also Functions and Arrays. You can make an array quickly by putting two brackets together, just like you can make an object quickly by putting two curly braces together.
[sourcecode language='jscript']var myObject = {};
var myArray = [];[/sourcecode]
Javascipt Object Notation, or JSON, uses a combination of object and array notation to define data packets sent between a web browser and a server. Here we have an object which contains an array of objects. Each object in the array is like a record of data, or a row in a spreadsheet.
[sourcecode language='jscript']var myObject = { data:[{guid:”month-1”, month:”January”} , {guid:”month-2”, month:”February”}] };
alert(myObject.data[0].month); // “January”[/sourcecode]
Functions are also object types, and as such, can be defined within our objects. So our dog object can bark by adding a “bark” function.

This is how we would add a Function object type property to an object.
[sourcecode language='jscript']var myObject = {
dimensions:[“768px”, “1024px”],
alertDimensions: function () {
alert(“Height=”+dimensions[0]+”, Width=”+dimensions[1]);
}
};
myObject.alertDimensions();[/sourcecode]
Custom Objects

Now let’s look at custom object types. Conceptually, an example of a object type would be “Dog”, and an instance of a “Dog” would be “Rover” and “Spot”. “Rover” and “Spot” are instances of a Dog. Objects are instances of an object type. We use this approach when our objects are not “one-offs”, if we need multiple instances of a type of object,… or we’re dog lovers. We make an object from the object type, and then use it like any other object. To make an object type, just make a function, except inside the function use the “this” keyword before property names.
[sourcecode language='jscript']function Dog (n) {
this.name = n;
}[/sourcecode]
We can define functions for our object types. Remember how are dog learned to bark? Just use the “prototype” keyword after the name of the function, then the function name set equal to a function definition. And remember to use the “this” keyword before the property name.
[sourcecode language='jscript']Dog.prototype.bark = function () {
alert(“Roof! My name is ”+ this.name + “.”);
};[/sourcecode]
Instantiate the object type like any other using the “new” keyword.

var myDog = new Dog(“Spot”);

And to access object type functions, just use the usual method, separating the object name from the function name with a period, with parenthesis after the function name.
[sourcecode language='jscript']myDog.bark();[/sourcecode]
Rich Internet Applications (RIA)

Object oriented client side programming is in heavy use in web 2.0 sites. A page has become an opportunity for RIA’s, or rich internet applications. All different kinds of Javascript objects interact to handle page events, data requests, and user interface animation and rendering.

Various object oriented Javascript methodologies, libraries and frameworks have popped us such as JSON, AJAX, jQuery, Prototype, Scriptaculous, Spry, and SproutCore. These obscure the long standing browser implementation discrepancies that have given client-side programming a bad name. Javascript programmers can focus more on using the libraries and less on browser differences. The show transcript has links to these projects.

SproutCore

The past few weeks a new Javascript framework came to a lot of people’s attention. It’s called Sproutcore. At Apple’s World Wide Developer Conference they presented some of their handywork. Turns out the existing .Mac Web Gallery’s were written with it. Although I hear there have been a bunch of optimizations of it since then. I’m a fan of the .Mac Web Gallery, but it is a little slow. Fortunately they’ve sped some things up for the upcoming MobileMe package that’s going to replace .Mac. Anyway, Sproutcore is really something. A developer with Ruby installed can install the framework with a single line of code on the command line.

sudo gem install sproutcore

That’s it. Installed.

I’ve read a little bit about Ruby on Rails but I haven’t had a chance to use Ruby so I’m still figuring that out. Lucky for people like me, Sproutcore only uses Ruby for building the HTML, Javascript and CSS files for deployment. The server can be anything. Apache with PHP, IIS with .Net, RoR, whatever. Database, doesn’t matter. Ruby and Sproutcore are all about the developer experience, Sproutcore’s final deployment is server agnostic. It’s all just HTML and Javascript and CSS files. Of any server can serve those.

So I’ve been a Javascript guy for quite a while, and I’m all for well done Javascript code. Sproutcore takes it to a whole other level. We’re talking about a framework that makes you use common design patterns like model-view-controller (or MVC). It outputs a browser independent UI with it’s own cool buttons and list views.

In terms of the model layer, it keeps track of the data such that you can imagine being able to build an app that doesn’t need an internet connection. Sure you would at first, but the data is stored locally and the server refreshes it, but it’s not a dumb system that is just the controller. It’s the whole model-view-controller pattern, deployed. The model could be stored someday similar to what Google Gears does where it stores web data locally on your system if you let it.

I’m really fascinated by this project. Check out the people who made it at Sproutit.com. Sproutcore is at Sproutcore.com. Sproutit and Sproutcore were started in order to create a web based email system. Then I guess Apple came and started contributing. Soon it will be open source.

There is a google group at

http://groups.google.com/group/sproutcore

and a wiki where people are posting tutorials.

http://github.com/sproutit/sproutcore/wikis

There is a version control repository in case you feel like lending a hacking hand to the project. The repository also has the samples that you can download with the easy Subversion checkout command.

svn co http://sproutcore.googlecode.com/svn/trunk/samples

On a Mac the code goes into a directory where all the Ruby helper code is there to check out.

/Library/Ruby/Gems/1.8/gems/sproutcore-0.9.11

And you may actually need to get into the source code because there isn’t much in the way of documentation or examples yet. It’s very young, as documentation goes. And it seems people are hungry for tutorials. The Sproutcore folks are posting those as fast as they can. If this project plays it’s cards right, which it seems to have done so far, someone will make some money selling books. I’d buy one. I’m digging like crazy through the code trying to figure out how to make stuff work. And I’m not getting very far. That’s a big part of the reason for the late podcast. I’ve been studying it to say something worthwhile about it.

They’ve posted a “hello world” example and I can get that going – no problem – but I’m a little hung up on getting data back and forth from the server and binding some of the views to my data. Anyway, I’ll figure that out. When I do, you’ll know.

I’ve put a snippet of code taken the “hello world” example and put them in the show transcript at PodTurtle.com. Check it out. Or better, check out Sproutcore.com and help me figure this project out.

# sproutcore hello_world
# cd hello_world
# sc-server
# sc-gen model hello_world/my_model
# sc-gen controller hello_world/my_controller
[Edit english.lproj/body.rhtml]
# sc-build

The command “sproutcore” makes a folder with a bunch of files to start editing. You “cd” in that new project folder and run “sc-server” to fire up the server. At that point just point your browser to

http://localhost:4020/hello_world

and you’re looking at the browser output. Make a change to

english.lproj/body.rhtml

and refresh the browser. You’ll see the change right away. The Ruby commands inside of the RHTML file are only view helpers. They do the work of building the HTML for you.

Technically it’s using Ruby to compile the HTML, but in the end you run the command “sc-build” to covert the RHTML files into HTML files and upload all that output to any server. Again you only need Ruby for building; the final product is just straight HTML which can run on any web server.

Sproutcore is about to go open source, about to go version 1.0. The current version is 0.9.11. So far so good. I’ll be following this and adding it to my arsenal. Very exciting.

Conclusion

So to wrap it up, to make powerful and compatible client side apps somewhat quickly, it is good idea to follow these projects that have gained traction, and become familiar with object oriented Javascript programming. Client side scripting has rich potential for the web developer, so expect future entries along these lines.

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • E-mail this story to a friend!
  • LinkedIn
  • MySpace
  • Reddit
  • Slashdot
  • StumbleUpon
  • FriendFeed
  • Turn this article into a PDF!
  • Ping.fm
  • Twitter

Passing Data with AJAX, Sessions, Cookies, and GET and POST Requests

Sunday, June 15th, 2008

Demo
http://www.davidvanvickle.com/examples/ep8/index.htm

Download
http://www.davidvanvickle.com/examples/ep8/ep8.zip

Web pages can be highly dynamic. We can log in to a site and get relevant data like a bank balance or see our social network friends – information specific to us. That means our page requests to the server are identified as being ours and the server can compile something with our data in it.

The internet’s way of communicating (“hypertext transfer protocol” or HTTP) is stateless. That means, on it’s own, it doesn’t remember the visitor from one page to the next. It just sends anonymous requests to the server, and the server sends back that static page. Fortunately there are a few mechanisms for distinguishing one request from another, which allows us to make web pages more personalized.

One mechanism is the cookie. Cookies are strings of text stored by the browser. A cookie allows the browser to remember things between pages. A common use of cookies is with sessions. When you visit a web site, you are in a session with that web site. Cookies are sent from the site and contain data specific to that session, such as a session ID. The browser receives and stores the cookie, then sends it back to the web site every time a new page is requested. So now the server, on some level, knows who is asking for what. The server can respond differently depending on the cookie value sent from the browser.

Use Javascript to access cookies on the client-side.

alert(document.cookie);

Here is a Javascript object for reading, writing and deleting cookies.

http://www.davidvanvickle.com/examples/ep8/index.htm
http://www.davidvanvickle.com/examples/ep8/ep8.zip

CookieHandler Example

var C = new CookieHandler();
C.set(‘mykey’, ‘myvalue’);
alert(C.get(‘mykey’));
C.del(‘mykey’);

The problem with using cookies is that the user can always disable them, so you have to make sure your client side app doesn’t break when that happens. Either that or you tell users up front that javascript and cookies must be enabled to use your application.

From the server side perspective, when cookies are not available, the server has to come up with another way to track session id’s. Without cookies, the session ID must be embedded in a different part of the page request. There are a couple methods to request information from a server. One is called a GET and the other is called a POST.

GET

Using a GET request means that data is attached to the URL after the question mark. This looks like “page.php?session=mysessionid”. Multiple bits of data can get strung together after the question mark with ampersands like “page.php?key1=value1&key2=value2”.

Usually this data is read on the server side, but Javascript on the client side can parse these GET query strings as well.

The following Javascript code can read the data sent with the GET method.

http://www.davidvanvickle.com/examples/ep8/index.htm
http://www.davidvanvickle.com/examples/ep8/ep8.zip

QueryStringReader Example

var qsr = new QueryStringReader();
var key1 = qsr.get("key1");
var key2 = qsr.get("key2");
if (key1) alert(key1);
if (key2) alert(key2);

POST

Using a POST request puts data in another part of the request packet and is generally preferred when more than a few pieces of data need to go back to the server. When a user uses a search field, often a GET is used as there are only a couple fields of data to transfer. But when a larger order form is used, it will tend to POST since there is more data to pass. When files such as images need to be sent from the client to the server, the POST method is always used. Javascript does not have access to POST data, only to GET data.

AJAX

Requests aren’t restricted to when the user goes between pages. Javascript can send it’s own requests behind the scenes within a single page. This is how AJAX works. Javascript does a GET or POST request from inside the page and gets an XML response from the server. The Javascript receives the XML, parses it, and changes the page layout somehow, all without the page having to reload. That’s AJAX. There is a great AJAX quick start tutorial on Mozilla’s developer wiki. A link to it is available in the show transcript at Podturtle.com.

http://www.davidvanvickle.com/examples/ep8/index.htm
http://www.davidvanvickle.com/examples/ep8/ep8.zip

var httpRequest;
if (window.XMLHttpRequest) { // Mozilla, Safari, ...
httpRequest = new XMLHttpRequest();
} else if (window.ActiveXObject) { // IE
httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
}
httpRequest.onreadystatechange = function() { alert(httpRequest.responseText); };
httpRequest.open('GET', ‘data.xml’, true);
httpRequest.send('');

Sessions

The most reliable and easy way to deal with sessions is to use the built in session handling of modern server side scripting languages such as PHP and ASP. There will be an object dedicated to keeping track of the session for us. And we get to store whatever we want in that temporary session data. A server side programmer can trust the server to maintain that session for the user however it needs to, via cookies or via GET requests. It’s automatic.

PHP

session_start();
$_SESSION[‘mykey’] = ‘myvalue’;
echo $_SESSION[‘mykey’];

ASP

Session(“mykey”) = “myvalue”
Response.write(Session(“mykey”))

Similarly, methods for getting GET and POST data from requests are also handled very easily with server side scripting languages.

PHP

echo $_GET[‘mykey’]; // GET
echo $_POST[‘mykey’]; // POST

ASP

Response.write(request.querystring(“mykey”)) ‘ GET
Response.write(request.form(“mykey”)) ‘ POST

Whether you plan to program on the client side or the server side, learning how to pass data between pages is the first step to making your web apps interactive and personalized.

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • E-mail this story to a friend!
  • LinkedIn
  • MySpace
  • Reddit
  • Slashdot
  • StumbleUpon
  • FriendFeed
  • Turn this article into a PDF!
  • Ping.fm
  • Twitter

The FriendFeed API

Thursday, May 29th, 2008

First off, let’s define aggregation as taking a bunch of things and putting them all in one place.

Let’s try something.  I’m going to read off a bunch of web services.  Which ones are you a member of?

  • Digg
  • Google Reader
  • Google Talk
  • Gmail
  • Mixx
  • Reddit
  • del.icio.us
  • Furl
  • Ma.gnolia
  • StumbleUpon
  • Jaiku
  • Pownce
  • Twitter
  • Seesmic
  • Vimeo
  • YouTube
  • Flickr
  • Picasa
  • SmugMug
  • Zooomr
  • Tumblr
  • iLike
  • Last.fm
  • Pandora
  • Goodreads
  • LibraryThing
  • Amazon
  • Disqus
  • LinkedIn
  • Netflix Queue
  • Netvibes
  • SlideShare
  • Upcoming
  • Yelp
  • or any blog
  • or anything else putting off an RSS feed

Each of these can be pulled into one feed by FriendFeed.com .

How many of those services are you a member of?  What do you do when you want visitors to see all your posts in one place?  Think about using FriendFeed and harnessing it’s API .  We’ll use it to create a single central feed out of the chaos.

So how does FriendFeed work?  Each member loads up their various accounts from around the web – Google Reader shared articles, Twitter posts, blog entries, YouTube videos, Flickr photos, del.icio.us bookmarks, all that stuff I listed earlier – and members can have discussions around each entry.  It’s a furious stream of information and comments coming from all directions. blogs, news, pictures, etc – all flying by, with comments.  It’s the daily web posting activity of thousands of people in one place.

Facebook already has a nice FriendFeed app so your friends can see your FriendFeed-aggregated posts.

Otherwise showing it on another web page (like a very cool business’ web page) means we’re off to the FriendFeed API.

First thing we need is a FriendFeed account.  They’re free.  Go to friendfeed.com and follow the instructions.

Then load all of your web memberships into FriendFeed so you’ll have stuff in your feed.

Then we need to download the API from http://code.google.com/p/friendfeed-api/ .  Documentation and forums are available at http://friendfeed.com/api/ .

Then, in order to authenticate with the API, we get our new account’s Remote Key from http://friendfeed.com/remotekey .

That’s all the official FriendFeed stuff we need.  Now on to our application.  I called it FriendFeedFormatter.

It is not affiliated with FriendFeed.  Use it all at your own risk. FriendFeed has stated that the API is young and could change at any time.  That disclaimer out of the way…

My code does all the work of taking our nickname and remote key and producing an HTML snippet from our FriendFeed feed that we can then have included in a PHP page.  Download example.zip from the show notes.  It’s got all the code that I’ve written using the API.

http://www.davidvanvickle.com/friendfeed/example.zip

In the show notes is a link to see sample output.  That will give you an idea of what to expect, and you can begin to think of how to embellish it.

http://www.davidvanvickle.com/friendfeed/build.php

There are only two files in example.zip.  One is my FriendFeedFormatter PHP class.  The other is build.php, which is the file you’ll customize and run.

Now find the FriendFeed API you downloaded.  In order for my custom class to work, you’ll need to include the friendfeed.php file from the API in the same folder.

So when you’re done there will be 3 files uploaded to a folder on your PHP enabled web server.  friendfeed.php from the API, plus FriendFeedFormatter.php and build.php.

Before we try to run anything we need to change a couple things inside of build.php.  So open that file in a text editor.

Enter your FriendFeed nickname where it says “MY_NICKNAME”.  Then enter your remote key where it says “MY_REMOTEKEY”.  That’s it.  There are more things we could toggle but let’s start here.

So save and close build.php and upload the 3 files to the web server.  Now you should be able to hit your build.php with a web browser and see what happens.

If all is well then you’ll see the HTML table that the FriendFeedFormatter produced using the API.  Now look in the webserver folder where you uploaded the 3 files.  You should now see a 4th file.  That file is what you’ll be including in your normal web pages.  It is JUST the HTML table displaying your feed.  Just a little piece of a web page, not the whole thing.  Perfect for inserting into a page that already exists.

Why don’t I just write a Javascript snippet like everyone else?  Well because I don’t want to slow my web pages with a call to a remote site.  The FriendFeed servers are pretty fast, but I still don’t want that dependancy if I can help it.  As long as I can run that build.php file on a regular basis, I will have a new enough include file for my site.  And it will load right away.

So how do I run build.php on a regular basis?  Good question.  If you want to automate that, I have half an answer.  If you’re familiar with running cron jobs, or if you want to learn how, cron would be a good way.  Cron allows your server to perform a task – such as calling build.php – according to a schedule, like every few minutes, instead of waiting for a web user to come along and trigger it.  Cron is usually running already.  You don’t have to install it.  Just add a crontab to call lynx, wget, or on older servers, the php cgi executable and pass it build.php and how often it should run.

Truth be told, I don’t have much experience with cron, but it seems to be where people go for scheduling scripts.  I included some examples in the show notes that I scraped from the web to get you started.  I’m sorry that I don’t have more tested examples.

Run every Monday morning at 4:41AM.  (minute hour day month weekday command)

41 04 * * 1 /path/to/lynx http://www.mydomain.com/path/to/your/cron.php
or
41 04 * * 1 /path/to/wget http://www.mydomain.com/path/to/your/cron.php
or
41 04 * * 1 /path/to/php /path/to/your/cron.php

Samples from:
http://www.hackernotcracker.com/2007-04/run-php-scripts-with-crond-and-crontab.html
http://www.modwest.com/help/kb.phtml?cat=2&qid=105

More at
http://www.webmasterworld.com/forum40/1258.htm

Otherwise just keep build.php bookmarked and hit it every time you do something that updates your feed.

Fortunately for server performance, but unfortunate for us, is that FriendFeed is on it’s own schedule.  Just because I update my blog, doesn’t mean FriendFeed is immediately aware of that post.  It may take a few minutes for it to show up in the FriendFeed feed.  (That’s a great reason to figure out how to make the cron way work)

The code to pull in the include file in your PHP page is the same code in build.php that displays the output in the browser.  The FriendFeedFormatter->get_output() method.

Once you see the output, I’m sure you’ll want to customize it more.  Go for it.

I built in a few enhancements.

One enhancement is date formatting.  The date may come to you a couple different ways.  One is in ISO8601 format.  That looks like this. “2008-05-21T15:02:22Z”

Another format would be as a UNIX timestamp, or a long number that represents the number of seconds since January 1, 1970.

You should be able to look in the date column, see which kind of date you have, and change the value of the date format variable in build.php.

$ff_date_format = ‘ ISO8601’; // or ‘unix’

On one server I got one format, on another I got the other format.  I think that’s based on the PHP version I had running but I’m not sure.  Anyway, that’s why my code has two ways to format dates.Another customization is the title of the table.  I recommend making that a link to your Friendfeed page.  The current set up has a link to your FF page, plus a link to  subscribe to your feed.

The formatter has a couple features users may appreciate.  One is inline Youtube videos.  First the user sees a JPG image of the video, but when it is clicked, it turns into a playable video.

Another feature is link detection, which is programmed to happen with Twitter entries.  The static URL’s become clickable.

The last feature is, when Flickr is detected, it lists the thumbnails of the images.

These are things I expected to happen from the API, but they weren’t there, so I added them to my formatter.

Open the formatter and notice how I can treat each service’s entries differently.  I can detect Twitter, for instance, and scan the entry for URL’s to convert into links.  Or I can detect Youtube and make the inline play feature happen.

The important thing is just to hack away to get going using FriendFeed to aggregate your public web activity.

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • E-mail this story to a friend!
  • LinkedIn
  • MySpace
  • Reddit
  • Slashdot
  • StumbleUpon
  • FriendFeed
  • Turn this article into a PDF!
  • Ping.fm
  • Twitter

PHP Programming with the FriendFeed API

Monday, May 26th, 2008

Problem
I want to display all of my published web activity (from podcast, YouTube, De.licio.us, blog, etc) in one place.

Solution
Use FriendFeed to pull in services, then use FriendField API to build a FriendFeed feed formatter which will build an HTML snippet file to be included on web pages. The snippet will display time-stamped activity from various web services. Display is customized by service. I created a PHP class named FriendFeedFormatter. It takes my FriendFeed nickname and remote key, and outputs the file to be included on web pages.

My FriendFeedFormatter PHP code:

Sample output | Download code ( updated May 28, 2008 )

Me on FriendFeed

My podcast episode explaining how to install and use this.

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • E-mail this story to a friend!
  • LinkedIn
  • MySpace
  • Reddit
  • Slashdot
  • StumbleUpon
  • FriendFeed
  • Turn this article into a PDF!
  • Ping.fm
  • Twitter