Class: Akane::Receivers::Stream

Inherits:
AbstractReceiver show all
Defined in:
lib/akane/receivers/stream.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from AbstractReceiver

#on, #on_delete, #on_event, #on_message, #on_tweet

Constructor Details

#initializeStream

Returns a new instance of Stream.



7
8
9
10
# File 'lib/akane/receivers/stream.rb', line 7

def initialize(*)
  super
  @thread = nil
end

Instance Attribute Details

#threadObject (readonly)

Returns the value of attribute thread.



23
24
25
# File 'lib/akane/receivers/stream.rb', line 23

def thread
  @thread
end

Instance Method Details

#running?Boolean

Returns:

  • (Boolean)


12
# File 'lib/akane/receivers/stream.rb', line 12

def running?() !!(@thread && @thread.alive?) end

#startObject



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
# File 'lib/akane/receivers/stream.rb', line 25

def start
  @logger.info "Stream : Starting"

  @thread = Thread.new do
    begin
      stream.user do |obj|
        case obj
        when Twitter::Tweet
          invoke(:tweet, obj)
        when Twitter::DirectMessage
          invoke(:message, obj)
        when Twitter::Streaming::DeletedTweet
          invoke(:delete, obj.user_id, obj.id)
        when Twitter::Streaming::Event
          invoke(:event,
                 'event' => obj.name, 'source' => obj.source,
                 'target' => obj.target, 'target_object' => obj.target_object)
        end
      end
    rescue Exception => e
      raise e if defined?(Twitter::Streaming::MockClient)
      @logger.error 'Error on stream'
      @logger.error e.inspect
      @logger.error e.backtrace
    end
  end

  self
end

#stopObject



55
56
57
58
59
# File 'lib/akane/receivers/stream.rb', line 55

def stop
  @thread.tap(&:kill).join
  @thread = nil
  self
end

#streamObject



14
15
16
17
18
19
20
21
# File 'lib/akane/receivers/stream.rb', line 14

def stream
  @stream ||= Twitter::Streaming::Client.new(
    consumer_key: @consumer[:token],
    consumer_secret: @consumer[:secret],
    access_token: @account[:token],
    access_token_secret: @account[:secret]
  )
end