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:
- The methods
posts,recent,recent_postsare available thanks to the metadata - they’re not explicitly coded anywhere. Similarly, URIs aren’t coded here either. - The resource hiercharchy. These are all separately identifiable things:
postsposts['tag' => 'ruby']posts['tag' => 'ruby'].recentposts['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: described_routes, httparty, path-to, rest, ruby
[...] In the Delicious API example, [...]