Delicious API with path-to/described_routes

The latest path-to contains an example (based on HTTParty’s) that queries Delicious via a Ruby API that’s 100% metadata driven.  Stripped of comments (the original commented version is here), the code is tiny:

require 'path-to/described_routes'
require 'pp'

config = YAML::load(File.read(File.join(ENV['HOME'], '.delicious')))

delicious = PathTo::DescribedRoutes::Application.new(
              :yaml => File.read(File.join(
                                     File.dirname(__FILE__),
                                     'delicious.yaml')),
              :http_options => {
                  :basic_auth => {
                      :username => config['username'],
                      :password => config['password']}})

pp delicious.posts['tag' => 'ruby'].get
pp delicious.posts['tag' => 'ruby'].recent['count' => '5'].get
delicious.recent_posts.get['posts']['post'].each do |post|
  puts post['href']
end

A couple of things to highlight are:

  1. The methods posts, recent, recent_posts are available thanks to the metadata - they’re not explicitly coded anywhere.  Similarly, URIs aren’t coded here either.
  2. The resource hiercharchy.  These are all separately identifiable things:
    • posts
    • posts['tag' => 'ruby']
    • posts['tag' => 'ruby'].recent
    • posts['tag' => 'ruby'].recent['count' => '5']

The metadata takes the form of described_routes-style resource templates, and I did it by hand this time in YAML:

---
- name: posts
  options:
  - GET
  uri_template: https://api.del.icio.us/v1/posts/get?{-join|&|tag,dt,url}
  optional_parameters:
  - tag
  - dt
  - url
  resource_templates:
  - name: recent_posts
    options:
    - GET
    uri_template: https://api.del.icio.us/v1/posts/recent?{-join|&|tag,count}
    rel: recent
    optional_parameters:
    - tag
    - count

Tags: , , , ,

One Response to “Delicious API with path-to/described_routes”

Leave a Reply