Skip to content

Instantly share code, notes, and snippets.

@HusseinMorsy
Created May 12, 2009 06:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HusseinMorsy/110345 to your computer and use it in GitHub Desktop.
Save HusseinMorsy/110345 to your computer and use it in GitHub Desktop.
HTTParty examples: Delicios and Twitter
# Example how to use HTTParty
# see http://railstips.org/2008/7/29/it-s-an-httparty-and-everyone-is-invited
require 'rubygems'
require 'httparty'
require 'pp'
config = YAML::load(File.read(File.join(ENV['HOME'], '.delicious')))
class Delicious
include HTTParty
base_uri 'https://api.del.icio.us/v1'
def initialize(u, p)
@auth = {:username => u, :password => p}
end
# query params that filter the posts are:
# tag (optional). Filter by this tag.
# dt (optional). Filter by this date (CCYY-MM-DDThh:mm:ssZ).
# url (optional). Filter by this url.
# ie: posts(:query => {:tag => 'ruby'})
def posts(options={})
options.merge!({:basic_auth => @auth})
self.class.get('/posts/get', options)
end
# query params that filter the posts are:
# tag (optional). Filter by this tag.
# count (optional). Number of items to retrieve (Default:15, Maximum:100).
def recent(options={})
options.merge!({:basic_auth => @auth})
self.class.get('/posts/recent', options)
end
end
puts config['username'], config['password']
delicious = Delicious.new(config['username'], config['password'])
pp delicious.posts(:query => {:tag => 'ruby'})
pp delicious.recent
delicious.recent['posts']['post'].each { |post| puts post['href'] }
require 'rubygems'
require 'httparty'
require 'pp'
#config = YAML::load(File.read(File.join(ENV['HOME'], '.twitter')))
USERNAME = "..."
PASSWORD = "..."
class Twitter
include HTTParty
base_uri 'twitter.com'
def initialize(u, p)
@auth = {:username => u, :password => p}
end
# which can be :friends, :user or :public
# options[:query] can be things like since, since_id, count, etc.
def timeline(which=:friends, options={})
options.merge!({:basic_auth => @auth})
self.class.get("/statuses/#{which}_timeline.json", options)
end
def post(text)
options = { :query => {:status => text}, :basic_auth => @auth }
self.class.post('/statuses/update.json', options)
end
end
twitter = Twitter.new(USERNAME, PASSWORD)
pp twitter.timeline.map{|t| t['user']['name']}
# pp twitter.timeline(:friends, :query => {:since_id => 868482746})
# pp twitter.timeline(:friends, :query => 'since_id=868482746')
# pp twitter.post('this is a test of 0.2.0')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment