Category Archives: Ruby

rake assets:precompile – Resolve the undefined method `directory?’ for nil:NilClass error message

This is my first post regarding this new feature of rails 3.1 and later, the assets pre-compiler.

To begin with, I believe that assets precompiler is an awesome feature of Rails no matter how quirky it looks when starting using it. Most likely, you are already convinced for that if you ‘re reading this post but I feel like I want to share with you my love to this feature.

BUT if you ever run into this error:

rake assets:precompile – Resolve the undefined method `directory?’ for nil:NilClass error message

locally or on production you will probably spend a lot of time looking around what is wrong. The –trace param will not help much as it doesn’t reply to the question: “What exactly is nil??”.

Well, I dubugged this situation by removing  all the assets from my /app/assets/ folder and adding the folder by folder while re-running the rake assets:precompile task after every addition. I was getting this error continusly till I tracked down to the specific folder whose files where causing the issue.

Long story short

That folder had aliased files (symlinks). Aliases and symlinks are the nil objects that the assets precompile task cannot find and aborts the operation. So, within your /app/assets file you should not have any aliased file and you are good to go.

 

I hope this helps.

 

How to uninstall all your gems

If you don’t use gemsets, this can occur  to  you. Although I do use gemsets almost always I had a ruby project running on the rvm 1.9.2 with messed up gems, many versions of each one etc. Anyway I decided I want to cleanup all the gems that are installed under this ruby.

The most practical way to delete all gems and all versions that I run into is the following

 

gem list | cut -d" " -f1 | xargs gem uninstall -aIx

 

It works! Just try it out..

Cheers.

Facebook application – deploying on the Cloud with Heroku.

Recently, Heroku announced a partnership with Facebook. Heroku is a famous [and my favorite] Platform as a Service for Ruby Apps and recently it launched a new stack that also supports java and python.
Facebook applications were always needed be scale-ready due to the network effect that they rely on. A successful Facebook application can literrally double it’s audience every day while it spreads over the huge user base of facebook.

This fact has developed a trend where facebook applications’ teams look for cloud solutions when deploying even small apps.

Facebook, listening to this trend, launched an automated way to host your app on cloud by integrating. Now when a facebook developer Creates a new app, in the first screen with all the basic options (app name, urls etc) there is a new option called “Cloud Services”

There you can select to host your facebook application on Heroku Cloud Service and see the following screens

Heroku Cloud Services - Integrating Facebook popup

Then you can select the stack (programming language) of your application. Heroku will spawn a process which will automatically build a new initial facebook application in the language specified. The app will have all the basics features, meaning authenticating from facebook, asking and using some basic permissions and rendering a welcome page featuring facebook’s api.

Heroku Cloud Services - Integrating Facebook - Choosing Stack / Language

In my case, I chose ruby and thus heroku started a new Sinatra Application, bundled with all the basic gems

Heroku - Facebook Application - Ruby - Sinatra

source :gemcutter
 
gem "sinatra"
gem "mogli"
gem "json"
gem "httparty"
gem "thin"

 

The magic part is that now heroku and facebook have complete the whole process for you and the exact application url that is spawned to start on heroku is added in the specified fields of the app configuration page.

Heroku Cloud Services - Integrating Facebook

To go on and customize your brand new application, just wait for the email from the heroku team that gives you a git repository. You clone, edit and

push heroku master

to deploy your app.

Share your localhost development environment online – The fastest and easiest way to ssh tunnel your local web server

Today, again, I needed to show the progress of an un-deployed application to the customer. It’s not rare at all to have a request to give an online link of your localhost environment. There are a numerous of solutions to this problem, especially if you have a static ip and you fully control your router and firewall ports you can do some magick and provide a public online url for your localhost.

But in most cases this can be tricky or un-doable. In my opinion the most effective way to show your localhost is to use ssh tunneling. This was a common practice back when facebook apps weren’t open to cross site connection and facebook app developers where used to open an ssh tunel to test and run their app in development environement.

Today, I run into the easies way to share your localhost environment via ssh tunel and with the help of a ruby gem called localtunel. The project is developed by Jeff Lindsay and it is supported by Twilio.

