→ URLs in Ruby

14 December 2009

Last year I showed you how to turn Ruby into Java.

This year I want to show you how to write URLs in Ruby. Let's have them return their body, shall we?

For example:

>> http://github.com/defunkt.json
=> #<URLDSL::Request http://github.com/defunkt.json/>
>> puts http://github.com/defunkt.json
[{"repository":{"url":"http://github.com/defunkt/repl",
... etc ...
=> nil

Yes, amazing.

>> puts http://github.com/api/v2/yaml/repos/show/schacon/grit
---
repository:
  :url: http://github.com/schacon/grit
  :description: Grit is a Ruby library for extracting information
                from a git repository in an object oriented manner
                - this fork tries to intergrate as much pure-ruby
                functionality as possible
  :homepage: http://grit.rubyforge.org/
  :fork: true
  :open_issues: 0
  :private: false
  :name: grit
  :owner: schacon
  :watchers: 81
  :forks: 11
=> nil

Obligatory Twitter URL:

>> require 'json'
=> true
>> url = http://twitter.com/statuses/show/6592721580.json
=> #<URLDSL::Request http://twitter.com/statuses/show/6592721580.json/>
>> JSON.parse(url.to_s)['text']
=> "He nose the truth."

That's about as far as we can take it, but that's far enough.

The code: gist 255948

Enjoy.