Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 92 lines (73 sloc) 2.641 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
require 'forwardable'
require 'rubygems'

gem 'oauth', '~> 0.3.6'
require 'oauth'

gem 'hashie', '~> 0.1.3'
require 'hashie'

gem 'httparty', '>= 0.5.2'
require 'httparty'

module Twitter
  class TwitterError < StandardError
    attr_reader :data

    def initialize(data)
      @data = data
      super
    end
  end

  class RateLimitExceeded < TwitterError; end
  class Unauthorized < TwitterError; end
  class General < TwitterError; end

  class Unavailable < StandardError; end
  class InformTwitter < StandardError; end
  class NotFound < StandardError; end

  def self.firehose
    response = HTTParty.get('http://api.twitter.com/statuses/public_timeline.json', :format => :json)
    response.map { |tweet| Hashie::Mash.new(tweet) }
  end

  def self.user(id)
    response = HTTParty.get("http://api.twitter.com/users/show/#{id}.json", :format => :json)
    Hashie::Mash.new(response)
  end

  def self.status(id)
    response = HTTParty.get("http://api.twitter.com/statuses/show/#{id}.json", :format => :json)
    Hashie::Mash.new(response)
  end

  def self.friend_ids(id)
    HTTParty.get("http://api.twitter.com/friends/ids/#{id}.json", :format => :json)
  end

  def self.follower_ids(id)
    HTTParty.get("http://api.twitter.com/followers/ids/#{id}.json", :format => :json)
  end
  
  def self.timeline(id, options={})
    response = HTTParty.get("http://api.twitter.com/statuses/user_timeline/#{id}.json", :query => options, :format => :json)
    response.map{|tweet| Hashie::Mash.new tweet}
  end

  # :per_page = max number of statues to get at once
  # :page = which page of tweets you wish to get
  def self.list_timeline(list_owner_username, slug, query = {})
    response = HTTParty.get("http://api.twitter.com/1/#{list_owner_username}/lists/#{slug}/statuses.json", :query => query, :format => :json)
    response.map{|tweet| Hashie::Mash.new tweet}
  end
end

module Hashie
  class Mash
  
    # Converts all of the keys to strings, optionally formatting key name
    def rubyify_keys!
      keys.each{|k|
        v = delete(k)
        new_key = k.to_s.underscore
        self[new_key] = v
        v.rubyify_keys! if v.is_a?(Hash)
        v.each{|p| p.rubyify_keys! if p.is_a?(Hash)} if v.is_a?(Array)
      }
      self
    end
  
  end
end

directory = File.expand_path(File.dirname(__FILE__))

require File.join(directory, 'twitter', 'oauth')
require File.join(directory, 'twitter', 'httpauth')
require File.join(directory, 'twitter', 'request')
require File.join(directory, 'twitter', 'base')
require File.join(directory, 'twitter', 'search')
require File.join(directory, 'twitter', 'trends')
Something went wrong with that request. Please try again.