Archive

Tag Archives: ruby

I had been really lazy to write this blog to finish off the thor application example. So I’ll just go right to the application, it’s nothing complicated. This application intends not to tweet actually in twitter but to demonstrate how thor tasks work.

So where I left from last post add a new folder bin in your application root and create a file tweet. This is going to be our executable file which will allow us to do something like,


tweet post hello

and will print You tweeted: hello.

Put the following in your bin/tweet file,


#!/usr/bin/ruby

require "tweet"

Tweet.start # this registers the thor class as task

Now install build and install the gem by executing ‘rake install’ command in application root. It will install the executable as well. So that’s it, the code can be downloaded from https://github.com/mftaher/Tweet-Thor. You can use it as a basic structure to create your own executable thor tasks.

Note: Before you install or build the gem always commit your git changes.

Ruby is a a wonderful language, you can never stop praising what it lets you do and help you create great applications. To eliminate the need to copy my initial application structure every time I decided to write a scaffolding application in ruby that will give me an initial structure with necessary files in it just like rails generator (I use Sinatra micro web-framework for ruby web development).

I had been researching many options out there to accomplish this and ruby gives you not only wide range of options but also quickest turn around time for a command line application. To name a few the ruby core OptionParser is a powerful class to begin with, there is also Trollop, of course rake and there’s Thor that replaces rake, sake and rubigen.

A great advantage with Thor is, it documents your command line tool as you develop. Thus makes it easy for the developers create several tasks with plenty options and documenting at the same time. So I thought this reason is good enough to build my application using Thor then the others.

Instead of doing a hello world application, I’m going to demonstrate a simple tweeting application using thor. First you’ll need to have a structure of your application you can easily get it by doing:

bundle gem tweet

You will get the default app structure provided by bundle which can be used to publish your own gem. Then create and edit tweet.thor in your project lib diretory:

class Tweet > Thor
  desc "post MESSAGE", "Tweet message in your command line"
  def post(message)
    puts "You tweeted: #{message}"
  end
end

You should be able to see the task list typing `thor -T`.

To make the command available in your terminal, we’ll cover that in next blog post when we will actually post a tweet.

I have always appreciated the things you can achieve with javascript. The power of javascript has been shown over the years and there’re very few web sites which doesn’t use Javascript today. Although javascript is powerful and great but the syntax and debugging had never been so easy. Writing a object oriented javascript required skill and discipline and lots of patience. And those who are programming javascript kudos because you are all doing great work!

Javascript is an object oriented but a prototypical language where harnessing the power of prototypal inheritance is a little cumbersome. The jQuery library did help us all by taking care of the DOM but it always lacked the structured programming like OOP, it simply isn’t designed to help you organize your code outside of vanilla JavaScript. Then there is Prototype JS framework which  lets you write OOP style dynamic web applications and Moo4q by Ryan Florence also gives you the ability to write OOP style on top of jQuery.

Then in 2009 came Jeremy Ashkenas released Coffeescript, a programming language that transcompiles to Javascript. Writing applications and managing your code can never been easier because of the simplicity used in Coffeescript.  “The language adds syntactic sugar inspired by Ruby, Python and Haskell to enhance JavaScript’s brevity and readability, as well as adding more sophisticated features like array comprehension and pattern matching. CoffeeScript compiles predictably to JavaScript and programs can be written with less code (typically 1/3 fewer lines) with no effect on runtime performance“- [wikipedia].

Speaking of fewer lines of code and simplicity here’s an example of simple Person class in coffeescript

class Person
  constructor: (name) -> 
  @name = name

Defining constructors (functions that get invoked upon instantiation) is simple, just using function named constructor. This is akin to using Ruby’s initialize or Python’s __init__. Which compiles to javascript as this

var Person;

Person = (function() {

  function Person(name) {
    this.name = name;
  }

  return Person;

})();

By prefixing argument’s with @, CoffeeScript will automatically set the arguments as instance properties in the constructor. You can try playing with Coffeescript here. Coffeescript is website a great resource for learning.

Installing Coffeescript:

You can download and install nodejs and then use npm to install coffeescript in your environment with the following command:

 npm install -g coffeescript

You can then type coffee and it will invoke the interactive coffee. Or you can start writing your script in a *.coffee file and compile to js with following command and require in your html file for testing.

 coffee -c somefile.coffee

Then it will generate a samename.js file to require in your html. You can also direclty use coffee files in your html, I’ll show an example in later post.

