Newer posts are loading.
You are at the newest post.
Click here to check if anything new just came in.

April 19 2012

On using mac instead of linux ...

This is a very common and constant discussion at my workplace. And don’t get me wrong, I love linux. But Jamie Zawinski says it in way that is very similar to what I feel about it:

I use a Mac instead of Linux on the desktop for a reason: because I think that the design and consistency that Apple’s UI brings is extremely valuable. I don’t buy computers based on how fast they are, I buy them based on how easy it is to get things done with them, and Apple is the hands-down winner on this pretty much across the board. (Oh, also because I want my audio card to work, but that’s neither here nor there.)

This quote is taken from a post in his blog where he talks about why he prefers to use Safari instead of Firefox on his mac, and you can read it entirely here.

March 20 2012

Answer by rogeriopvl for How to end execution of a block in Ruby?

To exit a block or loop use the break keyword.

return will exit a method.

March 19 2012

Answer by rogeriopvl for How to create expandable FAQ page in HTML?

Simple example using jQuery:

JavaScript/jQuery

<script type="text/javascript">
$(document).ready(function(){
    $('.faqlink').click(function(){
        $('.content').hide();
        $(this).next('.content').show();
    });
});
</script>

CSS

<style type="text/css">
.content {
    display: hidden;
}
</style>

HTML

<h2>My FAQ</h2>
<a class="faqlink" href="#">Link 1</a>
<div class="content">lorem ipsum</div>
<a class="faqlink" href="#">Link 2</a>
<div class="content">lorem ipsum</div>
<a class="faqlink" href="#">Link 3</a>
<div class="content">lorem ipsum</div>

March 13 2012

Having fun developing useless apps

I always thought that the first time I got on the Hacker News and Proggit front-pages would be with some awesome and complex project. I was wrong.

Part I - Like A Boss

On 25th November, while browsing the web, I found a funny project developed by Zach Holman called Fuck Yeah. This was a simple API developed in Node.js that received a piece of text, searched Google Images for that piece of text, and added that text along with a “Fuck Yeah!” to the first image found.

I found the project to be pretty cool, and since I got a little rusty with nodejs, I decided to fork the code and make a new version of the API, this time called Like a Boss. You can pretty much guess what it does. You can check out the code at Github by the way.

Since I had some spare time, I decided to go further and build a website that presented the generated images in a more good looking way. And here’s the result.

Hack Like A Boss

I decided to post the “Like a Boss” website to Hacker News. It got upvoted somehow, and BAM! Hacker News front-page. In 2 hours the website had about 2000 pageviews. This was cool, but not without some problems showing up that had to be solved in order to keep the code from crashing the server. Still, Heroku, the cloud application platform I used to host my nodejs app, handled all the traffic pretty well with only one web worker (included with the free plan).

After about 3 hours, the Hacker News post dropped from the front-page, and traffic almost vanished.

Part II - HTTP Cats API

On December 14th, again, while browsing the web, someone posted on Hacker News an awesome Flickr set of cat pictures authored by Tomomi Imura with each cat posing accordingly to a HTTP Status Code. These pictures ended up being a huge success.

I instantly thought about how awesome would be to have those cat images show up when an error occured on a web-server. I fired up vim and hacked some nodejs code to serve those images through a simple API. After some minutes, the HTTP Cats API was born. I pushed it to Heroku, and it was rocking. I sent the link to a couple of friends, we all laughed for a while and I thought that was it.

HTTP Cat 404 Not Found

After a while, I posted the API link to Hacker News and Proggit. The Hacker News post never got voted, the Proggit one got allot of upvotes, and soon I was on Proggit front-page. An avalanche of traffic ensued.

The post got duplicated on Hacker News, and this time it got allot of upvotes (probably a user with more influence). This was when all hell broke loose. Not only it got on the first-page it was also ranked in 2nd place.

The results where: on the first day I got 19.412 unique visitors. The second day, 21.764 unique visitors. The two days together summed up 255.034 pageviews. These values are amazing for a simple app that serves cat images. Actually, while I write this post, which is 2 months later, it still has around 100 unique visitors a day. And I’m not even counting direct API calls, just the index page visits.

So what?

Yeah, I know these web applications are pretty much useless, but I had allot of fun hacking them and watching the traffic hit the sites. And of course, it doesn’t really matter the fact that they are useless, as long as you have fun, learn and make other people smile, that’s what really matters.

March 08 2012

Answer by rogeriopvl for Getting the value of href attributes in all tags on a html file with Python

Well, just for completeness I will add here what I found to be the best answer, and I found it on the book Dive Into Python, from Mark Pilgrim.

Here follows the code to list all URL's from a webpage:

from sgmllib import SGMLParser

class URLLister(SGMLParser):
    def reset(self):                              
        SGMLParser.reset(self)
        self.urls = []

    def start_a(self, attrs):                     
        href = [v for k, v in attrs if k=='href']  
        if href:
            self.urls.extend(href)

