Saturday, September 6, 2014

Binary Tree using HTML5 Canvas and JavaScript

Hi Guys -

I was just playing around with HTML5 Canvas and thought it will be fun to create a binary tree implementation using JavaScript.

I tried this a few years ago with Java and applets. Well, yeah u guessed it, it was a PITA.

With JavaScript and Canvas, it was a smooth ride.

The code below is only for the purpose of demo. Some nodes might overlap if the tree has a lot
of nodes.

Tested on Google Chrome

1 - Create a file index.html and add the below code to it.
      It contains an
      a - Input box where the user will enter the number to be added
      b - A button to add entered number
      c - A canvas element
      d - A script tag

2 - Next we will add a node object. Node, as the name suggests, represent a node in the tree
     Add the below code in the script tag of the index.html


3 - Next we will add code to draw a line from parent to child. Add the below code to the script tag    

4 - Next we add the core logic of a binary tree. To understand that logic, you should know how
     a binary tree works. Add the blow code the script tag


5 - Now we  need to create a binary tree object to which we can add node
     Add the below code to the script tag

     var btree = new BTree();

6 - Ok. We are almost done. We just need to wire the input box and add to tree button
     Add the below code to the script tag


Thursday, August 21, 2014

Elasticsearch 101 - How secure is it ?

So, I wanted to add full text search functionality to one of my projects and after doing some research I decided to go with Elasticsearch.

While reading the docs, I came across two RESTful apis which were good but also dangerously exploitable.

So what are those apis - (assuming that a local instance is running on port - 9200 )

1 - curl XGET 'http://localhost:9200/_nodes?pretty=true'
     Run this command from your terminal and you will see a lot of details about your machine

  • Paths - 
    • logs: /Users/I/am/a/hacker/logs
    • home: /Users/I/can/see/your/home
  • OS info -
    • cpu details
    • memory details
    • and yeah JVM details
  • Network info - 
    • IP address
    • mac address

2 - curl -XPOST 'http://localhost:9200/_cluster/nodes/_master/_shutdown'   or
     curl -XPOST 'http://localhost:9200/_shutdown'

     Any user can execute the above command and bring down your whole cluster. 
     Still most big companies have pretty good firewall setup so its hard to get access to a machine but
     still, imo, there should be some kind of permission to execute these commands

Anyways these are some interesting things which I came across while reading the docs.

Sunday, April 6, 2014

Connecting To MongoDB From Node.js Beginner's Basic

Ok guys - Here we will create a simple file that will connect to a local instance of Mongo and read data from the 'test' database.

Assumptions - 
1 - A local Mongo server is running at port 27017 (default port)

Step 0 - Create a directory called 'testing' and cd into it.

Step 1 - Connect to mongo using a mongo shell
Step 2 -  Add some data to a collection named 'heroes' in the test db.
              use test
             db.heroes.insert([{name: 'Super Man'}, {name: 'Bat Man'}]) 

Step 3 - Create a file called simple_connect.js and the following content to the file


Step 4 - If you haven't already installed the 'mongodb' module, run
             npm install mongodb

Step 4 - Run
             node simple_connect.js


Friday, March 28, 2014

Rails Default Scope Affects Model Initialization

Hi Guys - Ran into this while working on a project.  Now, default scope works fine for most of the stuff but it also has a secret to itself which I didn't realize (my bad).

Lets consider two cases,  one in which default scope affect is realized and other one in which it isn't.

class ModelNotAffected < ActiveRecord::Base
  attr_accessible :name, :is_deleted

  default_scope where(:is_deleted => 0)


class ModelAffected < ActiveRecord::Base
  attr_accessible :stall, :is_active

  default_scope where(:is_active => 1)


How ?{:name => 'Spider Man'})
# => {:name => 'Spider Man', :is_deleted => 0}
# This is what u kind of want{:stall => Art Works''})
# => {:stall => 'Art Works', :is_active => 1}
# This might not be the case. There might be some condition which would make the stall as is_active

Well note that how default scope affects model initialization.


Thursday, March 20, 2014

Nodejs Watching a file + Spawning new processes to list file information

Hi guys - Trying to get my hands dirty with Node.js and its pretty cool.

Its so easy to do stuff. Here is a small code snippet which demonstrate watching a file and spawning a child process to list the file info.


Tuesday, March 18, 2014

Truncating text or string in Ruby

Hi Guys - Today I was working on a small issue where I had to add some client side validation.

The fix was to add limit to the number of chars in a text area. The fix is as easy as adding 'maxlength=n' attribute.

But this can be circumvented easily in the client side. So what ?

In the server side, are we going to throw an error and raise an exception that the text is too long ?

Well another way is to truncate this text.

In ruby, simply do this -

This will simply truncate the text


Sunday, March 16, 2014

https curl POST, GET, DELETE Images or Video files

Hi guys,

So I was trying to test some api's from my terminal and I decided to use CURL for doing POST, GET and DELETE.

Now, the api's use HTTPS so I had to figure out how to do https curl. After Googling for few minutes, I was able to figure it out.

There were few things which were missing from most of the posts. Hopefully this will help someone or save someone's time.

Few points to note

#1 - Url is in double quotes
#2 - query string is encoded.

# upload image/video using POST
curl -i --trace -H --data-binary "@/some/path/to/video/or/image/file" "https://somebaseurl/action?encodedQueryString" -v

#get file using GET
curl -i --trace -H -G "https://somebaseurl/action?encodedQueryString"

# delete file
curl -i --trace -H -X DELETE "https://somebaseurl/action?encodedQueryString"


Tuesday, February 18, 2014

jQuery - waiting for multiple AJAX requests to finish

Problem I am solving -

Wait for N or Multiple or Unknown number (Depends on some logic) of ajax request using JQuery to complete, i.e.

$.when(N ajax request).then(do something)

Lets take a look at easier version of the problem, what if we want to wait for only two calls

Well that was easy, so what... huh

What if you wana wait for n number of calls ? You cannot do $.when(call1, call2, ....., calln).then...

Lets take a look at a use case -

Lets say there is an array of ids and for each id in the array, we gota make a call and when all the data is loaded, we want to show a big red BLINK.

Here is how we gona do this -