Class: Userstream
- Inherits:
-
Object
- Object
- Userstream
- Includes:
- Connect
- Defined in:
- lib/userstream.rb,
lib/userstream/user.rb,
lib/userstream/event.rb,
lib/userstream/tweet.rb,
lib/userstream/friend.rb,
lib/userstream/connect.rb,
lib/userstream/event/delete.rb,
lib/userstream/event/follow.rb,
lib/userstream/event/retweet.rb,
lib/userstream/event/favorite.rb,
lib/userstream/event/unfavorite.rb,
lib/userstream/event/direct_message.rb
Defined Under Namespace
Modules: Connect Classes: Delete, DirectMessage, Event, Favorite, Follow, Friend, Handlers, Retweet, Tweet, Unfavorite, User
Constant Summary collapse
- EventHandlerTypes =
[:friend, :tweet, :follow, :unfollow, :favorite, :unfavorite, :retweet, :delete, :direct_message, :block, :unblock, :list_member_removed, :list_member_added, :list_user_subscribed, :list_user_unsubscribed]
- ConnectionHandlerTypes =
[:disconnect, :connect]
- HandlerTypes =
ConnectionHandlerTypes + EventHandlerTypes
Instance Attribute Summary collapse
-
#consumer_secret ⇒ Object
readonly
Returns the value of attribute consumer_secret.
-
#consumer_token ⇒ Object
readonly
Returns the value of attribute consumer_token.
-
#fill_in ⇒ Object
readonly
Returns the value of attribute fill_in.
-
#handlers ⇒ Object
readonly
Returns the value of attribute handlers.
Instance Method Summary collapse
- #connect(*users) ⇒ Object
- #connect_single(user) ⇒ Object
- #dispatch_connect(user) ⇒ Object
- #dispatch_disconnect(user) ⇒ Object
- #dispatch_friend(user, data) ⇒ Object
-
#initialize(options = {}) ⇒ Userstream
constructor
A new instance of Userstream.
Methods included from Connect
Constructor Details
#initialize(options = {}) ⇒ Userstream
Returns a new instance of Userstream.
40 41 42 43 44 45 46 47 48 |
# File 'lib/userstream.rb', line 40 def initialize( = {}) @consumer_token = [:consumer_token] @consumer_secret = [:consumer_secret] @fill_in = [:fill_in] @connect_url = "https://userstream.twitter.com/2/user.json" @handlers = Handlers.new(*HandlerTypes.map{|h| []}) @on_connect_called = {} @users = [] end |
Instance Attribute Details
#consumer_secret ⇒ Object (readonly)
Returns the value of attribute consumer_secret.
38 39 40 |
# File 'lib/userstream.rb', line 38 def consumer_secret @consumer_secret end |
#consumer_token ⇒ Object (readonly)
Returns the value of attribute consumer_token.
38 39 40 |
# File 'lib/userstream.rb', line 38 def consumer_token @consumer_token end |
#fill_in ⇒ Object (readonly)
Returns the value of attribute fill_in.
38 39 40 |
# File 'lib/userstream.rb', line 38 def fill_in @fill_in end |
#handlers ⇒ Object (readonly)
Returns the value of attribute handlers.
30 31 32 |
# File 'lib/userstream.rb', line 30 def handlers @handlers end |
Instance Method Details
#connect(*users) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/userstream.rb', line 100 def connect(*users) unless EM.reactor_running? EM.run do connect(*users) end else @users.concat(users) @user_adder ||= EM.add_periodic_timer(1) { users_to_connect = @users.slice!(0, 10) if users_to_connect && users_to_connect.any? users_to_connect.each do |user| connect_single(user) end end } end end |
#connect_single(user) ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/userstream.rb', line 118 def connect_single(user) parser = Yajl::Parser.new parser.on_parse_complete = Proc.new{|parsed_data| if parsed_data['direct_message'] (user, parsed_data) elsif parsed_data['friends'] dispatch_friend(user, parsed_data) elsif parsed_data['text'] dispatch_tweet(user, parsed_data) elsif parsed_data['event'] method_sym = "dispatch_#{parsed_data['event']}".to_sym if respond_to?(method_sym) send("dispatch_#{parsed_data['event']}".to_sym, user, parsed_data) else puts "no handler for #{parsed_data['event']}" end elsif parsed_data['delete'] dispatch_delete(user, parsed_data) else puts "i didn't know what to do with this!" pp parsed_data end } http = get_connection(user, @connect_url, :get) http.errback { |e, err| dispatch_disconnect(user) } http.stream { |chunk| dispatch_connect(user) begin parser << chunk rescue Yajl::ParseError p $! puts "bad chunk: #{chunk.inspect}" end } end |
#dispatch_connect(user) ⇒ Object
94 95 96 97 98 |
# File 'lib/userstream.rb', line 94 def dispatch_connect(user) return if @on_connect_called[user.name] @handlers.connect.each{|h| h.call(user)} @on_connect_called[user.name] = true end |
#dispatch_disconnect(user) ⇒ Object
89 90 91 92 |
# File 'lib/userstream.rb', line 89 def dispatch_disconnect(user) return if @handlers.disconnect.empty? @handlers.disconnect.each{|h| h.call(user)} end |
#dispatch_friend(user, data) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/userstream.rb', line 72 def dispatch_friend(user, data) unless @handlers.friend.empty? data['friends'].each_slice(100) do |friend_ids| parser = Yajl::Parser.new parser.on_parse_complete = proc { |friends| friends.each do |friend| @handlers.friend.each{|h| h.call(friend)} end } friend_http = get_connection(user, "http://api.twitter.com/1/users/lookup.json", :post, :body => {'user_id' => friend_ids.join(',')}) friend_http.stream { |chunk| parser << chunk } end end end |