Switching to Ruby from PHP wasn’t as hard as deploying rails/sinatra app on existing Apache-PHP environment. It should’ve been fairly easy as I choose to do it with Phusion Passenger instead of using Proxy to the rails/sinatra app. But who knew things can get really ugly if I hadn’t tested locally.

Problem Ughhh @#$@%@:

Actually I deployed a rails/sinatra app in production first then was trying to configure in my localhost but then figured out when passenger module is loaded DirectoryIndex index.php isn’t working, meaning index.php of any web application wasn’t recognized by apache by default anymore as before you have to type it in the browser. Digging deeper found out mod_dir is not compatible with phusion passenger offering no fix yet. You might want to check with conflicting apache module with phusion passenger before even trying to install passenger in your existing apache-php environment.

Avoid Catastrophe:

The reason why I did not notice it in my production environment at first because of cache. Once I tried to deploy in my local machine things aren’t working as it was in production. I had to type in index.php to gain access. Checked AddHandler, AddType, DirectoryIndex index.php everything in place and nothing seems to work until I found the conflicting apache modules.

Solution:

There’s a workaround provided by the Passenger team i.e. using PassengerEnabled off. After Passenger module is loaded you can turn it off so that mod_dir can do it’s job setting correct DirectoryIndex, and then enable it where Phusion Passenger is required, preferable inside rails/sinatra app  <Directory> block. If you put it outside it may globally configure which will again make mod_dir not to work. Only when an agent is requesting for the rails/sinatra app it gets turned on for that Directory block and there’s no conflict anymore. A good example is provided at Phusion Passenger Guide. This setting can be done otherway around depending on the number of rails/sinatra and php application you have running in your environment.

Still waiting for something to go wrong just hoping it happens in my local machine ealier.

Local Environment:

Mac OS X Lion 10.7.2 , built in apache2, php5.3.6., rvm 1.10.0, ruby 1.9.2, passenger 3.0.11

Sproutcore is an MVC architecture javscript framework for client-side for developing desktop like rich web applications. It enables building blazingly fast, innovative user experiences on the web. To know more about sproutcore visit here.

Install sproutcore in your windows machine you can download the windows installer from the site. Or you can install using ruby 1.9.2-p180.

gem install sproutcore -pre

if you are working on Windows then you will probably run into this when you install SproutCore:

gem install sproutcore -pre
Fetching: rack-1.2.2.gem (100%)
Fetching: json_pure-1.4.6.gem (100%)
Fetching: extlib-0.9.15.gem (100%)
Fetching: abstract-1.0.0.gem (100%)
Fetching: erubis-2.6.6.gem (100%)
Fetching: thor-0.14.6.gem (100%)
Fetching: eventmachine-0.12.10.gem (100%)
ERROR: Error installing sproutcore:
The ‘eventmachine’ native gem requires installed build tools.

Please update your PATH to include build tools or download the DevKit
from ‘http://rubyinstaller.org/downloads’ and follow the instructions
at ‘http://github.com/oneclick/rubyinstaller/wiki/Development-Kit’

You can find out more about DevKit at https://github.com/oneclick/rubyinstaller/wiki/Development-Kit and you can download DevKit at http://rubyinstaller.org/downloads, which you probably picked up on from the error message above.

Note: the latest version of “eventmachine” is not compatible with DevKit. You have to get the pre-release version by using this command:

gem install eventmachine –pre

Once you successfully install eventmachine run the following command again to install sproutcore.

gem install sproutcore -pre

When you try and install SproutCore again you should see a successful install like this:

gem install sproutcore -pre
Fetching: daemons-1.1.2.gem (100%)
Fetching: thin-1.2.11-x86-mingw32.gem (100%)
Fetching: sproutcore-1.4.5.gem (100%)
Successfully installed daemons-1.1.2
Successfully installed thin-1.2.11-x86-mingw32
Successfully installed sproutcore-1.4.5
3 gems installed
Installing ri documentation for daemons-1.1.2…
Installing ri documentation for thin-1.2.11-x86-mingw32…
Installing ri documentation for sproutcore-1.4.5…
Installing RDoc documentation for daemons-1.1.2…
Installing RDoc documentation for thin-1.2.11-x86-mingw32…
Installing RDoc documentation for sproutcore-1.4.5…

once you get this you should be able to create your first application running the following in command prompt:

sc-init

You should have a project folder created with the required files. Now you cd into the project folder and run the following command to start the server:

sc-server

Sproutcore browser by default listens to port 4020. Once you see the server has started to listening you can browse from browser to http://localhost:4020

That’s it you should see the welcome to sproutcore message in your browser after launching your project application!

%d bloggers like this: