Skip to content

Commit

Permalink
Merge branch 'faraday'
Browse files Browse the repository at this point in the history
Conflicts:
	Gemfile.lock
	twitter.gemspec
  • Loading branch information
sferik committed Sep 28, 2010
2 parents 7b4a51d + 8f4c5ff commit 80aff88
Show file tree
Hide file tree
Showing 20 changed files with 517 additions and 511 deletions.
26 changes: 19 additions & 7 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,49 @@ PATH
remote: .
specs:
twitter (0.9.12)
addressable (~> 2.2.1)
faraday (~> 0.4.6)
faraday_middleware (~> 0.0.5)
hashie (~> 0.4.0)
httparty (~> 0.6.1)
multi_json (~> 0.0.4)
oauth (~> 0.4.3)

GEM
remote: http://rubygems.org/
specs:
crack (0.1.8)
addressable (2.2.1)
fakeweb (1.3.0)
faraday (0.4.6)
addressable (>= 2.1.1)
rack (>= 1.0.1)
faraday_middleware (0.0.5)
faraday (~> 0.4.5)
hashie (~> 0.4.0)
multi_json (~> 0.0.4)
oauth2 (~> 0.0.13)
hashie (0.4.0)
httparty (0.6.1)
crack (= 0.1.8)
json (1.4.6)
mocha (0.9.8)
rake
multi_json (0.0.4)
oauth (0.4.3)
oauth2 (0.0.13)
faraday (~> 0.4.1)
multi_json (>= 0.0.4)
rack (1.2.1)
rake (0.8.7)
shoulda (2.11.3)

PLATFORMS
ruby

DEPENDENCIES
addressable (~> 2.2.1)
fakeweb (~> 1.3.0)
faraday (~> 0.4.6)
faraday_middleware (~> 0.0.5)
hashie (~> 0.4.0)
httparty (~> 0.6.1)
json (~> 1.4.6)
mocha (~> 0.9.8)
multi_json (~> 0.0.4)
oauth (~> 0.4.3)
shoulda (~> 2.11.3)
twitter!
6 changes: 3 additions & 3 deletions examples/search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
search.each { |result| pp result }

puts '*'*50, 'Parameter Check', '*'*50
pp Twitter::Search.new('#austineats').fetch().results.first
pp Twitter::Search.new('#austineats').page(2).fetch().results.first
pp Twitter::Search.new('#austineats').since(1412737343).fetch().results.first
pp Twitter::Search.new('#austineats').fetch.results.first
pp Twitter::Search.new('#austineats').page(2).fetch.results.first
pp Twitter::Search.new('#austineats').since(1412737343).fetch.results.first
27 changes: 27 additions & 0 deletions lib/faraday/raise_errors.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module Faraday
class Response::RaiseErrors < Response::Middleware
def self.register_on_complete(env)
env[:response].on_complete do |response|
case response[:status].to_i
when 400
raise Twitter::RateLimitExceeded, "(#{response[:status]}): #{response[:body]}"
when 401
raise Twitter::Unauthorized, "(#{response[:status]}): #{response[:body]}"
when 403
raise Twitter::General, "(#{response[:status]}): #{response[:body]}"
when 404
raise Twitter::NotFound, "(#{response[:status]}): #{response[:body]}"
when 500
raise Twitter::InformTwitter, "Twitter had an internal error. Please let them know in the group. (#{response[:status]}): #{response[:body]}"
when 502..503
raise Twitter::Unavailable, "(#{response[:status]}): #{response[:body]}"
end
end
end

def initialize(app)
super
@parser = nil
end
end
end
167 changes: 34 additions & 133 deletions lib/twitter.rb
Original file line number Diff line number Diff line change
@@ -1,28 +1,19 @@
require "forwardable"
require "oauth"
require "hashie"
require "httparty"
require "multi_json"
require 'addressable/uri'
require 'faraday'
require 'faraday_middleware'
require 'forwardable'
require 'hashie'
require 'oauth'

