PageRenderTime 24ms CodeModel.GetById 6ms app.highlight 11ms RepoModel.GetById 3ms app.codeStats 0ms

/examples/twitter.rb

http://github.com/jnunemaker/httparty
Ruby | 31 lines | 21 code | 5 blank | 5 comment | 0 complexity | 7e5eb45894a2e946afc5eaf31258b535 MD5 | raw file
 1dir = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
 2require File.join(dir, 'httparty')
 3require 'pp'
 4config = YAML.load(File.read(File.join(ENV['HOME'], '.twitter')))
 5
 6class Twitter
 7  include HTTParty
 8  base_uri 'twitter.com'
 9
10  def initialize(u, p)
11    @auth = {username: u, password: p}
12  end
13
14  # which can be :friends, :user or :public
15  # options[:query] can be things like since, since_id, count, etc.
16  def timeline(which = :friends, options = {})
17    options.merge!({ basic_auth: @auth })
18    self.class.get("/statuses/#{which}_timeline.json", options)
19  end
20
21  def post(text)
22    options = { query: { status: text }, basic_auth: @auth }
23    self.class.post('/statuses/update.json', options)
24  end
25end
26
27twitter = Twitter.new(config['email'], config['password'])
28pp twitter.timeline
29# pp twitter.timeline(:friends, query: {since_id: 868482746})
30# pp twitter.timeline(:friends, query: 'since_id=868482746')
31# pp twitter.post('this is a test of 0.2.0')