Module: Twterm::StreamingClient

Includes:
Publisher
Included in:
Client
Defined in:
lib/twterm/streaming_client.rb

Constant Summary collapse

CONSUMER_KEY =
'vLNSVFgXclBJQJRZ7VLMxL9lA'.freeze
CONSUMER_SECRET =
'OFLKzrepRG2p1hq0nUB9j2S9ndFQoNTPheTpmOY0GYw55jGgS5'.freeze

Instance Method Summary collapse

Methods included from Publisher

#publish

Methods included from Utils

check_type

Instance Method Details

#connect_user_streamObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/twterm/streaming_client.rb', line 14

def connect_user_stream
  streaming_client.stop_stream

  @streaming_thread = Thread.new do
    begin
      publish(Event::Notification.new(:message, 'Trying to connect to Twitter...'))
      streaming_client.userstream
    rescue EventMachine::ConnectionError
      publish(Event::Notification.new(:error, 'Connection failed'))
      sleep 30
      retry
    end
  end
end

#initialize_user_streamObject



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
# File 'lib/twterm/streaming_client.rb', line 29

def initialize_user_stream
  return if user_stream_initialized?

  streaming_client.on_friends do
    user_stream_connected!
  end

  streaming_client.on_timeline_status do |tweet|
    status = Status.new(tweet)
    publish(Event::Status::Timeline.new(status))
    publish(Event::Status::Mention.new(status)) if status.text.include?('@%s' % screen_name)
  end

  streaming_client.on_delete do |status_id|
    publish(Event::StatusDeleted.new(status_id))
  end

  streaming_client.on_event(:favorite) do |event|
    user = User.new(Twitter::User.new(event[:source]))
    status = Status.new(Twitter::Status.new(event[:target_object]))

    event = Event::Favorite.new(user, status, self)
    publish(event)
  end

  streaming_client.on_event(:follow) do |event|
    source = User.new(Twitter::User.new(event[:source]))
    target = User.new(Twitter::User.new(event[:target]))

    event = Event::Follow.new(source, target, self)
    publish(:followed, event)
  end

  streaming_client.on_no_data_received do
    user_stream_disconnected!
    connect_user_stream
  end

  user_stream_initialized!
end