Tinker's thoughts~

About Blog GitHub

21 May 2013
Distraction and Genius

Sometimes we always wonder if it’s a good thing to get distracted. When we are working on something important getting distracted is always annoying when we are doing something important. While I was writing this post I was getting chat pings from a friend of mine with Viber and I found it very distracting.

Distraction as Innovative juice

But I think distraction is positive. Distraction allows us to feed new ideas into our heads. It allows us to be innovative, be more human. Every invention out there was created from man’s distractions. Distraction allows humans who are focused on a single task to get a grasp of a different approach, a different direction.

Light bulb

I am pretty sure Edison got the idea of a light bulb from a candle. And the idea for Candle was from the Sun or Fire. Human’s observe nature - workings of nature and innovate new concepts and designs and ultimately new inventions.

Productive way of distraction

For example we are developing on a javascript development environment called JaggeryJS. It’s similar to NodeJS but it runs on Java (Rhino-based). We were writing code which looked a lot ugly and I thought why not use something like in Sinatra.

Previous style

previousstyle.jslink
<%
//The Coffeeshop example
include("../model/orderQuery.jag");
response.contentType = 'text/javascript';
var verb = request.getMethod();
var orderid = request.getParameter('orderid');
var order = request.getParameter('order');
var status = request.getParameter('status');
var addition = request.getParameter('addition');
var data = request.getContent();
var log = new Log();
var uri = request.getRequestURI();
uri = uri.substring(request.getContextPath().length);
if(uri != null) {
var uriMatcher = new URIMatcher(uri);
if(uriMatcher.match("/orders/{orderid}/{page}")) {
orderid = uriMatcher.elements().orderid;
}
if(uriMatcher.match("/orders/{orderid}/")) {
orderid = uriMatcher.elements().orderid;
}
}
if(data != "") {
data = parse(data);
if(data.addition) {
addition = data.addition;
}
if(data.status) {
status = data.status;
}
}
if(verb == "DELETE" && orderid != null) {
deleteOrder(orderid);
} else if(verb == "GET" && orderid != null && addition == null && status == null) {
viewOrder(orderid);
} else if(verb == "PUT" && orderid != null && addition != null) {
addAddition(orderid, addition);
} else if(verb == "PUT" && orderid != null && status != null) {
updateStatus(orderid, status);
} else if(verb == "POST" && order != null) {
addOrder(order);
} else if(verb == "GET") {
listAllOrders();
}
%>

New Sinatra-type style

gistfile1.jslink
<%
// Temporarly including dbconnection
include("/model/dbcon.jag");
var log = new Log();
var Router = require('router.js').Router;
var jbj = new Router('/MDMServer1.1/');
jbj.route('users/{userid}|GET',function(ctx){
var result = db.query("select * from Users where id="+ctx.userid);
if(result[0]!=null){
response.content = result[0];
response.status = 200;
}else{
response.status = 404;
}
});
jbj.route('users/{userid}/groups|PUT',function(ctx){
db.query("DELETE FROM UserGroups where user_id="+ctx.userid);
for (var i = 0; i < ctx.groups.length; i++) {
var group = ctx.groups[i]+"";
db.query("INSERT INTO UserGroups(user_id, group_id) VALUES("+ctx.userid+","+group+")");
}
response.status = 200;
print("User assigned Successfully");
});
//Process all the routes against the current request
jbj.process(request);
%>

So how did I get to the idea of using such a neat style for defining REST-apis in JaggeryJS? Well my love for new technologies got me to work on Sinatra, Redis, NodeJS and many more. Sinatra was my inspiration since it has neat routing that is just gorgeous.

Sinatra-style
get '/users' do
# get a listing of all the users
end

I just now witnessed another great thing of distraction. I wanted to learn new programming languages after playing with Java for years. Ruby was one of my favorites cause you can even talk Ruby code :D. I am using Octopress for this blog and I had an issue with posting the above gist code snippets. The problem was in the regex expression used in the code.

Having a bug
def render(context)
if parts = @text.match(/([\d]*) (.*)/)
gist, file = parts[1].strip, parts[2].strip
script_url = script_url_for gist, file
code = get_cached_gist(gist, file) || get_gist_from_web(gist, file)
html_output_for script_url, code
else
""
end

Since I was familiar with Ruby I got the bug figured and fixed -

Bug free
def render(context)
if parts = @text.match(/([\w]*) (.*)/)
gist, file = parts[1].strip, parts[2].strip
script_url = script_url_for gist, file
code = get_cached_gist(gist, file) || get_gist_from_web(gist, file)
html_output_for script_url, code
else
""
end

Having a good grasp and knowledge of different sectors of technology allows oneself to be lot better. We get a good understanding of different paradigms used and how those can help solve our problems.

Update :- The above bug I found was regarding sharing private gists. So if the gist is public the original code will work fine as well.


Till next time mate,
Dulitha at 22:46

About Blog GitHub