Archive for the ‘Programming’ Category

Deployment Nightmare: Godaddy

Tuesday, November 4th, 2008

Against the advice of, well, the entire Rails community, I attempted to deploy a project to Godaddy.

Godaddy proudly displays the little Ruby logo next to Php when browsing through their hosting plans, and seeing as you can get Rails hosting for $7/month through them, it seems like a decent deal, even if you know it’s gonna get slow.

Baisically, what ends up happening when deploying to Godaddy, is you upload your application to a special directory you create through their control pannel, modify your dispatch.fcgi file, .htaccess files, turn off Java and pray.  Its all supposed to run as FCGI in the end, which is not the speediest way to deploy a Rails app, but is acceptable for smaller, non-demanding applications.

The application I was attempting to deploy was a Rails 2.1 app, and I found that Godaddy is rocking Rails 1.1.6 (Release Aug 2006), so I froze Rails (standard practice), change the configs to point to the manually created database (no rake support here folks) and tried to hit it.  Nothing.  First, every time you modify the .htaccess file (and you will need to – many times) it takes 10-30 minutes for Apache to notice.  So, you’re stuck making a change, waiting, testing, washing and repeating.  If you’re using FCGI, there is literally NO output into the ‘errorlog’ Godaddy gives you, so you have to deploy with regular CGI to get any idea why your application isn’t working and then switch back to FCGI and pray.

Finally, after a couple hours of searching the interwebs and poking and prodding, I was getting somewhere – the error log started to get populated, but FastCGI wasn’t starting correctly.  After some snooping, I saw that Rails 2.1 assumes that you’re not running whatever version of Gem was around in 2006.  Gem was failing with a method not found error when Rails tries to talk to Godaddy’s almost 3 year old version.

I putzed around a bit looking for ways to skip that step of Rails’ boot process, swaping out boot.rb files from older version of Rails, hacking, etc.. and to no avail.

This is where I stopped.  I figured that even if I got past this problem, the version of Rmagick was from mid 2006 and would likely not work with my app, and I wasn’t about to try and get Godaddy to update it.  If they haven’t updated any of the Rails stuff yet, I don’t think they are going to any time soon.

My message for Godaddy is this:  I want the entire day I spend on this back.  You should stop advertising that you support Ruby/Rails when you obviously don’t.  I’d bet 90% of Rails apps in development today would not be able to run on their hosting plans. Eh.

rQuote – Ruby on Rails Stock Quote Plugin

Friday, August 29th, 2008

I merged some stock quoting stuff I had into a Rails plugin today. If you’d like to be able to simply grab real time stock quotes in your Rails app, this will do the job. Pretty much any stock symbol will work and you can enter as many as you’d like, you’ll get a hash array of hashes containing each symbol’s current value, change since open, and volume.

http://github.com/johnyerhot/rquote/tree/master

Rquote
======

Gets realtime stock quotes from Yahoo Finance. 

Its super simple to use.

Example
=======

quote = Rquote.new
quote.find("aapl", "msft") 

=> [{:change=>"-4.02", :price=>"169.72", :volume=>"16105013", :symbol=>"aapl"}, {:change=>"-0.42", :price=>"27.52",
:volume=>"27024456", :symbol=>"msft"}]

Copyright (c) 2008 John Yerhot, released under the MIT license

rDigg – Ruby on Rails Digg API plugin

Thursday, August 14th, 2008

I’ve sort of finished up rDigg today. It’s to the point I’m comfortable letting other people start to play with it anyways.

As you’d expect, rDigg is a Digg API wrapper in the form of a Ruby on Rails plugin. It still needs some work, but works pretty well.

For example:

#create new Rdigg object
digg = Rdigg.new

# find the 3 newest submissions from Kevin Rose
stories = digg.user.find_submissions("kevinrose", :count => 3)

# stories is now an array with a hash for each story
stories.first[:story] #=> the story's text
stories.first[:href] #=> the story's url
stories.first[:diggs] #=> number of diggs the story has

Grab it at: http://github.com/johnyerhot/rdigg/tree/master. I’ll have the Rdoc up at rdigg.yerhot.org later tonight You can peruse the documentation at rdigg.yerhot.org. I highly recommend you check out all methods that are available to you.

If you want to really dig in (sorry couldn’t resist) I’d go over the Digg API wiki to see what arguments you can pass.

I hope you enjoy the plugin!

Simple Meta Programming in Ruby

Sunday, August 10th, 2008

