Created
May 12, 2009 06:23
-
-
Save HusseinMorsy/110345 to your computer and use it in GitHub Desktop.
HTTParty examples: Delicios and Twitter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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'] } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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