Saturday, 2 February 2013

Rails - RabbitMQ - undefined method `topic' for #

So I'm writing a Rails app on the side and one of the things I wanted to use was a massaging system (so I can potentially write apps in different languages and have it share a common message queue). I am going through the tutorials, and they explained that the following should be done using the following:

require "rubygems"
require "bunny"

connection = Bunny.new
connection.start

channel  = connection.create_channel
# topic exchange name can be any string
exchange = channel.topic("weathr", :auto_delete => true)
...

However, when I run my app, I got this error:

undefined method `topic' for #
What gives? Upon looking at the source, it was found that apparently in 0.9.0 (which is still in beta), the api has changed quite a bit if you are using topics.
Therefore the fix is to upgrade to the pre version of 0.9.
In your Gemfile, add the following:

gem "bunny", ">= 0.9.0.pre6"

Thursday, 31 January 2013

RVM is not a function, selecting rubies with 'rvm use ...' will not work.

The problem:

So I'm writing a rails app using my Ubuntu 12.10 box at home, and after I installed rvm and tried to follow the tutorial to set up rails here @ http://www.unfoldingcode.com/2011/05/using-rvm-to-install-rails-31-on-ubuntu.html

However, I got the error stating that
'RVM is not a function, selecting rubies with 'rvm use ...' will not work.'

The solution:

You need to change your terminal emulator preferences to allow login shell. Sometimes it is required to use `/bin/bash --login` as the command. Please visit https://rvm.io/integration/gnome-terminal/ for a example

Now I don't want to go through the logging process as suggested since it messes up my configuration, so what I ended up doing to make it work was simply pasting this following line into my ~/.bashrc file:

[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*

Tuesday, 29 January 2013

Grails plugin: REST facilities set timeout on withHttp

One of my tasks at my work currently is to implement a timeout on web service calls we make. We currently use the withHttp closure to make our calls inside of our Grails projects. This handy call is nice but unfortunately it doesn't have any options to set the default timeout. To set the timeout, you will have to do the following:

  withHttp(uri: host )
  {
   def client = getClient()
   client.setHttpRequestRetryHandler( new DefaultHttpRequestRetryHandler(1, true) )
   def httpParams = client.getParams()
   httpParams.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, TIMEOUT)
   httpParams.setParameter(CoreConnectionPNames.SO_TIMEOUT, TIMEOUT)
   
   handler.failure = { resp ->
    return new Response(resp)
   }
   
   request( GET, XML ) {  req ->   
    
    response.success = { resp ->
     def payload = resp?.entity?.content?.text
     rr = new Response(resp)
    }
   }
  }
Notice that we are also setting the retry count = 1
DefaultHttpRequestRetryHandler(int retryCount, boolean requestSentRetryEnabled)
Also notice that in addition to setting the connection timeout, we also should set the socket timeout (otherwise weird exceptions will occur if there's a problem).

Friday, 25 January 2013

AngularJS and Jetbrains LiveEdit

Some people might know about the LiveEdit feature in the Jetbrain's Webstorm IDE. It's an awesome feature for running code in your IDE and have it show up right away on the browser, without you refreshing the page.

However, it is supported only on Chrome (Chromium for me since I'm on Ubuntu).

I got the issue that whenever I run any ajax calls, I get this error: 

Origin null is not allowed by Access-Control-Allow-Origin.

 The solution: I searched the Internet for solutions, and even though some say either one of these flags would work for local testing, I had to add both of them in order for it to work. Go figure!

chromium-browser --allow-access-from-files --disable-web-security

Thursday, 24 January 2013

AngularJS: Error: Unknown provider: $configServiceProvider <- $configService

So I'm writing a small app using AngularJS and after reading the awesome tutorial I decided to try it. I started out with their angular-seed app and the first thing I wanted to create was a ConfigService:


In services.js



/* Controllers */

function DailyTipController($scope, $http, $configService) {

//    $http.get('')

    $scope.tip = $configService.version;

}





So far so good right? Nope. When I run my app, this is what I got:

Error: Unknown provider: $configServiceProvider <- anonymous="" at="" configservice="" error="">) at file:///home/tchan/repo/codetipdaily/public/client/app/lib/angular/angular.js:2641:15 at Object.getService [as get]

Alright...what's happening here?

I went through the documentation and found that in addition to registering the the controller, you also need to inject the controller with the right parameters:


In controllers.js


DailyTipController.$inject = ['$scope','$http', 'configService'];





Sunday, 20 January 2013

Center a set of images instead of using float:left

I was working on some css today (did I tell you I suck at it?) and I find a tip somewhere that I'd thought I'd share.

The problem:

I had an issue with a couple of icons floating to the left:

http://screencloud.net/v/n58h

but I wanted the icons to be centered on the page instead.

The solution:

Remove the float:left and add in display:inline-block:

The result:

http://screencloud.net/v/3s3u

Problem solved!


Friday, 18 January 2013

Be aware of the ordering of operators

I was working on some Groovy code that required to use of the Elvis operator and an operation involving the addition of two maps, like so:

utag_map = utag_map + attrs.extraParams ?: [:] //add any user defined data to the map

However, this was the issue I was getting when I run my tests

                java.lang.NullPointerException

What gives?

I figured it out, and the reason of that is because of the precedence of the operations. You will need to apply the parenthesis to the second part of the operation first!

Solution:

utag_map = utag_map + (attrs.extraParams ?: [:]) //add any user defined data to the map