module Twitter
include HTTParty

class TwitterError < StandardError
attr_reader :data

def initialize(data)
@data = data
super
end
def self.adapter
@adapter ||= Faraday.default_adapter
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.adapter=(value)
@adapter = value
end

def self.user_agent
@user_agent ||= 'Ruby Twitter Gem'
Expand All @@ -33,11 +24,13 @@ def self.user_agent=(value)
end

def self.api_endpoint
@api_endpoint ||= "api.twitter.com/#{self.api_version}"
api_endpoint = "api.twitter.com/#{Twitter.api_version}"
api_endpoint = Addressable::URI.heuristic_parse(api_endpoint).to_s
@api_endpoint ||= api_endpoint
end

def self.api_endpoint=(value)
@api_endpoint = value
@api_endpoint = Addressable::URI.heuristic_parse(value).to_s
end

def self.api_version
Expand All @@ -48,122 +41,30 @@ def self.api_version=(value)
@api_version = value
end

def self.firehose(options = {})
perform_get("/statuses/public_timeline.json")
end

def self.user(id, options={})
perform_get("/users/show/#{id}.json")
end

def self.status(id, options={})
perform_get("/statuses/show/#{id}.json")
end

def self.friend_ids(id, options={})
perform_get("/friends/ids/#{id}.json")
end

def self.follower_ids(id, options={})
perform_get("/followers/ids/#{id}.json")
end

def self.timeline(id, options={})
perform_get("/statuses/user_timeline/#{id}.json", :query => options)
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_screen_name, slug, query = {})
perform_get("/#{list_owner_screen_name}/lists/#{slug}/statuses.json", :query => query)
end

private

def self.perform_get(uri, options = {})
base_uri self.api_endpoint
make_friendly(get(uri, options))
end

def self.make_friendly(response)
raise_errors(response)
data = parse(response)
# Don't mash arrays of integers
if data && data.is_a?(Array) && data.first.is_a?(Integer)
data
else
mash(data)
end
end

def self.raise_errors(response)
case response.code.to_i
when 400
data = parse(response)
raise RateLimitExceeded.new(data), "(#{response.code}): #{response.message} - #{data['error'] if data}"
when 401
data = parse(response)
raise Unauthorized.new(data), "(#{response.code}): #{response.message} - #{data['error'] if data}"
when 403
data = parse(response)
raise General.new(data), "(#{response.code}): #{response.message} - #{data['error'] if data}"
when 404
raise NotFound, "(#{response.code}): #{response.message}"
when 500
raise InformTwitter, "Twitter had an internal error. Please let them know in the group. (#{response.code}): #{response.message}"
when 502..503
raise Unavailable, "(#{response.code}): #{response.message}"
end
end

def self.parse(response)
case response.body
when ''
nil
when 'true'
true
when 'false'
false
else
MultiJson.decode(response.body)
end
end
class TwitterError < StandardError
attr_reader :data

def self.mash(obj)
if obj.is_a?(Array)
obj.map{|item| Hashie::Mash.new(item)}
elsif obj.is_a?(Hash)
Hashie::Mash.new(obj)
else
obj
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
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", "request")
require File.join(directory, "twitter", "base")
require File.join(directory, "twitter", "search")
require File.join(directory, "twitter", "trends")
require File.join(directory, "twitter", "geo")
require File.expand_path("../faraday/raise_errors", __FILE__)
require File.expand_path("../twitter/oauth", __FILE__)
require File.expand_path("../twitter/request", __FILE__)
require File.expand_path("../twitter/base", __FILE__)
require File.expand_path("../twitter/search", __FILE__)
require File.expand_path("../twitter/trends", __FILE__)
require File.expand_path("../twitter/geo", __FILE__)
require File.expand_path("../twitter/unauthenticated", __FILE__)
Loading

0 comments on commit 80aff88

Please sign in to comment.