April 27, 2010

04/27, @10:00am

The Limonade website names the aforementioned Camping and Sinatra frameworks as sources of inspiration. It’s perhaps the most compact of the solutions described here in terms of the amount of code required to build something useful, with powerful demos such as the Wikir wiki (written in under 200 lines of code). But perhaps the most powerful evidence of Limonade’s minimalistic approach is the example included with the project README, which includes all of the code necessary to power a one-page website:

require_once 'lib/limonade.php';
dispatch('/', 'hello');
    function hello()
    {
        return 'Hello world!';
    }
run();

Minimalistic indeed!

— Top 10 Lightweight Frameworks for PHP Development by Jason Gilmore

April 13, 2010

04/13, @9:58am

ALL code is ugly. Yours, mine, everyone’s. Code Is Ugly. Just face it. When someone says “beautiful code”, hear “beautiful rotten entrails”. The only beautiful code, the most clean code, is no code at all. If it’s there, it’s ugly. The unfortunate fact of software development is that, in order to create beautiful software, we must use ugly code. — gist: 357981 - A better coding convention for lists and object literals in JavaScript (via mwunsch)

March 22, 2010

03/22, @10:00am:

[by Mark Headd]

A couple of months ago, I did a quick write up on a new cloud telephony company named Cloudvox. In the interim, I’ve been doing some playing around with their HTTP/JSON API for creating telephone applications, and I’ve been blown away by how simple and powerful a tool it is for building sophisticated cloud telephony applications.

This post will provide a quick overview of how the Cloudvox JSON API can be paired with PHP and the delightfully awesome Limonade Framework. If you’re not a PHP developer don’t despair - this example can easily be ported to other languages like Ruby (using Sinatra) or C# (using Kayak).

Cloudvox API Helper Classes

When writing apps for the Cloudvox JSON API, there are two things that we need to manage - the JSON that we will send to Cloudvox (using plain old HTTP) and the response Cloudvox sends back to our app (this will include any user input collected from the caller, and also things like a unique identifier for the call, caller ID and other information about the call).

To make managing both sides of our exchange with Cloudvox easier, I’ve created a set of PHP classes that can be used with any standard PHP IDE to make writing Cloudvox JSON and parsing Cloudvox responses simple and easy. You can download this class library from GitHub.

Using these classes is pretty straightforward:

<?php
  include('cloudvox-json-http-classes.php');
  $speak = new Speak("Hello world!");
  $hangup = new Hangup();
  Output::renderJSON($speak, $hangup);
?>

Will render:

[{"name":"Speak","phrase":"Hello world!"},{"name":"Hangup"}]

Required properties are included in the constructor for each class - in most IDE’s this means that you can simply use Shift + Control + Space Bar when you create a new instance of the object to see what properties are required.

Optional properties on all classes are handled by using the PHP __set() method in the base class. This effectively let’s you overload the object and set properties which are not declared in the class definition. So for example, if we wanted to collect input from the caller (e.g., their zip code) we would use the GetDigits class, and overload it to add a URL to post the results to:

<?php
  // First argument passed into the constructor sets # of digits to collect.
  // Second argument sets the timeout.
  $getDigits = new GetDigits(1,5);
  // Now we overload the object to add a URL to post the results to.
  $getDigits->url = “http://somehost.com/myscript.php”;
?>

The problem with overloading in PHP is that you don’t get the benefit of having your IDE display overload options (it can’t because the properties that we wish to set are not declared in the class definition). There also isn’t any way to control what overloaded properties get set, so its possible to add things the Cloudvox won’t understand.

I’m not sure there is any way around this given the way that PHP has implemented overloading. I do plan to work on a set of Cloudvox classes using another language that handles overloading a bit better, like C#, but for now you should only overload to set the url and method properites for classes that can use them (see the Cloudvox docs for more details).

Sipping on some Limonade

If you know of the excellent Ruby Framework Sinatra but you want to code your project in PHP, fear not - check out the equally excellent PHP framework called Limonade. It’s the functional equivalent of Sinatra for PHP.

Using Limonade with our set of Cloudvox JSON classes makes building cloud telephony applications very simple. The biggest benefit is that you don’t have to split up the different steps in your application (i.e., collecting input, validating input, re prompting, etc.) into different PHP files (which can be kind of a pain) - all of your steps can be contained within a single file.

Limonade lets you set a URL “route” that Cloudvox can send HTTP requests to with results, and to get rendered JSON for another application step. For example:

