lib/bird_grinder/tweeter.rb in birdgrinder-0.1.3.1 vs lib/bird_grinder/tweeter.rb in birdgrinder-0.1.4.0
- old
+ new
@@ -18,26 +18,26 @@
class Tweeter
is :loggable, :delegateable
require 'bird_grinder/tweeter/streaming'
require 'bird_grinder/tweeter/search'
+ require 'bird_grinder/tweeter/abstract_authorization'
+ require 'bird_grinder/tweeter/basic_authorization'
+ require 'bird_grinder/tweeter/oauth_authorization'
VALID_FETCHES = [:direct_messages, :mentions]
cattr_accessor :api_base_url
self.api_base_url = "http://twitter.com/"
-
- attr_reader :auth_credentials
# Initializes the tweeter with a given delegate. It will use
# username and password from your settings file for authorization
# with twitter.
#
# @param [Delegate] delegate the delegate class
def initialize(delegate)
check_auth!
- @auth_credentials = [BirdGrinder::Settings.username, BirdGrinder::Settings.password]
delegate_to delegate
end
# Automates fetching mentions / direct messages at the same time.
#
@@ -56,11 +56,11 @@
# @param [Hash] opts extra options to pass in the query string
def follow(user, opts = {})
user = user.to_s.strip
logger.info "Following '#{user}'"
post("friendships/create.json", opts.merge(:screen_name => user)) do
- delegate.receive_message(:outgoing_follow, {:user => user}.to_nash)
+ delegate.receive_message(:outgoing_follow, N(:user => user))
end
end
# Tells the twitter api to unfollow a specific user
#
@@ -68,11 +68,11 @@
# @param [Hash] opts extra options to pass in the query string
def unfollow(user, opts = {})
user = user.to_s.strip
logger.info "Unfollowing '#{user}'"
post("friendships/destroy.json", opts.merge(:screen_name => user)) do
- delegate.receive_message(:outgoing_unfollow, {:user => user}.to_nash)
+ delegate.receive_message(:outgoing_unfollow, N(:user => user))
end
end
# Updates your current status on twitter with a specific message
#
@@ -93,12 +93,12 @@
# @param [Hash] opts extra options to pass in the query string
def dm(user, text, opts = {})
text = text.to_s.strip
user = user.to_s.strip
logger.debug "DM'ing #{user}: #{text}"
- post("direct_messages/new.json", opts.merge(:user => user, :text => text)) do
- delegate.receive_message(:outgoing_direct_message, {:user => user, :text => text}.to_nash)
+ post("direct_messages/new.json", opts.merge(:user => user, :text => text)) do |json|
+ delegate.receive_message(:outgoing_direct_message, status_to_args(json, :direct_message))
end
end
# Returns an instance of BirdGrinder::Tweeter::Streaming,
# used for accessing the alpha streaming api for twitter.
@@ -180,17 +180,22 @@
results.all = (res.previous_cursor == 0 && res.next_cursor == 0)
delegate.receive_message(:incoming_follower_ids, results)
end
else
logger.info "Getting all followers for #{id}"
- get_followers(id, opts.merge(:cursor => -1), {
+ get_followers(id, opts.merge(:cursor => -1), N({
:user_id => id,
:all => true,
:ids => []
- }.to_nash)
+ }))
end
end
+
+ # @todo Use correct authorization method
+ def authorization_method
+ @authorization_method ||= (OAuthAuthorization.enabled? ? OAuthAuthorization : BasicAuthorization).new
+ end
protected
def get_followers(id, opts, nash)
get_followers_page(id, opts) do |res|
@@ -212,28 +217,26 @@
def request(path = "/")
EventMachine::HttpRequest.new(api_base_url / path)
end
def get(path, params = {}, &blk)
- http = request(path).get({
- :head => {'Authorization' => @auth_credentials},
- :query => params.stringify_keys
- })
+ req = request(path)
+ http = req.get(:query => params)
+ authorization_method.add_header_to(http)
add_response_callback(http, blk)
http
end
def post(path, params = {}, &blk)
real_params = {}
params.each_pair { |k,v| real_params[CGI.escape(k.to_s)] = CGI.escape(v) }
- http = request(path).post({
- :head => {
- 'Authorization' => @auth_credentials,
- 'Content-Type' => 'application/x-www-form-urlencoded'
- },
+ req = request(path)
+ http = req.post({
+ :head => {'Content-Type' => 'application/x-www-form-urlencoded'},
:body => real_params
})
+ authorization_method.add_header_to(http)
add_response_callback(http, blk)
http
end
def add_response_callback(http, blk)
@@ -274,12 +277,11 @@
results.type = type
results
end
def check_auth!
- if BirdGrinder::Settings["username"].blank? || BirdGrinder::Settings["username"].blank?
- raise BirdGrinder::MissingAuthDetails, "Missing twitter username or password."
- end
+ return if BirdGrinder::Settings.username? && BirdGrinder::Settings.password?
+ raise BirdGrinder::MissingAuthDetails, "Missing twitter username or password."
end
end
end
\ No newline at end of file