import urllib, urllister
usock = urllib.urlopen("http://diveintopython.net/")
parser = urllister.URLLister()
parser.feed(usock.read())         
usock.close()      
parser.close()                    
for url in parser.urls: print url

Thanks for all the replies.

Getting the value of href attributes in all tags on a html file with Python

I'm building an app in python, and I need to get the URL of all links in one webpage. I already have a function that uses urllib to download the html file from the web, and transform it to a list of strings with readlines().

Currently I have this code that uses regex (I'm not very good at it) to search for links in every line:

for line in lines:
    result = re.match ('/href="(.*)"/iU', line)
    print result

This is not working, as it only prints "None" for every line in the file, but I'm sure that at least there are 3 links on the file I'm opening.

Can someone give me a hint on this?

Thanks in advance

March 05 2012

Convert the output of os.cpus() in Node.js to percentage

Is there a way to convert the os.cpus() info to percentage? Just like the output of iostat (on the CPU section).

My code:

var os = require('os');
console.log(os.cpus());

The output:

[ { model: 'MacBookAir4,2',
    speed: 1800,
    times: 
     { user: 5264280,
       nice: 0,
       sys: 4001110,
       idle: 58703910,
       irq: 0 } },
  { model: 'MacBookAir4,2',
    speed: 1800,
    times: 
     { user: 2215030,
       nice: 0,
       sys: 1072600,
       idle: 64657440,
       irq: 0 } },
  { model: 'MacBookAir4,2',
    speed: 1800,
    times: 
     { user: 5973360,
       nice: 0,
       sys: 3197990,
       idle: 58773760,
       irq: 0 } },
  { model: 'MacBookAir4,2',
    speed: 1800,
    times: 
     { user: 2187650,
       nice: 0,
       sys: 1042550,
       idle: 64714820,
       irq: 0 } } ]

I would like to have the "times" metric converted to percentage, just like is show on the iostat command:

  cpu
us sy id
6  3 91

I understand that the values in the nodejs function are in CPU ticks, but I have no idea what formula should I use to convert them to percentage :)

Thanks.

March 04 2012

February 26 2012

February 19 2012

February 12 2012

February 08 2012

Answer by rogeriopvl for Handling OPTIONS request in nginx

I'm probably late, but I had the same problem, and found two solutions to it.

First is tricking Nginx that a 405 status is actually a 200 OK and then proxy_pass it to your HAProxy like this:

error_page 405 =200 @405;
location @405 {
    root /;
    proxy_pass http://yourproxy:8080;
}

The second solution is just to catch the OPTIONS request and build a response for those requests:

location / {
    if ($request_method = OPTIONS ) {
        add_header Content-Length 0;
        add_header Content-Type text/plain;
        return 200;
    }
}

Just choose which one suits you better.

I wrote this in a blog post where you can find more details.

February 05 2012

January 29 2012

January 27 2012

Nginx and the HTTP OPTIONS method

The current version of Nginx (1.0.x) doesn’t seem to support HTTP OPTIONS requests. It returns 405 "Method Not Allowed" whenever this request is sent.

Here’s an example using curl from the terminal:

$ curl -X OPTIONS busydudes.com

<html>
<head><title>405 Not Allowed</title></head>
<body bgcolor="white">
<center><h1>405 Not Allowed</h1></center>
<hr><center>nginx/1.0.11</center>
</body>
</html>

HTTP OPTIONS requests are essential if you’re doing cross-site HTTP requests and need to obtain the server authorization for the URI where the request originated from.

To solve this problem I found two solutions.

Solution 1 (not recommended)

If you’re using another web server behind Nginx and want the request to be handled by that web server, you can trick Nginx into believing that the HTTP status 405 is actually a 200 OK status, so it will just delegate the request to your webserver using the proxy_pass directive. Just keep in mind that this is an ugly fix. Edit your nginx.conf file and add the following:

error_page 405 =200 @405;
location @405 {
    root /;
    proxy_pass http://localhost:8080;
}

Solution 2

If you’re just using Nginx as your main server, or just want to obtain the authorization response for the origin domain, then this is definitely the best solution. Edit your nginx.conf file and add the following:

location / {
    if ($request_method = OPTIONS ) {
        add_header Access-Control-Allow-Origin "http://example.com";
        add_header Access-Control-Allow-Methods "GET, OPTIONS";
        add_header Access-Control-Allow-Headers "Authorization";
        add_header Access-Control-Allow-Credentials "true";
        add_header Content-Length 0;
        add_header Content-Type text/plain;
        return 200;
    }
}

You should edit the http://example.com to match the origin URI you want to allow. If you want to allow any origin just use the * wildcard.

January 22 2012

January 15 2012

January 08 2012

January 01 2012

December 25 2011

Older posts are this way If this message doesn't go away, click anywhere on the page to continue loading posts.
Could not load more posts
Maybe Soup is currently being updated? I'll try again automatically in a few seconds...
Just a second, loading more posts...
You've reached the end.