Class: BotNyan::Base

Inherits:
Object
  • Object
show all
Includes:
Info
Defined in:
lib/bot_nyan/base.rb

Direct Known Subclasses

Bot

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Info

#debug, #error, #info, #logger_set!, #warn

Constructor Details

#initializeBase

Returns a new instance of Base.



44
45
46
# File 'lib/bot_nyan/base.rb', line 44

def initialize
  logger_set! debug?
end

Class Method Details

.get_matched_reply_actionsObject

Outer methods that called from inner of Base



135
136
137
# File 'lib/bot_nyan/base.rb', line 135

def get_matched_reply_actions
  @matched_reply_actions
end

.get_relpy_actionObject



139
140
141
# File 'lib/bot_nyan/base.rb', line 139

def get_relpy_action
  @reply_action
end

.on_matched_reply(regexp, &block) ⇒ Object

Outer methods that called from main objrct



144
145
146
147
# File 'lib/bot_nyan/base.rb', line 144

def on_matched_reply(regexp, &block)
  @matched_reply_actions ||= {}
  @matched_reply_actions[regexp] = block
end

.on_replied(&block) ⇒ Object



149
150
151
# File 'lib/bot_nyan/base.rb', line 149

def on_replied(&block)
  @reply_action ||= block
end

.run!Object



40
41
42
# File 'lib/bot_nyan/base.rb', line 40

def self.run!
  self.new.run
end

.set(key, value) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
# File 'lib/bot_nyan/base.rb', line 153

def set(key, value)
  keys = [:consumer_key, :access_token, :name, :debug?]
  if keys.include? key
    self.instance_eval do
      define_method key, lambda { value }
      private key
    end
  else
    raise NotImplementedError, "This option is not support, #{key}, #{value}"
  end
end

.set!(key, value) ⇒ Object



165
166
167
168
169
170
171
172
# File 'lib/bot_nyan/base.rb', line 165

def set!(key, value)
  keys = [:run?]
  if keys.include? key
    define_singleton_method key, value
  else
    raise NotImplementedError, "This option is not support, #{key}, #{value}"
  end
end

Instance Method Details

#add_on_replied(&block) ⇒ Object



129
130
131
# File 'lib/bot_nyan/base.rb', line 129

def add_on_replied(&block)
  @replied_action = block
end

#debug?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/bot_nyan/base.rb', line 48

def debug?
  nil
end

#get_matched_reply_actionsObject

Inner methods that call self.class methods



121
122
123
# File 'lib/bot_nyan/base.rb', line 121

def get_matched_reply_actions
  self.class.get_matched_reply_actions
end

#get_relpy_actionObject



125
126
127
# File 'lib/bot_nyan/base.rb', line 125

def get_relpy_action
  self.class.get_relpy_action
end

#match?(event) ⇒ Boolean

Returns:

  • (Boolean)


84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/bot_nyan/base.rb', line 84

def match?(event)
  get_matched_reply_actions.each do |regexp, block|
    if event.text.match regexp
      debug "matched to #{regexp}"
      instance_exec &block
      throw :halt
    end
  end
  if event.text.match(/(^@#{name}\s)/u) and get_relpy_action
    debug "respond to default reply"
    instance_exec &get_relpy_action
    throw :halt
  end
end

#reply(msg) ⇒ Object Also known as: respond



112
113
114
# File 'lib/bot_nyan/base.rb', line 112

def reply(msg)
  @wrapper.reply msg
end

#runObject



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
# File 'lib/bot_nyan/base.rb', line 52

def run
  @wrapper = set_wrapper
  info "starting bot for @#{name}"
  begin
    loop do
      begin
        @wrapper.connect do |event|
          catch :halt do
            debug event.event
            if event.text and event.text.match /(^@#{name}\s)/u
              debug "tweet event"
              match? event
            else
              debug 'not reply'
            end
          end
        end
      rescue Timeout::Error
        info "reconnectting to twitter..."
        sleep 30
      end
    end
  rescue Interrupt
    info "exitting bot service for @#{name}..."
    exit 0
  end
end

#set_wrapperObject



80
81
82
# File 'lib/bot_nyan/base.rb', line 80

def set_wrapper
  Wrapper::TwitterWrapper.new name, consumer_key, access_token, debug?
end

#statusObject Also known as: event

Inner methods and called from given blocks



100
101
102
# File 'lib/bot_nyan/base.rb', line 100

def status
  @wrapper.status
end

#update(msg) ⇒ Object Also known as: tweet



108
109
110
# File 'lib/bot_nyan/base.rb', line 108

def update(msg)
  @wrapper.update msg
end

#userObject



104
105
106
# File 'lib/bot_nyan/base.rb', line 104

def user
  @wrapper.status.user
end