It Just Feels Better
I've come to hate the assert_* syntax when writing tests:
should "be able to produce the correct sound" do bot = Bot.new assert_equal 'bleep', bot.bleep end
This is my preferred method because it's faster and just "feels better" (thanks, Matt):
it "should be able to produce the correct sound" do bot = Bot.new bot.bleep.should == 'bleep' end
If you like this syntax better, check out my throat-punch gem and pull it into your Ruby projects. It's just a simple way to wrap some my favorite Ruby testing tools: context, matchy, and mocha (which you should check out as well).
Sync Your Flickr Photosets With Your iPod
One of the main reasons I wrote the Fleakr gem was to pull down my latest photosets from Flickr and sync them with my iPod. To get started, create a directory that you'll use to store all your sets:
$ mkdir /tmp/flickr
Connect your iPod, open iTunes, and navigate to the "Photos" configuration tab. Select the option to sync photos from a local directory (first checkbox) and choose the directory you just created (/tmp/flickr):

To download your latest set, you can use this code. Go ahead and name this file 'flickr-download', but make sure to install the fleakr gem first:
#!/usr/bin/env ruby require 'rubygems' require 'fleakr' Fleakr.api_key = 'sekrit' user = 'the decapitator' target_dir = '/tmp/flickr' Fleakr.user(user).sets.first.save_to(target_dir, :medium)
Once this file is saved, make it executable and run it:
$ chmod +x flickr-download $ ./flickr-download
Once your script completes, you'll have the latest photoset saved to your target directory:
$ ls -F /tmp/flickr/
The Decapitator/
$ ls -1 /tmp/flickr/The\ Decapitator/
01_2117922283_715587b2cb.jpg
02_2125604584_9c09348fd6.jpg
03_2118696542_8af5763bde.jpg
04_2146369495_e266cf3c18.jpg
05_2146384313_a8dbfb589c.jpg
...
You'll notice that each file is prefixed with a series of digits - this is used to preserve the original order of the photos in the set. Otherwise your photos would display in a seemingly random order when played as a slideshow on your iPod.
That's it! Your photos will now be available on your iPod after the next synchronization. If you want to retrieve other sizes, you can specify that size in place of where I put :medium above. See the documentation for all available sizes.
You can run this script each time you add a new set to your Flickr photostream, or even schedule it to be run periodically using cron or a tool like Lingon.
Violent School

If you can make it through the undergrad program at U of Phoenix, you can tackle anything!
Using Fleakr to Log API Calls
I just added a new feature to the fleakr gem that gives you the ability to log all calls to the API. To get started just point it to a logger:
Fleakr.logger = Logger.new('/tmp/fleakr.log')
This is great when just using the gem, but it's more useful when you're using it as part of your Rails app:
# config/initializers/fleakr.rb Fleakr.logger = RAILS_DEFAULT_LOGGER
By default, this logs all traffic (including file data if you're doing uploads), so you may want to turn down logging in development to see just the requests:
# config/environments/development.rb config.log_level = :info
If you missed it, check out my recent post to get started using Fleakr in your Rails app.
The Power of The Tubes
An interesting course of events (to me, at least):
- Justin gets an idea about a gem he wants to write, and writes it
- Blogs about what he's created, why, and how to use it
- The company who created the API recognizes this and gives props
This is something that's becoming more of the norm these days as companies (and people) monitor their brands and what others are saying about them. For me, it causes me to pause and reconsider when I think that someone won't care about what I have to say - you never know who will find value in your words or knowledge.
Connect Your Rails App to Flickr With The Fleakr Gem
Fleakr started out as a simple way for me to download photos through the console (something it does quite well), but it is also really easy to use inside your Rails application.
Let's build a sample app that displays the latest photos for a specific Flickr user. First, install the gem and create your Rails app:
$ sudo gem install fleakr $ rails fleakr-demo
Configure the gem inside your application:
# config/environment.rb config.gem 'fleakr'
Since we're doing read-only calls, we only need an API key - let's set that in an initializer:
# config/initializers/fleakr.rb Fleakr.api_key = 'your_api_key_here'
Now we'll create a simple action that will accept a Flickr user as a parameter and pull back the recent photos:
$ script/generate controller users show
Then, add a route (and remember to delete public/index.html):
# config/routes.rb map.user ':username', :controller => 'users', :action => 'show'
Add a line to retrieve the specified user:
class UsersController < ApplicationController def show @user = Fleakr.user(params[:username]) end end
Finally, create some simple view code to pull back the latest photos (limited to 100) in groups of 10
<% # app/views/users/show.html.erb %>
<% @user.photos.each_slice(10) do |photos| %>
<div>
<% photos.each do |photo| %>
<%= link_to image_tag(photo.square.url),
photo.url,
:width => photo.square.width,
:height => photo.square.height %>
<% end %>
</div>
<% end %>
You're all set - start up your server and point your browser to a URL containing a Flickr username (e.g. http://localhost:3000/teamviget).
Warning: This is extremely inefficent and is designed only to show you what's possible and shouldn't be used in a production application. The rendering of this view can take up to a minute because it has to do N + 1 API calls (where N is the number of recent photos, capped at 100).
Multi-line Memoization in Ruby
During a recent code review, David pointed out a quick tip to do multi-line memoization. It's a cool technique that I've used in my own code:
class Search def results @results ||= begin response = Fleakr::Api::MethodRequest.with_response!('photos.search', parameters) (response.body/'rsp/photos/photo').map {|p| Photo.new(p) } end end end
Check out the post on the Viget Extend blog for more details.
Fun With Flickr Contacts
One of the fun things about the API I created with the Fleakr gem is that many of the chained associations available were provided "for free." For example, you can find the contacts for a user:
Fleakr.api_key = 'sekrit' user = Fleakr.user('teamviget') puts user.contacts.map(&:username) # => ["benscofield", "Brian Landau", "Brian Williams", "carolynhack", ... # "ryanmoede", "Samanthatoy", "stephay22", "The Mindinator", "whafro"] puts user.contacts.length # => 21 puts user.contacts.first.name # => "Ben Scofield" puts user.contacts.last.name # => "M. Jackson Wilkinson" puts user.contacts.last.location # => "Washington, DC, USA"
Not really that earth-shattering. But since a "contact" is really an instance of the User class, I can chain the calls to do some strange and useless things:
puts user.contacts.last.contacts.length # => 70 puts user.contacts.last.contacts.first.sets.last.photos.first.url # => "http://www.flickr.com/photos/aarongustafson/55410766/"
There are other attributes available for your contacts as well:
# Attributes available from the API puts user.class.attributes.map(&:name) # => [:id, :username, :name, :location, :photos_url, :profile_url, # :photos_count, :icon_server, :icon_farm, :pro, :admin] # Additional attributes puts user.pro? # => true puts user.admin? # => false puts user.icon_url # => "http://farm2.static.flickr.com/1018/buddyicons/11166619@N03.jpg"
And associations:
puts user.photos.map(&:title)[0..1] # => ["VigeTurf Loves Boston!", "Turf in Central Cali"] puts user.groups.map(&:name)[0..1] # => ["Refresh DC", "Happy at work"] puts user.sets.map(&:title)[0..1] # => ["Viget South Holiday Dinner 2008", "VigeTurf Shots"]
Use your powers for good, and beware the random strange images.


