In one of my projects, I needed to save file from a particular url. I found a groovier way of doing this using category.
class FileBinaryCategory {
def static leftShift(File file, URL url) {
url.withInputStream {is ->
file.withOutputStream {os ->
def bs = new BufferedOutputStream(os)
bs << is
}
}
}
}
Here, I created a category class and created a static method named leftShift inside it. Here is how I used the category to download the file.
File download(String address) {
def file = File.createTempFile(address.encodeAsMD5(), ".zip")
use(FileBinaryCategory) {
file << address.toURL()
}
return file
}
A category class consists of static methods. The first argument of the method defines the type to which that method is applied. In my case, the first argument is of type File.
The advantage of using category is that it can be applied to a specific part of the code by using the use keyword.
In line 4, the static method leftShift is called and a temporary file is passed as first argument and address.toURL() is passed as the second argument. The leftShift method simply copies the content from given address to the file.
Grails provides @Category annotation to achieve the same. You can find more about it here.
Raj Gupta
raj.gupta@intelligrape.com
@rajdgreat007

Reading comments of LinkedIn wall post
Hi,
In one of my grails project, i needed to show the comments on any wall post of linkedin through API. I used the Java wrapper to connect any linkedIn account with the grails application which can be seen here. But somehow this library was not working when we need to fetch comments from any wall post and display them in our UI.
I searched a lot about it but couldn’t find anything appropriate, then i decided to use the linkedIn API directly for retrieving the data. To make GET calls , i used the Scribe java library which can be downloaded from here.
To make API calls on linkedIn, we need to have an authenticated account’s access_token and access_secret, which can be obtained by connecting a linkedIn account with the application as mentioned in this post.
Code to fetch comments on LinkedIn Wall post :-
String consumerKey = CONSUMER_KEY // key obtained by linkedIn app String consumerSecret = CONSUMER_SECRET // secret obtained from linkedIn app String accessToken = 'assess_token' String accessSecret = 'access_secret' String postId = POST_ID // id of the wall post OAuthService service = new ServiceBuilder() .provider(LinkedInApi.class) .apiKey(consumerKey) .apiSecret(consumerSecret) .debug() .build(); String url = "http://api.linkedin.com/v1/people/~/network/updates/key=${postId}/update-comments?format=json"; OAuthRequest request = new OAuthRequest(Verb.GET, url); org.scribe.model.Token accessToken = new org.scribe.model.Token(accessToken,accessSecret) service.signRequest(accessToken, request); Response response = request.send(); String jsonResponse = response.getBody() def updates = JSON.parse(jsonResponse) // contains comments data in JSON format updates.values.each {def commentData -> println "Comment : ${commentData.comment}" println "Creator: ${commentData.person.firstName}" }This code will fetch the comments from any linkedin wall post. It worked in my case.
Hope it helps.
Cheers..!!!
Vishal Sahu
vishal@intelligrape.com
www.linkedin.com/in/vishalsahu