HTTP wrappers with ease

httparty looks really cool. It’s a little library for making writing tiny REST clients easier. From the examples (edited for length):

class Twitter
  include HTTParty
  base_uri 'twitter.com'

  def initialize(user, pass)
    self.class.basic_auth user, pass
  end

  # which can be :friends, :user or :public
  # options[:query] can be things like since, since_id, count, etc.
  def timeline(which=:friends, options={})
    self.class.get("/statuses/#{which}_timeline.xml", options)['statuses'].map { |s| s.to_struct }
  end
end

twitter = Twitter.new('bob@example.com', 'bobtime')

twitter.timeline.each do |s|
  puts s.user.name, s.text, "#{s.created_at} #{s.id}", ''
end

Great job, John!

Published by Adam Keys

Telling a joke. Typing.

One reply on “HTTP wrappers with ease”

Comments are closed.