For a project that I'm working on, I want to download a set of pictures from flickr once a day. In this way, my Rails app won't need to access the flickr api - it can just grab the pics out of an images directory.

I figured that it would be an awfully easy task to download pictures from the internet using Ruby, and it is, but it was very difficult to find good examples. I googled for an hour or so, and kept coming up empty-handed. Finally, I checked in gmail where I keep mail from the Ruby Language mailing list. I found a relatively straightfoward way to do this.

Let's say that you want to download a picture from flickr, and you know its URL. Here's the ruby script to download the file:

1
2
3
4
5
6
7
8
9
10

require 'net/http'

Net::HTTP.start("static.flickr.com") { |http|
  resp = http.get("/92/218926700_ecedc5fef7_o.jpg")
  open("fun.jpg", "wb") { |file|
    file.write(resp.body)
   }
}
puts "Yay!!"

The Net::HTTP class contains the magic needed to handle this operation. I don't think this will work at all if you need to pull down a file from an FTP server. For now, we're dealing with http urls. So, just strip off the "http://" portion of the url and everything after that up to the first / goes into the start method.

Now, we get the file. The rest of your image url after what you put in the start method goes into the get method. This grabs the file from flickr.

Now we're going to copy the file down to where the script is running. First, let's create the file we're going to copy the picture into. Using the open method, the first parameter is the name of the file that you're going to plop the picture into. We could have used "218926700_ecedc5fef7_o.jpg" or anything else here. The second parameter of the open method, "wb" indicates that we're opening the file for (w)riting and we're going to be writing (b)inary information. The "b" may not be necessary on non-Windows platforms.

Finally, we're going to write into the new file, the contents or "body" of what we grabbed from flickr. So, this writes in the binary bits of the picture into fun.jpg. Remember that with the way that we created fun.jpg it'll be in the same directory with our ruby script.

This same method will copy down .html files, .css files, .pdf's and just about any other kind of file. In my next RubyNoob entry, I'll write about how to combine this method with a flickr api call to grab an arbitrary number of recent photos from flickr. As usual, I'm still a noob, and there's probably much better ways to do this. If you know a better way, please share in the comment section below.
As I'm reading through Head First Design Patterns, I've decided to write some sample ruby code for each pattern. This will certainly be educational for me, and it might prove useful for some of my readers. As always, I am not an expert in ruby by any means, so if I flub up my code or if there's a better way to do something, please leave a comment and illuminate me. So, here we go!

Read the rest of this entry
One of the first projects that I have planned for Ruby on Rails involves calling the del.icio.us API and formatting results tailored to each user's desires. Actually, Lindsay thought the whole idea up, and it probably won't make much sense until we debut it "sometime soon."

My first task was just figuring out how to call one of the APIs through Ruby. After a lot of detective work, I figured out a first, very rough go at it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

require 'open-uri'
require "rexml/document"
include REXML
user = "your_login"
pass = "your_password"
url='http://del.icio.us/api/posts/all?&tag='
puts 'Enter Tag: '
tag=gets
print url+tag
open(url+tag,
  :http_basic_authentication =>
  [user, pass]
  ) do |f|
  result = f.read
  doc = Document.new result
  doc.elements.each("posts/post") {|element| puts element.attributes["href"] }
  end


Read the rest of this entry

Another Excellent Cheatsheet

April 17th, 2006

Cheatsheet from Cenophobie

Ruby Recipes

April 17th, 2006

Here's a great resource I just found: PLEAC-Ruby It's chock full of tons of useful examples of how to do things with Ruby. No rails stuff here, but extremely useful nevertheless!
The wedding last night was great fun! Thanks Sally!!! Now back to our previously scheduled rubynoob article. This busy weekend my goal was to figure out why the Amazon toolbar wouldn't work for me. Here's what the sidebar looks like:
Amazon sidebar component
My problem, though, is that it doesn't quite work as expected. I tried linking Amazon books in several different ways with no success. I just couldn't figure out how to make it work. So, I figured that investigating the component would be a great way to learn a little bit more about Rails, and I was right! Long story short - in order to get typo to create sidebar links to Amazon items, you need to choose a text filter in the blog settings page which uses the Amazon post-processing filter. None of the default text filters uses the Amazon post-processing filter, so you'll either need to create a new one or edit an existing one and make sure to include the it as well as inserting your Amazon associate's id. I couldn't find any documentation anywhere that spells this out, but the rest of this article explains how I figured this out. Read the rest of this entry
whys guide
One thing I've found out that's important to do before jumping headfirst into Ruby on Rails is familiarizing yourself with the Ruby language. I started going through some tutorials back when I first got excited about the whole idea surrounding RoR. While the demos were awesome, I didn't really understand what the hell I was doing. Ruby the language looks so much different than c# or java or any of the other languages I've used. So, let's look at my Learning Ruby resources.

I'm currently using mainly just two resources. The first has the odd name of Why's Poignant Guide to Ruby,it's completely free and it is certainly one of the strangest "books" I've ever read, technical or not. And that's saying quite a lot because I've read some pretty odd stuff in my time. However, it has hands down the best explanation of Ruby's blocks and iterators that I've found. This aspect of Ruby seems to be one of it's most powerful features, but it's been pretty difficult for me to really wrap my brain around. Why does a pretty good job of explaining it though. The book is really written with totally new coders in mind, but I've managed to distill quite a bit of good information from it. Try it out - just don't expect to read the entire thing in one setting - that might cause hallucinations or something!

My other favorite resource for learning Ruby is much more traditional, though probably a lot more complete. I really enjoy the Pragmatic Programmer's Guide to Learning Ruby. This reads much more like a normal book and is also full of great content. I suggest checking out the Pragmatic Programmers website and considering purchasing a PDF version of the book. I have a two monitor setup on my desk and I find that keeping the PDF open while working through the examples works much better than trying to type in the code while holding the book open on my lap. The PDF contains links to the source code on the web and is just generally more convenient. The book, though, is much easier to read away from my computer.

It's going to take me quite a while to master this language -there's a lot of new stuff, or at least new ways of doing old stuff. The nice thing is that Ruby is just a lot of fun to learn!