Class: Shiki::Base
- Inherits:
-
Object
- Object
- Shiki::Base
- Defined in:
- lib/shiki/base.rb
Constant Summary collapse
- TWITTER_URL =
"http://twitter.com"
- TWITTER_USERSTREAM_API =
"https://userstream.twitter.com/2/user.json"
- SCHEMES =
[ [:status, :mention, :follow, :favorite, :retweet], [:search] ]
- @@blocks =
{}
- @@conditions =
{}
- @@env =
{}
Instance Attribute Summary collapse
-
#memory ⇒ Object
Returns the value of attribute memory.
Class Method Summary collapse
-
.blocks ⇒ Object
Accessor methods.
- .call(action, num, option) ⇒ Object
- .enable(fuda, option) ⇒ Object
- .env(param) ⇒ Object
- .event(action, option = nil, &block) ⇒ Object
- .method_missing(action, *option) ⇒ Object
- .register(opt) ⇒ Object
- .reply(opts, status = nil) ⇒ Object
-
.say(sentence) ⇒ Object
(also: tweet)
Bot methods.
- .set(option, value = nil) ⇒ Object
- .use(name, option = nil) ⇒ Object
Instance Method Summary collapse
- #call(action, num, option = nil) ⇒ Object
- #disharmony?(keys) ⇒ Boolean
- #guess_scheme(keys) ⇒ Object
-
#initialize ⇒ Base
constructor
A new instance of Base.
- #proc_follow(status) ⇒ Object
- #proc_status_event(event, status) ⇒ Object
- #register(option) ⇒ Object
- #run ⇒ Object
Constructor Details
#initialize ⇒ Base
Returns a new instance of Base.
24 25 26 27 28 29 30 31 32 |
# File 'lib/shiki/base.rb', line 24 def initialize super() @key = self.oauth_key @consumer = OAuth::Consumer.new(@key[:consumer_key], @key[:consumer_secret], :site => TWITTER_URL) @access_token = OAuth::AccessToken.new(@consumer, @key[:access_token], @key[:access_token_secret]) register(:twitter => Pupil.new(@key)) #register(:memory => Shiki::Memory.new(self.memory_database)) end |
Instance Attribute Details
#memory ⇒ Object
Returns the value of attribute memory.
14 15 16 |
# File 'lib/shiki/base.rb', line 14 def memory @memory end |
Class Method Details
.blocks ⇒ Object
Accessor methods
147 |
# File 'lib/shiki/base.rb', line 147 def blocks; @@blocks; end |
.call(action, num, option) ⇒ Object
155 156 157 |
# File 'lib/shiki/base.rb', line 155 def call(action, num, option) @@blocks[action][num][:block].call option end |
.enable(fuda, option) ⇒ Object
176 177 178 179 |
# File 'lib/shiki/base.rb', line 176 def enable(fuda, option) # Not implement # e.g. auto_follow end |
.env(param) ⇒ Object
148 |
# File 'lib/shiki/base.rb', line 148 def env(param); @@env[param]; end |
.event(action, option = nil, &block) ⇒ Object
181 182 183 184 |
# File 'lib/shiki/base.rb', line 181 def event(action, option=nil, &block) @@blocks[action] ||= [] @@blocks[action] << {:block => block, :option => option} end |
.method_missing(action, *option) ⇒ Object
186 187 188 189 190 191 |
# File 'lib/shiki/base.rb', line 186 def method_missing(action, *option) @@env[:fuda].each do |name, option| return option[:block] if action == name && option[:instance] end raise NoMethodError end |
.register(opt) ⇒ Object
150 151 152 153 |
# File 'lib/shiki/base.rb', line 150 def register(opt) option = [*opt][0] @@conditions[option[0]] = option[1] end |
.reply(opts, status = nil) ⇒ Object
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
# File 'lib/shiki/base.rb', line 201 def reply(opts, status=nil) puts "get reply" raise ArgumentError, "target parameter not given" unless opts.values[0] sentence, target = opts.to_a.first name = nil case target.class.to_s when "Pupil::User" name = target.screen_name when "String" name = target when "Symbol" name = target.to_s end if status @@conditions[:twitter].update("@#{name} #{sentence}", status) else @@conditions[:twitter].update("@#{name} #{sentence}") end end |
.say(sentence) ⇒ Object Also known as: tweet
Bot methods
195 196 197 |
# File 'lib/shiki/base.rb', line 195 def say(sentence) @@conditions[:twitter].update(sentence) end |
.set(option, value = nil) ⇒ Object
159 160 161 162 |
# File 'lib/shiki/base.rb', line 159 def set(option, value=nil) #class_eval "def self.#{option}() #{value.inspect} ; end" define_method option, proc{ value } end |
.use(name, option = nil) ⇒ Object
164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/shiki/base.rb', line 164 def use(name, option=nil) require(File.join(File.dirname(File.($0)), "fuda/#{name}")) fuda = eval("#{name.capitalize}") @@env[:fuda] ||= {} if fuda.prepare[:make_instance] @@env[:fuda].update(name.to_sym => {:block => fuda.new(option), :option => option, :instance => true}) else @@env[:fuda].update(name.to_sym => {:block => fuda, :option => option, :instance => false}) eval("#{name.capitalize}.new(#{option})") end end |
Instance Method Details
#call(action, num, option = nil) ⇒ Object
38 39 40 |
# File 'lib/shiki/base.rb', line 38 def call(action, num, option=nil) self.class.call(action, num, option) end |
#disharmony?(keys) ⇒ Boolean
42 43 44 45 46 |
# File 'lib/shiki/base.rb', line 42 def disharmony?(keys) i1 = keys.map{|k| SCHEMES[0].include?(k)} i2 = keys.map{|k| SCHEMES[1].include?(k)} return ((i1&i2).size > 0)? true : false end |
#guess_scheme(keys) ⇒ Object
48 49 50 51 52 53 54 |
# File 'lib/shiki/base.rb', line 48 def guess_scheme(keys) if keys.map{|k| SCHEMES[0].index(k)} return :userstream else return :search end end |
#proc_follow(status) ⇒ Object
93 94 95 96 97 98 99 100 101 |
# File 'lib/shiki/base.rb', line 93 def proc_follow(status) blocks = self.class.blocks[:follow] num = 0 blocks.each do |block| option = block[:option] call(:follow, num, status) num += 1 end end |
#proc_status_event(event, status) ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/shiki/base.rb', line 103 def proc_status_event(event, status) blocks = self.class.blocks[event] num = 0 blocks.each do |block| option = block[:option] if option response = [] if option[:include] if Regexp.new(option[:include]) =~ status.text response << true else response << false end end if option[:from] if option[:from].to_s == status.user.screen_name response << true else response << false end end if response.index(false) == nil call(event, num, status) next else next end end call(event, num, status) num += 1 end end |
#register(option) ⇒ Object
34 35 36 |
# File 'lib/shiki/base.rb', line 34 def register(option) self.class.register(option) end |
#run ⇒ Object
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 85 86 87 88 89 90 91 |
# File 'lib/shiki/base.rb', line 56 def run keys = self.class.blocks.keys raise ArgumentError, "Disharnomy event blocks are detected." if disharmony?(keys) raise NoMethodError, "self.oauth_key not defined" unless self.respond_to? :oauth_key scheme = guess_scheme(keys) ps = Pupil::Stream.new self.oauth_key begin ps.start scheme do |status| #puts "Event: #{status.event}" case status.event when :status if status.text.match /^@/ # Mention event next unless self.class.blocks[:mention] proc_status_event(:mention, status) else # Status event next unless self.class.blocks[:status] proc_status_event(:status, status) end when :retweet # Retweeted event next unless self.class.blocks[:retweet] proc_status_event(:retweet, status) when :follow # Foolow event next unless self.class.blocks[:follow] proc_follow(status) end end rescue Interrupt puts "> Stopping ..." exit() end end |