Edgar Gonzalez

Apr 12

MongoDB shell inserts Float when try to insert an Integer/Long

Because of numbers are doubles in Javascript, when yo do this:

> db.test.insert({name: ‘foo’, value: 1});

you get this:

> db.test.findOne({name:  ’foo’});
{‘name’: ‘foo’, ‘value’: 1.0}

To avoid this you can use NumberInt or NumberLong:

> db.test.insert({name: ‘foo’, value: NumberLong(1)});

Feb 08

RubyCheckOnSave Sublime Text 2 Plugin

More than one year ago, I created a very simple script that enable Sublime Text 2 to automatically check the syntax of ruby files when they are saved

Three months ago I finally found the time to convert that script into my first Sublime Text 2 plugin: the RubyCheckOnSave plugin.

Jan 25

RVM: How to remove all gems from default gemset

To remove all gems from a gemset you use the gemset empty command:

$ rvm gemset empty <my_gemset_name>

And to remove the gems from default gemset, you must supply an empty string as the gemset name:

$ rvm gemset empty ""

Jan 24

How to cURL and JSON prettyprint

cUrl is a simple and powerful tool to test RESTful APIs, but sometimes the output could be messy, specially when the response is a long JSON.

To improve the output readability, I’m using cUrl with jsontool (a node.js script).

After you install it:

$ npm install -g jsontool

Then pipe the cUrl output to jsontool

$ curl -is http://search.twitter.com/search.json\?q\=foo | json -i

Below an example of the output

Sample of cUrl with JSON prettyprint

Jan 07

How to remove a Git submodule and put the code into the main repo

You need to remove the submodule and re-add the files into the main repo.

Edit the .gitmodules and .git/config files and remove the lines that specify the submodule, then;

git rm --cached path/to/submodule # delete reference to submodule HEAD (no trailing slash)
rm -rf path/to/submodule/.git
git add path/to/submodule # add files instead of commit reference
git commit -m "remove submodule"

Dec 27

Permission denied (publickey) when deploying heroku code - Sometimes is the git remote …

Today I tried to deploy to heroku a new version of an application (that not has been deployed in several months):

git push heroku master

but I got the following error:

Permission denied (publickey).
fatal: The remote end hung up unexpectedly

The weird part is that I can deploy other 3 apps without problem, that means that my public SSH key is properly stored on Heroku.

If the Heroku api is experiencing downtime, you will get this error when you try to push. So, I checked https://status.heroku.com/ and everything seemed to be fine on Heroku’s end.

Then I checked my git remotes, and my problem was that the remote heroku repository was corrupted. I refreshed it as follows:

git remote -v

Then removed the heroku one that is wrong:

git remote rm heroku

Then added the new one

git remote add heroku git@heroku.com:myapp.git

Dec 20

How to sort in ruby an enumerable by an attribute that could be nil

I have an Enumarable of tokens, and I need to sort them descending by the last_access attribute, but for some objects this attribute could be nil. One OOP approach to do it, is defining the <=> method (Comparable) in the Token class:

class Token
  include Comparable

  define <=> other
    ...
  end
end

Then I’ll only need to call tokens.sort. But I’m not ready to change the behavior of the Token class in that way, instead of that I prefer to follow an approach that don’t have side effects. So, an easy hack is to use an immediate-if like this:

tokens.sort{|a,b| 
  (a.last_access && b.last_access) ? 
    a.last_access <=> b.last_access : 
    (a.last_access ? 1 : -1) 
}.reverse!

Dec 18

How to delete large amount of data of a MongoDB collection “quickly”

We have a db collection that is around 30 million documents, and I need to trim it down, to only keeping the documents created on the last month. 

One approach would be use the remove command with a condition on the created_at field (the collection already have an index on this field):

db.my_collection.remove({created_at: {$lte: new Date("11/01/2012")}});

But this approach will be very slow, instead of that, a better way to do it is rename the current collection (for instance to “old_collection”) using renameCollection. Then performing a query-and-insert from the “old_collection” into “my_collection”:

db.my_collection.renameCollection("old_collection");

db.createCollection("my_collection");

db.my_collection.createIndex(...); // recreate the indexes for the collection

// copy docs from old collection into the new collection
db.old_collection.find(
    {created_at: {$gte: new Date("11/01/2012")}
  ).sort({_id: -1}).forEach(
    function(row) { db.my_collection.insert(row); }
  );

// drop old collection
db.old_collection.drop();

This approach is typically faster than running a bunch of removes on your data

Oct 11

“We live in a world where there is more and more information, and less and less meaning” — Jean Baudrillard

Aug 25

Ruby on Rails :: How to add HTTP basic authentication to your staging app in Heroku

First you set up your staging app following the Heroku’s guide for Managing Multiple Environments for an App

Then edit the config/environments/staging.rb:

#config/environments/staging.rb
MyApp::Application.configure do

  # Basic authentication
  config.middleware.insert_after(::Rack::Lock, "::Rack::Auth::Basic", "My App") do |u, p|
    [u, p] == [ENV['HTTP_USER'], ENV['HTTP_PASSWORD']]
  end

  ...

And finally set the config vars:

heroku config:add HTTP_USER='foo' HTTP_PASSWORD='bar' -a myapp-staging