Let’s see now how it works.
First of all you need to have ruby installed in your local evnironment. To make sure you have ruby install, run

which ruby

and if you don’t get an answer try installing ruby via rvm.

After that you need to install this gem in the particular ruby version. So try

sudo gem install localtunnel

or try without sudo if you are using an rvm installed ruby.

The last thing to keep in mind is that you need to have an ssh key and provide the path of your ssh public key. In case you don’t have one, follow the instructions from github on how to generate an ssh key .

Supposing that your public key is under ~/.ssh/id_rsa.pub , the last step to take is to run

Savvas-Georgious-MacBook-Pro:.ssh savvasmsc$ localtunnel -k ~/.ssh/id_rsa.pub 80 This localtunnel service is brought to you by Twilio. Port 80 is now publicly accessible from http://3a5f.localtunnel.com ...

for the default port of 80 of a typical webserver or

$ localtunnel -k ~/.ssh/id_rsa.pub 3000

for a typical rails server etc.

When finished remember to kill the tunneling that exposes the local development environment to the whole internet with a Ctrl-C.

You can also have a look into the source code of this implementation here

Hello neo4j with ruby.

Neo4j is popular Graph Database system that allows you to manipulate data as nodes related with each other. Following a graph approach on data allows you to implement applications with special characteristics such as apps where users are following other users or pages. Actually, it’s almost impossible to simulate a graph data structure with conventional relational databases (SQL) or even with key-value storage systems. Graph databases is a single way to go, at least if you want to be able to scale for users more than your social circle.

Amongst the existing graph databases solutions, neo4j is gaining popularity. This can be because of it’s robust implementation, the complete documentation that the neo4j wiki page [http://wiki.neo4j.org]offers and the hooks for languages such as ruby, java, python.

In our case, the goal is to implement a simple user-following-user Ruby on Rails application that stores the following information in the graph database, neo4j.

First we need to install and use jruby. Relying to rvm we run start with

rvm install jruby

Then we need to generate a gemset for jruby and create a rails app, using this gemset.

rvm use jruby-1.6.3
 
rvm gemset create neotest
 
rvm use jruby-1.6.3@neotest

To hook the neo4j database with ruby, we should install and use (require) the neo4j gemset. So we run

gem install neo4j

Now Let’s run a simple test. Create a file and paste the following code.

require "rubygems"
require "neo4j"
 
#domain model
class Waypoint
  include Neo4j::NodeMixin
  #the persistent properties of this class
  property :name, :lon, :lat
  #friend relationships to other persons
  has_n :roads
  index :name
end
 
#populate the db
Neo4j::Transaction.run do
  #the waypoints
  NYC = Waypoint.new :name=>'New York', :lon=>-74.007124, :lat=>40.714550
  SF  = Waypoint.new :name=>'San Francisco', :lon=>-122.420139, :lat=>37.779600
  SEA = Waypoint.new :name=>'Seattle', :lon=>-122.329439, :lat=>47.603560
  #the roads
  NYC.roads << SF
  NYC.roads << SEA
  SEA.roads << SF
end
Neo4j::Transaction.run do
  #do a fulltext search over all Waypoints
  Waypoint.find(:name => 'New York').each { |x|
    puts "Found Waypoint: #{x.name}"
  }
end

Now save the file as neotest.rb

Go to terminal and run the file
$ jruby neotest.rb.

So now we have all the basic setup to start playing with neo4j graph database and ruby. As I just started messing with neo4j and graph database structure, I promise that there will be many following posts about these topics.


	

Heroku: How to sync your production database with your development environment.

Apart from heroku db:pull (uses taps and imports from psql to any local sql database) there is a faster way to download and import the production data to your local server.

1. Generate a dump file of the production database using

heroku pgbackups:capture

This will actually run the pg_dump script on the production server
2. List the backups

heroku pagbackups

3. Get the download-able url of the latest backup listed

heroku pgbackups {lates_backup_name}:url

4. Download the file using the returned url.

Postgresql can use the files comming from the pg_dump with the usage of pg_restore.

Now try this

pg_restore --verbose --clean --no-acl --no-owner -h 127.0.0.1 -U savvasmsc -d sweetlifer_development ~/Downloads/b019.dump