Archive for the ‘ Programming ’ Category

note to myself on mongoDB procedure

For PHP MongoDB Driver

sudo apt-get update
sudo apt-get install php5-dev php5-cli php-pear
sudo pecl install mongo
// if apache2
cd /etc/php5/apache2
sudo vim php.ini
add "extension=mongo.so" without double quotes
/wq! and enter
sudo /etc/init.d/apache2 restart

For MongoDB Server

sudo apt-get adv --keyserver keyserver.ubuntu.com --recv 7F0CEB10
sudo vim /etc/apt/sources.list
add "deb http://downloads-distro.mongodb.org/repo/debian-sysvinit dist 10gen" without double quotes
/wq! and enter
sudo apt-get update
sudo apt-get install mongodb-10gen

To launch mongodb commandline

mongo

Learning generics in Java

Good learning if you are interested in learning Java. :)

Link

pulling query string in node.js

in web development, pulling data from query string is the basic and fundamental task. In node.js, the syntax is like this:

http://{host}:{port}/?name=shinstudio.com
1
2
3
4
5
6
7
var http = require('http'), url = require('url');
http.createServer(function(request, response) {
    response.writeHead(200, {"Content-Type":"text/plain"});
    var urlObj = url.parse(request.url, true);
    response.write("Hello " + urlObj.query["name"] + "!\n");
    response.close();
}).listen(8000);

you can actually test my test app here:

http://www.groupstick.com:8001/?name=shinstudio.com

memcached with java

I found a great post regarding the subject. It’s a very good read.

Mobile App Engineer Trend

Web vs. Mobile web

I may be wrong, but it seems to me that many tech companies are investing more resources (engineering, marketing, and budget) into mobile strategy. The example? Apple.

It’s still that web pages are more dominant than mobile web pages, but more and more companies are providing alternative web pages just for mobile devices (mobile webkit).

Read more

JavaScript String.replace with regex

I often use String.replace in JavaScript whenever I have to use JavaScript. Today was the day that I ended up with using it for my work. I know that String.replace supports regular expression to match your string, but I was not clear if I could use “capturing groups” and “quantifiers”. As I guessed, I was able to use quantifiers in the replace string like the sample below:

    var link_template = '<ol>\
        <li><label>Link 1 Title:</label><input class="text" id="relLink1title" name="relLink{1}title" value="" /></li>\
        <li><label>Link 1 URL:</label><input class="text" id="relLink1url" name="relLink{1}url" value="" /></li>\
        <li><label>Link 1 Img URL:</label><input class="text" id="rellink1imgurl" name="rellink{1}imgurl" value="" /></li>\
        <li class="btn"><span><a href="#" class="new-related-link">Add a New Link</a> | <a href="#" class="remove-related-link">Remove</a></span></li>\
        </ol>';
 
    link_template = link_template.replace(/(<label>.*?)(\d)(.*?<\/label>)/im, '$1 2 $2');

ByteArray in Flash slide

I found this ByteArray slides by Lee Brimelow, which he presented at the Flash on the beach.

I knew that low level manipulation is so powerful tool, but had very little idea of how I could use them. This slide shows a good start for a beginner like myself.

Peronally, I used bitwise operators to extract red, green, blue color values. However, with the bitwise operators it works for many other things.

One of samples that Lee Brimelow presented was this:

for (var i=0;i<100;i++) {
    if (i&1) {
        // i is odd
    } else {
        // i is even
    }
}

To check whether the number is odd or even and according to Lee, it’s 600% faster than using the modulus operator.

RESTful web services

Many developers heard/use/implement/develop RESTful web services daily basis including myself. It seems likely that we all know what it is and how it works. However, if we ask ourselves why we are using it, what would be your answer?

My answer to this question is this:

To make *services* more sense.

We all know that RESTful web services utilizes http methods such as GET, POST, PUT, and DELETE. Most commonly used methods to me are GET and POST. And technical aspects of discussion can keep going. However, the question is how we can utilize RESTful web services and how we can make it so that other developers can consume them in easy way? Eventually RESTful web services is for openness. Facebook opens up their RESTful API so that other developers or 3rd party application of facebook can pull up information on users or something like that.

With RESTful API, it is *possible* that you can organize calls.

Let’s look at this imaginable API path for getting a particular movie info.

http://RESTfulAPIHost:Port/movie/

By just looking at the API path, you can take a good guess of what it might return to you. And then after that, you naturally think ahead and append movies name to it.

http://RESTfulAPIHost:Port/movie/dark-knight/

Looking at the API path, I would naturally think that I could expect movie information for the movie “Dark Knight”. This natural thought process leads to more details API paths like these:

http://RESTfulAPIHost:Port/movie/dark-knight/actors/

http://RESTfulAPIHost:Port/movie/dark-knight/credits/

http://RESTfulAPIHost:Port/movie/dark-knight/showtimes/

pretty xml & json

I was working on a feed project at work and dealing with lots of XML and JSON data. If data were well indented and formatted, my tasks would have been a lot smooth. However, the reality was not like how I imagined as usual.

At first I googled about it. I kinda found one but the application required actual file, which could be okay. However, I could see myself copying and pasting xml into a file and upload, which is not quite ideal.

So I decided to make one for myself and my tasks. Let me know if it works well for you or you find a bug.

Here they are:
http://www.shinstudio.com/tools/pretty-xml
http://www.shinstudio.com/tools/pretty-json

Rotating view in iPhone app

When iPhone came out for the first time, I was amazed at how iPhone rotated safari web browser. I wondered how iPhone would rotate it and was not aware that not all iPhone applications are rotatable, but now I know how.

Let’s assume that we already have your own extended UIViewController class. UIViewController class has these two classes for the rotate feature:

1
2
3
4
5
6
7
-(BOOL)shouldAutorotateToInterfaceOrientation:
	(UIInterfaceOrientation)interfaceOrientation {
}
-(void)willRotateToInterfaceOrientation:
	(UIInterfaceOrientation)orientation
		duration:(NSTimeInterval)duration {
}

shouldAutorotateToInterfaceOrientation method. You simply just return “YES” so that the viewcontroller rotate its views automatically. The 2nd method “willRotateToInterfaceOrientation” is the one you want to implement.

Let’s say you already had one UIView object with white color background. When you rotated your iPhone or iPhone simulator to landscape orientation, the UIView would be rotated. However, the view wouldn’t fit the screen. To make the view to stretch to the all 4 edges of the screen, I would write something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
-(void)willRotateToInterfaceOrientation:
	(UIInterfaceOrientation)orientation
		duration:(NSTimeInterval)duration {
	CGRect appFrame;
	appFrame.origin = CGPointMake(0.0f, 0.0f);
	if ((orientation == UIInterfaceOrientationLandscapeLeft) ||
			(orientation == UIInterfaceOrientationLandscapeRight))
		appFrame.size = CGSizeMake(480.0f, 300.0f);
	else
		appFrame.size = CGSizeMake(320.0f, 460.0f);
 
	[contentView setFrame:appFrame];
}

As you can see, every time view gets rotated, you would need to use setFrame method to set the new frame with a new size and new origin.