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
93
94
95
96
97
98
99
100
101
102
|
# File 'lib/twterm/streaming_client.rb', line 21
def connect_user_stream
return if user_stream_connected?
@streaming_thread = Thread.new do
begin
publish(Event::Message::Info.new('Trying to connect to Twitter...'))
streaming_client.user do |event|
keep_alive!
case event
when Twitter::Tweet
status = status_repository.create(event)
user = user_repository.create(event.user)
publish(Event::Status::Timeline.new(status))
if !status. && status.text.include?('@%s' % screen_name)
publish(Event::Status::Mention.new(status))
notification = Event::Notification::Mention.new(status, user)
publish(notification)
end
if status. && event..user.id == user_id
= status_repository.create(event.)
notification = Event::Notification::Retweet.new(, user)
publish(notification)
end
when Twitter::Streaming::Event
case event.name
when :favorite
next if event.source.id == user_id
user = user_repository.create(event.source)
status = status_repository.create(event.target_object)
notification = Event::Notification::Like.new(status, user)
publish(notification)
when :follow
next if event.source.id == user_id
user = user_repository.create(event.source)
notification = Event::Notification::Follow.new(user)
publish(notification)
when :list_member_added
next if event.source.id == user_id
list = list_repository.create(event.target_object)
notification = Event::Notification::ListMemberAdded.new(list)
publish(notification)
when :quoted_tweet
next if event.source.id == user_id
status = status_repository.create(event.target_object)
user = user_repository.create(event.source)
notification = Event::Notification::Quote.new(status, user)
publish(notification)
end
when Twitter::DirectMessage
next if event.sender.id == user_id
user = user_repository.create(event.sender)
message = DirectMessage.new(event)
notification = Event::Notification::DirectMessage.new(message, user)
publish(notification)
when Twitter::Streaming::FriendList
user_stream_connected!
when Twitter::Streaming::DeletedTweet
publish(Event::StatusDeleted.new(event.id))
end
end
rescue Twitter::Error::TooManyRequests
publish(Event::Message::Error.new('Rate limit exceeded'))
sleep 120
retry
rescue Errno::ENETUNREACH, Errno::ETIMEDOUT, Resolv::ResolvError
publish(Event::Message::Error.new('Network is unavailable'))
sleep 30
retry
rescue Twitter::Error => e
publish(Event::Message::Error.new(e.message))
end
end
end
|