lib/chatterbot/streaming.rb in chatterbot-1.0.2 vs lib/chatterbot/streaming.rb in chatterbot-2.0.0.pre
- old
+ new
@@ -2,66 +2,59 @@
#
# simple twitter stream handler
module Streaming
- def authenticated_user
- @user ||= client.user
+ def streaming_tweet_handler
+ usable_handlers = [:home_timeline, :search]
+ name, block = @handlers.find { |k, v| usable_handlers.include?(k) }
+ block
end
- # Streams messages for a single user, optionally including an
- # additional search/etc
#
- # @param opts [Hash] options
- # @option options [String] :with Specifies whether to return information for just the users specified in the follow parameter, or include messages from accounts they follow.
- # @option options [String] :replies Specifies whether to return additional @replies.
- # @option options [String] :stall_warnings Specifies whether stall warnings should be delivered.
- # @option options [String] :track Includes additional Tweets matching the specified keywords. Phrases of keywords are specified by a comma-separated list.
- # @option options [String] :locations Includes additional Tweets falling within the specified bounding boxes.
- # @yield [Twitter::Tweet, Twitter::Streaming:
- def do_streaming(streamer)
- debug "streaming twitter client"
- streaming_client.send(streamer.endpoint, streamer.connection_params) do |object|
- handle_streaming_object(object, streamer)
- end
- end
-
- def handle_streaming_object(object, streamer)
+ # Take the passed in object and call the appropriate bot method
+ # for it
+ # @param [Class] object a streaming API object
+ #
+ def handle_streaming_object(object)
debug object
case object
when Twitter::Tweet
if object.user == authenticated_user
debug "skipping #{object} because it's from me"
- elsif streamer.tweet_handler && !on_blacklist?(object) && !skip_me?(object)
+ elsif (h = streaming_tweet_handler) && valid_tweet?(object)
@current_tweet = object
- streamer.tweet_handler.call object
+ update_since_id(object)
+
+ h.call(object)
@current_tweet = nil
end
when Twitter::Streaming::DeletedTweet
- if streamer.delete_handler
- streamer.delete_handler.call(object)
+ if (h = @handlers[:deleted])
+ h.call(object)
end
when Twitter::DirectMessage
- if streamer.dm_handler # && !on_blacklist?(object) && !skip_me?(object)
+ if object.sender == authenticated_user
+ debug "skipping DM #{object} because it's from me"
+ elsif (h = @handlers[:direct_messages])
@current_tweet = object
- streamer.dm_handler.call object
+ update_since_id_dm(object)
+ h.call(object)
@current_tweet = nil
end
when Twitter::Streaming::Event
if object.respond_to?(:source) && object.source == authenticated_user
debug "skipping #{object} because it's from me"
- elsif object.name == :follow && streamer.follow_handler
- streamer.follow_handler.call(object.source)
- elsif object.name == :favorite && streamer.favorite_handler
- streamer.favorite_handler.call(object.source, object.target_object)
+ elsif object.name == :follow && (h = @handlers[:followed])
+ h.call(object.source)
+ elsif object.name == :favorite && (h = @handlers[:favorited])
+ h.call(object.source, object.target_object)
end
when Twitter::Streaming::FriendList
debug "got friend list"
- if streamer.friends_handler
- streamer.friends_handler.call(object)
- end
end
end
- end
+
+ end
end