Metaprogramming.  What a lovely buzz word.  I guess I’ve heard it enough and knew what the short definition is. Metaprogramming is code that writes code.  I think it is one of those things I just never thought about, even though I had used concepts and even written some before without realizing it until recently.

Here is a short and simple example.

class Something

@my_hash = {"foo" => "1234", "bar" => "5678"}

def initialize
  @my_hash.each do |a, b|
       self.instance_eval do
             define_method(a.to_s) {b.to_s}
       end
  end
end
end

And now we can play with it.

a = Something.new
a.foo  # => 1234
a.bar # => 5678

Now, what happened here is pretty neat in my opinion. We took our hash, my_hash, and in our initialize method, created two instance methods from its values. define_method is what did all the magic. You need to pass it a proc or, like what I did, just give it a simple block (just the string value from my hash).

Pretty neat, eh?

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

Slicehost problems with Gem *FIXED*

Monday, March 17th, 2008

Today I set up 470.johnyerhot.com and set up a Rails app I worked on to run there, but I needed to install the Pdf-writer Gem for proper functionality. Low and behold when I tried

sudo gem install pdf-writer

I was greeted with nothing, just

Updating Gem source index for: http://gems.rubyforge.org

and that was it. After searching and searching I realized the the version of Gem I had installed was 0.94 and that was the problem.

The solution is as follows:

wget http://rubyforge.org/frs/download.php/29548/rubygems-1.0.1.tgz
tar zxvf rubygems-1.0.1.tgz
cd rubygems-1.0.1
ruby setup.rb
sudo gem update

You just need to manually install the updated Gem. Cinch.

Drupal | Front Page Module + Nice Menus = Headache

Monday, March 3rd, 2008

I’ve spend all morning trying to get this to work.  I am using nice menus and the frontpage modules in Drupal for a project at work and I could not for the life of me get the nice menu to work on the newly created frontpage I set up using “full” type of front page.

After hours of banging my head on the desk I’ve figured it out.

    <?php
$block = module_invoke(’nice_menus’, ‘block’, ‘view’, 1);
print $block['content'];
?>

Yes that is it.  This will print out the contents of the nice menus block.  Ugh.

Weekly Roundup

Monday, January 28th, 2008

Heres the week in review:

-Linux Mint home networking is now done and working beautifully. Had some trouble getting the onboard Via Chrome9 Video working correctly, but I followed the instructions here(compiling it from source) and everything was great. I wanted to use the OpenChrome driver instead of the VESA driver because the OpenChrome one has support for XvMC which accellerates all kinds of video (i.e. Xvid, mpeg4…). Only thing left to do is get a static IP so I can get to my machine from the outside world.

-Royner – I haven’t had much time to work on ironing out any of the kinks before a first release. Hopefully this week will be different. Thinking about Slicehost to host it, as I’ve heard great things about them. I’m also going to try out deprec for deployment, using Nginx instead of Apache. Should be interesting.


Other than thats, its another work week. See ya’ll.

Rake Leaves

Thursday, January 10th, 2008

Yeah, I came up with this little Rails rake task called ‘rake leaves’. It will just create a text file that has all your tables, column names, and their data types. I’ve always thought it would be handy to have this so you don’t need to switch to MySQL and spend the time to look it up.

Anyways, if you want to download it go for it. All you need to do is put leaves.rake in /lib/tasks and then from the console

$rake leaves

You’ll get a file called table_structure_whatever_the_date_is.txt.
You can download if right here.

Rails: Using Helpers in Your Controller.

Thursday, January 10th, 2008

This morning there was a guy in the rails IRC room asking about using helpers in his controller and I recently wrote an app that used the great pdf-writer gem to generate some pdfs and used helpers in the controller for formatting in the pdf. If you are going to generate very complicated pdfs, its best not to put your pdf formatting code directly in the controller, but if you need just a real quick pdf generated, this works great.

First, create a new class in your application controller:

class Helper
include Singleton
include ActionView::Helpers::PdfHelper #or whatever helpers you want
end

I wanted to use my pdf_helpers
..and then:

def pdf_helpers
Helper.instance
end

Will create an instance of your new class.

Now, go to your pdf controller (or whatever) and simply:

pdf.text pdf_helpers.my_crazy_helper(@foo.bar)

The pdf.text part is not the important part, thats just for pdf-writer, but what is is using “pdf_helpers.your_helper” to create a new instance of the helper class which will allow you to use that helper.