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.



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

def initialize
  logger_set! debug?
end

Class Method Details

.get_matched_reply_actionsObject

Outer methods that called from inner of Base



133
134
135
# File 'lib/bot_nyan/base.rb', line 133

def get_matched_reply_actions
  @matched_reply_actions
end

.get_relpy_actionObject



137
138
139
# File 'lib/bot_nyan/base.rb', line 137

def get_relpy_action
  @reply_action
end

.on_matched_reply(regexp, &block) ⇒ Object

Outer methods that called from main objrct



142
143
144
145
# File 'lib/bot_nyan/base.rb', line 142

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

.on_replied(&block) ⇒ Object



147
148
149
# File 'lib/bot_nyan/base.rb', line 147

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

.run!Object



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

def self.run!
  self.new.run
end

.set(key, value) ⇒ Object



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

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



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

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



127
128
129
# File 'lib/bot_nyan/base.rb', line 127

def add_on_replied(&block)
  @replied_action = block
end

#debug?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/bot_nyan/base.rb', line 52

def debug?
  nil
end

#get_matched_reply_actionsObject

Inner methods that call self.class methods



119
120
121
# File 'lib/bot_nyan/base.rb', line 119

def get_matched_reply_actions
  self.class.get_matched_reply_actions
end

#get_relpy_actionObject



123
124
125
# File 'lib/bot_nyan/base.rb', line 123

def get_relpy_action
  self.class.get_relpy_action
end

#match?(event) ⇒ Boolean

Returns:

  • (Boolean)


90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/bot_nyan/base.rb', line 90

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

#reply(msg) ⇒ Object



114
115
116
# File 'lib/bot_nyan/base.rb', line 114

def reply(msg)
  @wrapper.reply msg
end

#runObject



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

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"
              debug event
              match? event
            else
              debug 'not tweets'
              debug event
            end
          end
        end
      rescue Timeout::Error
        info "reconnectting to twitter..."
        sleep 30
      end
    end
  rescue Interrupt
    info "\nexitting bot service for @#{name}..."
    exit 0
  end
end

#set_wrapperObject



86
87
88
# File 'lib/bot_nyan/base.rb', line 86

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

#statusObject

Inner methods and called from given blocks



106
107
108
# File 'lib/bot_nyan/base.rb', line 106

def status
  @wrapper.status
end

#update(msg) ⇒ Object



110
111
112
# File 'lib/bot_nyan/base.rb', line 110

def update(msg)
  @wrapper.update msg
end