Ever had to use a third party API through a gem or library in your
Rails app and wondered where the requests are going to?
Through the magic of Net HTTP Spy
(a little gem I wrote) you can spy on the majority of HTTP traffic to external
API’s taking place in your Rails application.
Installation
sudo gem install martinbtt-net-http-spy
Real World Example using Google Data API’s
You can now get access to pretty much any Google
application/content through their comprehensive set of API’s - very handy indeed.
In this example we are going to use the DocList manager sample application as seen on
the GData on Rails tutorial pages.
First grab the source:
svn co http://gdata-samples.googlecode.com/svn/trunk/doclist/DocListManager
Then create a new file initializers/api_logging.rb and add the following:
if ENV.has_key?('API_LOGGING')
gem 'martinbtt-net-http-spy'
require 'net-http-spy'
Net::HTTP.http_logger = Rails.logger
Net::HTTP.http_logger_options = {:body => true} if ENV['API_LOGGING_FULL']
end
Sample Usage and Output:
API_LOGGING=true ./script/server
Processing DoclistController#documents (for 127.0.0.1 at 2009-11-14 20:24:47) [POST]
Parameters: {"authenticity_token"=>"76e735433a9459f28a2dfb23090dec203b73e65c"}
CONNECT: ["docs.google.com", 80]
GET /feeds/default/private/full/-/document/mine
PARAMS {}
BODY: Net::HTTPOK
Rendered doclist/documentslist (7.4ms)
Completed in 1672ms (View: 11 | 200 OK [http://localhost/]
Processing DoclistController#show (for 127.0.0.1 at 2009-11-14 20:24:49) [POST]
Parameters: {"url"=>"http://docs.google.com/feeds/default/private/full/document:0BUjxlHAOrFICZGh1aGdrZ18xM2ZudGt3M2Zz", "authenticity_token"=>"76e735434a9459f08a2dfb23090dec203b73e65c"}
CONNECT: ["docs.google.com", 80]
GET /feeds/default/private/expandAcl/document:0BUjxlHAOrFICZGh1aGdrZ18xM2ZudGt3M2Zz
PARAMS {}
BODY: Net::HTTPOK
CONNECT: ["docs.google.com", 80]
GET /feeds/download/documents/Export?docID=0BUjxlHAOrFICZGh1aGdrZ18xM2ZudGt3M2Zz&exportFormat=png
PARAMS {}
BODY: Net::HTTPOK
Rendered doclist/_show (7.7ms)
Completed in 2515ms (View: 15 | 200 OK [http://localhost/doclist/show]
Note 1: By setting API_LOGGING=full the BODY: portion of the logging output will be replaced with the
actual body response from the server. In this case it is the XML used to display the documents.
Note 2: Not all API libs are built the same and many interact with Net::HTTP in ways not easy
to capture. Also be warned.. this modifies the Net::HTTP class directly so be careful if
you intend using this in production.
Summary
Net HTTP spy provides a great way to peek at what is going on under the hood of your application and find out
which http requests are being made to third parties. I’ve used this technique successfully with my team at work
to track down API issues and I’ve also had numerous reports of other developers making use of the gem for the same reasons.
I hope you find this post helpful and if you have any further questions or feedback feel free to ask in the comments
section.
Quick Reference
Continue Reading…
Posted by Martin on Nov 16, 2009
Monkeybars lets you create elegant looking cross platform desktop apps with JRuby.
You’ll want to play with the latest and greatest versions of Monkeybars without conflicting with your current gem install. Here is a little guide on how to do just that.
Download
Fire up a terminal and enter the following:
mkdir -p ~/code/gems
git clone git://gitorious.org/monkeybars/mainline.git monkeybars-mainline
cd monkeybars-mainline
rake jar
mate ~/.bash_profile
This might take a while so grab a cup of coffee while you wait. At last count the Monkeybars repo was ~90MB.
Edit your bash profile
Once your bash profile pops up in Textmate add the following to the bottom of it:
function monkeybars-edge() {
ruby ~/code/gems/monkeybars-mainline/bin/monkeybars $1 $2
}
Great, almost there. You’ll just need to reload you bash profile, so either start a new terminal or run the following
. ~/.bash_profile
Note the leading dot.
Result?
Lets see if that worked:
martin$ monkeybars-edge myapp
Creating directory myapp
Copying monkeybars project structure
Excellent.
Anything else?
The version of Monkeybars added to the classpath in src/manifest.rb is likely to be an older one. If you peek in the lib/java directory you’ll see which version you have. Update the manifest line accordingly. In the example below it was 0.6.4 and I updated to 0.6.5.
require 'resolver'
case Monkeybars::Resolver.run_location
when Monkeybars::Resolver::IN_FILE_SYSTEM
add_to_classpath '../lib/java/monkeybars-0.6.5.jar'
end
Continue Reading…
Posted by Martin on Dec 04, 2008
The presentation files from the talk I gave at RailsConf Europe are now available for download.
Direct link: Stories on a Cloud - Distributed Browser Testing with Selenium Presentation (PDF format).
I really enjoyed giving the talk and their seemed to be enough interest for follow up material. All good!
I’ll be publishing supporting links and research details very soon, so watch this space.
Posted by Martin on Sep 08, 2008
(1 comments)
A quick shout about our talk tomorrow at RailsConf Europe:

Drive your browser (or somebody else’s!) through an RSpec story. In this talk I’ll be covering Rspec stories, Selenium, message queues, packaging your apps for cross platform distribution, and more!
Read more info over at the O’Reilly Conference page
Posted by Martin on Sep 02, 2008
More news to follow shortly..

sneak peak: http://github.com/martinbtt/radiant-import-mephisto
Posted by Martin on Jul 29, 2008