Class: StreamingHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/chatterbot/streaming_handler.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bot, opts = {}) ⇒ StreamingHandler

Returns a new instance of StreamingHandler.



13
14
15
16
# File 'lib/chatterbot/streaming_handler.rb', line 13

def initialize(bot, opts = {})
  @bot = bot
  @opts = opts
end

Instance Attribute Details

#delete_handlerObject (readonly)

Returns the value of attribute delete_handler.



7
8
9
# File 'lib/chatterbot/streaming_handler.rb', line 7

def delete_handler
  @delete_handler
end

#dm_handlerObject (readonly)

Returns the value of attribute dm_handler.



5
6
7
# File 'lib/chatterbot/streaming_handler.rb', line 5

def dm_handler
  @dm_handler
end

#favorite_handlerObject (readonly)

Returns the value of attribute favorite_handler.



4
5
6
# File 'lib/chatterbot/streaming_handler.rb', line 4

def favorite_handler
  @favorite_handler
end

#follow_handlerObject (readonly)

Returns the value of attribute follow_handler.



6
7
8
# File 'lib/chatterbot/streaming_handler.rb', line 6

def follow_handler
  @follow_handler
end

#friends_handlerObject (readonly)

Returns the value of attribute friends_handler.



8
9
10
# File 'lib/chatterbot/streaming_handler.rb', line 8

def friends_handler
  @friends_handler
end

#optsObject

Returns the value of attribute opts.



11
12
13
# File 'lib/chatterbot/streaming_handler.rb', line 11

def opts
  @opts
end

#search_filterObject (readonly)

Returns the value of attribute search_filter.



9
10
11
# File 'lib/chatterbot/streaming_handler.rb', line 9

def search_filter
  @search_filter
end

#tweet_handlerObject (readonly)

Returns the value of attribute tweet_handler.



3
4
5
# File 'lib/chatterbot/streaming_handler.rb', line 3

def tweet_handler
  @tweet_handler
end

Instance Method Details

#apply(block) ⇒ Object



93
94
95
# File 'lib/chatterbot/streaming_handler.rb', line 93

def apply(block)
  instance_exec &block
end

#apply_main_block(&block) ⇒ Object



84
85
86
87
88
89
90
# File 'lib/chatterbot/streaming_handler.rb', line 84

def apply_main_block(&block)
  if !@tweet_handler.nil?
    warn "WARNING: when using streaming, you may only have one block of user/replies/timeline/sample/filter"
    raise RuntimeError, 'Unable to load bot'
  end
  @tweet_handler = block
end

#botObject



18
19
20
# File 'lib/chatterbot/streaming_handler.rb', line 18

def bot
  @bot
end

#configObject



22
23
24
# File 'lib/chatterbot/streaming_handler.rb', line 22

def config
  bot.config
end

#connection_paramsObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/chatterbot/streaming_handler.rb', line 65

def connection_params 
  opts = {
    #:with => 'followings',
    #:replies => false,
    :stall_warnings => false
  }.merge(@opts)
 
  opts.delete(:endpoint)
    
  # convert true/false to strings
  opts.each { |k, v| opts[k] = v.to_s }
  
  if @search_filter
    opts[:track] = @search_filter
  end

  opts
end

#delete(&block) ⇒ Object



57
58
59
# File 'lib/chatterbot/streaming_handler.rb', line 57

def delete(&block)
  @delete_handler = block
end

#direct_message(&block) ⇒ Object



49
50
51
# File 'lib/chatterbot/streaming_handler.rb', line 49

def direct_message(&block)
  @dm_handler = block
end

#endpointObject

filter, firehose, sample, user



27
28
29
# File 'lib/chatterbot/streaming_handler.rb', line 27

def endpoint
  opts[:endpoint] || :user
end

#favorited(&block) ⇒ Object



45
46
47
# File 'lib/chatterbot/streaming_handler.rb', line 45

def favorited(&block)
  @favorite_handler = block
end

#followed(&block) ⇒ Object



53
54
55
# File 'lib/chatterbot/streaming_handler.rb', line 53

def followed(&block)
  @follow_handler = block
end

#friends(&block) ⇒ Object



61
62
63
# File 'lib/chatterbot/streaming_handler.rb', line 61

def friends(&block)
  @friends_handler = block
end

#search(query, opts = {}, &block) ⇒ Object



31
32
33
34
35
# File 'lib/chatterbot/streaming_handler.rb', line 31

def search(query, opts = {}, &block)
  @search_filter = query
  @search_opts = opts
  apply_main_block(&block) if block_given?
end

#user(&block) ⇒ Object Also known as: replies, timeline, sample, filter



37
38
39
# File 'lib/chatterbot/streaming_handler.rb', line 37

def user(&block)
  apply_main_block(&block)
end