<?php
  dispatch_get('/start', 'cloudvox_start');
  function cloudvox_start() {
    $speak = new Speak("Hello world!");
    $hangup = new Hangup();
    Output::renderJSON($speak, $hangup);
  }
?>

This “Hello World” example - defined in a script named sample.php - could be accessed by hitting http://somehost.com/index.php?/start. To make things easier, we’ll use a little Apache magic to allow URL rewriting:

This will let us access the above URL by hitting http://somehost.com/index.php/start.

A simple demo app using the Cloudvox JSON helper classes and the Limonade Framework can be seen below. You can use this sample application with a new Cloudvox account to get started building cloud telephony applications.

This simple app demonstrates how powerful the Cloudvox JSON API is for creating cloud telephony apps. When coupled with an elegant framework like Limonade, sophisticated, cloud-based telephony applications are readily available to any developer that wants to build one.

The helper classes for the Cloudvox API are obviously a work in progress, so if anyone reading this has comments or suggestions feel free to let me know - mheadd [at] voiceingov.org.

March 16, 2010

Two new websites built with Limonade:

March 15, 2010

03/15, @4:06pm:

Limonade now has its own domain name ;-)

March 10, 2010

03/10, @9:57am:

This tutorial will teach you how to use content_for().

As an example, let’s use this default layout:

<body>
  <div id="header">
    <h1>Our big bold header</h1>
  </div>
  <div id="content">
    <div id="side">
      <?php if (isset($side)) echo $side; ?>
    </div>
    <div id="main">
      <?= $content; ?>
    </div>
  </div>
  </body>

See that we have not only our usual $content variable to render the content, but also a second variable called $side, which will be responsible to render our sidebar.

Next, create a controller function to handle the home action of our app:

# GET /
dispatch('/', 'home');
function home() {
  return render('home.html.php');
}

And, the home.html.php view file:

<!-- begining 'side' block -->
<?php content_for('side'); ?>
<ul>
  <li>Item 1</li>
  <li>Item 2</li>
</ul>
<?php end_content_for(); ?>
<!-- ending 'side' block -->

<h2>Home Action</h2>
<p>Main content…</p>

Finally, refresh the page to see that our sidebar now receives the content of our 'side' block.

To improve our code, let’s use the partial function, which will render view file at that point within the current rendered view.

Change the file home.html.php to:

<!-- begining 'side' block -->
<?php content_for('side'); ?>
  <?= partial('_sidebar.html.php'); ?>
<?php end_content_for(); ?>
<!-- ending 'side' block -->

<h2>Home Action</h2>
<p>Main content…</p>

And create a file called _sidebar.html.php in the view folder. Note that the underscore is optional.

<ul>
  <li>Item 1</li>
  <li>Item 2</li>
</ul>

Now you can break your views into modular files. It’s useful to insert specific CSS or JS and to make some AJAX stuff.

March 8, 2010

Want to share your limonade experience, your tips, a tutorial ? Now you can submit them directly on this tumblr.

January 23, 2010

01/23, @8:07pm:

01/23, @5:12pm:

Since thursday, the Limonade github fork queue seems to be broken (or something’s wrong with GitHub). But I wanted to apply Colin’s last commit before working on the project today.

So, I followed this article and finally, it was very easy.

# Setup repo as a remote branch
git remote add -f cbrumelle git://github.com/cbrumelle/limonade.git

# Create the local copy
git checkout -b cbrumelle/0.5

# Get their changes into my personal working branch:
git checkout 0.5
git cherry-pick 6f556e7 # <hash of user's specific changes that they requested you to pull>

Then I clean up my project by removing Colin’s branches (maybe there’s a nicer way to do that):

# First I list my branches
git branch -a

# this is the output:
  0.4
* 0.5
  cbrumelle/0.5
  master
  remotes/cbrumelle/0.4
  remotes/cbrumelle/0.5
  remotes/cbrumelle/master
  remotes/origin/0.4
  remotes/origin/0.5
  remotes/origin/master

# Then I delete the local copy
git branch -d cbrumelle/0.5

# And the remote ones
git branch -r -d cbrumelle/0.4
git branch -r -d cbrumelle/0.5
git branch -r -d cbrumelle/master

# finally remove remote repository reference
git remote rm cbrumelle

November 30, 2009

11/30, @10:05am:

Changes:

I have updated Wikir and the blog example. Please update your code!