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

Grails TagLib mocking collaborators returns with Cannot invoke method [method] on null object


If you find yourself testing a TagLib and the TagLib uses a service, and you want to mock out that service in your test, you will need to retrieve the taglib from the applicationContext, otherwise setting your mocked service wont' work.

Example that won't work:

@TestFor(WebTagLib)
class WebTagLibTests {

void testTealium() {
tagLib.springSecurityService = mockSpringSecurityService(userId)
tagLib.geoIpService = mockGeoIpService('CAN')

assert applyTemplate('') == 'Hello World'
}
...

Returned exception:

org.codehaus.groovy.grails.web.taglib.exceptions.GrailsTagException: Error executing tag : Cannot invoke method isLoggedIn() on null object

Example that will work:

@TestFor(WebTagLib)
class WebTagLibTests {
private static def userId = '12345'

void testTealium() {
def tagLib = applicationContext.getBean(WebTagLib)
tagLib.springSecurityService = mockSpringSecurityService(userId)
tagLib.geoIpService = mockGeoIpService('CAN')

assert applyTemplate('') == 'Hello World'
}

...

Saturday 12 January 2013

Having fun with Javascript

So I've been reading John Resig's "Secrets of the Javascript Ninja". In same ways I regret not reading this any sooner! I thought I knew the concepts of Javacript, but John really digs deep into it and I found myself looking at this curry code example he gave and scratching my head, so I decided to dig more and blog about it. Look for more posts like these where I will be dissecting his snippets more.


Function.prototype.curry = function () {
var fn = this, args = Array.prototype.slice.call(arguments);
return function () {
return fn.apply(this, args.concat(Array.prototype.slice.call(arguments)));
};
};


Coming from a backend technology (ie. Java), I was a little baffled by this at first. After all, I thought I knew a thing or two about javascript. Howeveer, the first task was to try to invoke the above function so I can trace the steps to gain a better understanding. The first thing I did was to think of an scenario to call this:


console.log(Math.min.curry('1', '2')('3')); // == 1

Seemed a little unnatural, but sure I can live with that.

Once I figured that out, the next step was to see what the arguments parameters was. You'd think that they were the same, but unfortunately I forgot the fact that it was returning a new function, that means the context of `this` changes (which I knew) AND that the arguments list also changed. Once I figured that out then the rest came easy.

The lesson of the day is: look for places where it's returning a new function...the contexts (this and arguments) will most likely change!
 


Thursday 10 January 2013

Grails: Testing classes in src/groovy

I spent some time today creating an annotation parsing that lives in src/groovy of a Grails application. I wanted to unit test it to ensure the behavior is correct. Since it's not a controller or service or a domain class, the following steps were required to make it work.

1.) Create the test under test/unit/{package}/{class under test}Tests
2.) Make sure the test class extends GroovyTestCase (a built in base class in Groovy)
3.) Also, make sure that the file name matches the class name (unlike Java, Groovy classes don't have to match the file name).

Once you have those requirements then you can run your tests just like any other Grails context aware classes.

Tuesday 8 January 2013

Intellij: Unable to connect to Perforce in Ubuntu

So I upgraded to Intellij and unfortunately my company is still on Perforce, so I had to set that up with Intellij.

The problem that I came across was this when it tried to connect:


bash: ./p4admin.bin: cannot execute binary file

Why? I downloaded both the p4 and the p4 Visual client and extracted the p4 binary to the p4v/bin directory and pointed Intellij to it. 

It turned out that I was using the 64 bit version in Ubuntu and only the 32 bit was supported (even though my Ubuntu was 64bit).

Once I configured the correct p4 then everything worked.