/examples/twitter.rb

http://github.com/jnunemaker/httparty · Ruby · 31 lines · 21 code · 5 blank · 5 comment · 0 complexity · 7e5eb45894a2e946afc5eaf31258b535 MD5 · raw file

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