Archive for May, 2008

Mongrel + Nginx: Deploying to a subdirectory

Tuesday, May 27th, 2008

Though using subdomains is all the rage right now, there are certianly instances where you may want to deploy your rails application to a subdirectory such as:

http://www.johnyerhot.com/myrailsapp

Of course Nginx makes it super easy to do so. If you need to get your webserver ready with Nginx, PHP running as a FCGI instance, and Rails check out my other how to.  

Now, onward!

First create a new virtual host.  In my case, for yerhot.org it would look like this:
server {
listen 80;
server_name yerhot.org;
access_log /var/www/yerhot.org/logs/access.log;
error_log /var/www/yerhot.org/logs/error.log;
location / {
root /var/www/yerhot.org/;
index index.html;
}
}

Pretty simple setup, telling Nginx to listen on port 80 for requests for yerhot.org, where to store logs, and finally setting up the site root at /var/www/yerhot.org.

All we would have to do to have Nginx redirect to our Rails app when looking for yerhot.org/myrailsapp is make another location block (in other words, place this right before the last curly brace).

location /myrailsapp {
proxy_pass http://localhost:8000;
}

Now, all requests for /myrailsapp will get proxied to port 8000. Now fire up your Rails app on port 8000.
mongrel_rails start -e production -p 8000 -d
Restart Nginx:
/etc/init.d/nginx stop
/etc/init.d/nginx start

And….

Crap. Rails is looking for a ‘myrailsapp’ route, which there is none.  No, no, don’t create one – we’ll need to use a little known feature of Mongrel to fix the problem.. the prefix.
Stop Mongrel…
mongrel_rails stop
And try this:
mongrel_rails start -e production -p 8000 -d --prefix=/myrailsapp

And… your app should fire right up. Pretty neat, if you wanted to you could use it forward to a Mongrel Cluster, a FCGI instance of PHP (from my other post), or lots of stuff.

I’m on Alexa.. no. 1,257,367 world wide..

Monday, May 26th, 2008

Holy crap, apparently I’m the 1,257,367th most visited site in the world… or  the  the 740,255th most popular in the US.. apparently. I have  a hard time believing either of these  figures.  I know that the PHP/Nginx/Rails Post is very popular, as well as  the Google Maps and the one about how to use Rails Helpers in controllers, and finally the one about BackgrounDRB, but damn.

Anyways, pretty neat.

Usually Frown Upon this Stuff but…

Friday, May 23rd, 2008

Quite Possibly the most awesome music video ever. Thank you Weezer for restoring my faith in the world…

Trip to Seattle & Washington State

Thursday, May 22nd, 2008

Just booked a little week long excursion to Washington State for next month.  We’re flying in to Seattle on June 14th and will be returning on 21st.  Should be super sweet, the Pacific Northwest is my favorite region of The States and its been about 6-7 years since I’ve been out there.

Jen’s never been there before, so I’m very sure her mind will be blown.  The tentative itinerary is 2-3 days in Seattle, 1 day for The Olympic Nat’l Park/Washington Coastline, and a day for a quick Portland/Mt. St. Helen’s trip.  Maybe only 2 days in Seattle and another day split between Mt. St. Helen’s and Mt. Rainier and a full day for Portland… hell, we don’t know maybe we’ll just see what we feel like doing, thats what we usually end up doing on vacations.  For our trip to Florida, we literally had no plan except to go to Florida and went with the flow the whole time and it was great.

I’d also like to go out to Spokane and visit some relatives, we’ll have to see.. so much to do so little time.

I’ll for sure be posting some pics when we get back…

Super Simple Ruby Web Scraper

Monday, May 19th, 2008

Alrighty folks. Quick walk though for scraping remote web data with Ruby. This is how I did it for my little web scraper I wrote on Saturday..

DISCLAIMER: Web Scraping is kind of a gray area.. don’t steal things that are copywritten and don’t be a jerk. Give credit where credit is due..

First thing is first. You’ll need to install the Mechanize Ruby Gem.

sudo gem install mechanize

Mechanize is pretty slick. It will iterate through a given url and let you access various html elements. Further, you can use Hpricot methods to further grab data.Lets get going..

require ‘rubygems’require ‘mechanize’require ‘uri’

url = “http://www.johnyerhot.com”

The way this is set up, you MUST have a complete url.

@mech = WWW::Mechanize.new

@page = @mech.get(url)

Now, lets say we want to get all the urls for embedded images from the webpage (http://www.johnyerhot.com)..

@imgs = @page.search(”img[@src]“).map {|src| src['src']}

You’ve now got an array (@imgs) with all the urls for embeded images! What we actually did was use Hpricot’s search method to look for and image tags and sucked out the src attribute of that tag. Mechanize does have its own methods for grabbing tags also, for example, you can grab all the link targets from every link in the web page.

#remember @page is just our mechanize instance
# w/ http://www.johnyerhot.com

@links = Array.new
@page.links.each do |link|
@links << link.href
end

Now lets weed out any links to non-images:

@links.each do |link| #yeah we’re only collecting jpgs

if (link.to_s.include? “.jpg”) || (link.to_s.include? “.JPG”) || (link.to_s.include? “.jpeg”) || (link.to_s.include? “.JPEG”)

@imgs << link
end
end

Finally, lets actually grab all those pictures and save them locally using Open URI…

@counter =0
@imgs.each do |image|
url = URI.parse(image)#parse the url and separate need info
Net::HTTP.start(url.host, url.port) { |http|
#appeand the image path with the web root.
image = http.get(image)#actually make the file to save
open(”#{url.host}_#{counter}.jpg”, “wb”) { |file|
file.write(image.body)
counter = counter + 1
}
end

And there you have it! Put it all together and you should have a functioning Ruby web scraper…Sort of. You still have to account for relative vs. absolute urls, are you gonna let in more than jpgs?, what if you need basic authentication for the url? There are still some missing pieces that need to be implemented to have this be ready for general use, but the core is there.
Further Reading
Mechanize Docs
Hpricot
Open URI
Ruby net/http Docs

Afternoon at Lake Superior

Sunday, May 18th, 2008

Spent the afternoon down at the Lake today with Leeroy and the Mrs.  I have to admit, Duluth in the summer time is not that bad.  If the tourists were not around it would better, but today was pretty beautiful.  Lake More pictures here.

yerhot.org and Jen’s business

Monday, May 12th, 2008

jen gradOver the weekend my sister graduated from St. Mary’s. For some reason it gave me the idea of buying yerhot.org and then setting up a family portal complete with yerhot.org email for everyone. Prolly just through Gmail, but still john@yerhot.org or rebecca@yerhot.org would be pretty neat. Especially if we all end up moving around the country.. Just some thoughts.

In other news, Jen has decided to strike out on her own, say good bye to Qwest and teach voice lessons and do weddings/events full-time. I’m very happy about this, she is such a talented musician and should really be doing something musical. I think she’ll be pretty happy with it.

Just an update. Congrats Becca, you’ve worked really hard. Love ya.

More pictures