Class: BotNyan::Wrapper::TwitterWrapper
- Inherits:
-
Object
- Object
- BotNyan::Wrapper::TwitterWrapper
show all
- Includes:
- Info
- Defined in:
- lib/bot_nyan/base.rb
Instance Method Summary
collapse
Methods included from Info
#debug, #error, #info, #logger_set!, #warn
Constructor Details
#initialize(name, consumer_keys, access_tokens, cond) ⇒ TwitterWrapper
Returns a new instance of TwitterWrapper.
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
|
# File 'lib/bot_nyan/base.rb', line 182
def initialize(name, consumer_keys, access_tokens, cond)
logger_set! cond
@name = name
unless name and consumer_keys and access_tokens
error @name, @consumer_keys, @access_tokens
raise RuntimeError, "Necessarys are not difined!"
end
@consumer = OAuth::Consumer.new(
consumer_keys[:key],
consumer_keys[:secret],
:site => 'http://twitter.com'
)
@access_token = OAuth::AccessToken.new(
@consumer,
access_tokens[:token],
access_tokens[:secret]
)
@client = Twitter::Client.new(
:consumer_key => consumer_keys[:key],
:consumer_secret => consumer_keys[:secret],
:oauth_token => access_tokens[:token],
:oauth_token_secret => access_tokens[:secret]
)
@json = nil
end
|
Instance Method Details
#connect ⇒ Object
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
|
# File 'lib/bot_nyan/base.rb', line 208
def connect
uri = URI.parse("https://userstream.twitter.com/2/user.json?track=#{@name}")
https = Net::HTTP.new(uri.host, uri.port)
https.use_ssl = true
https.start do |https|
request = Net::HTTP::Post.new(uri.request_uri)
request["User-Agent"] = "bot servise for @#{@name}"
request.oauth!(https, @consumer, @access_token)
buf = String.new
https.request(request) do |response|
raise Exception.new "Authorize failed. #{request.body}" if response.code == '401'
response.read_body do |chunk|
buf << chunk
while (line = buf[/.+?(\r\n)+/m]) != nil
begin
buf.sub!(line,"")
line.strip!
status = JSON.parse(line)
rescue
break
end
@json = status
yield status
end
end
end
end
end
|
#reply(msg) ⇒ Object
248
249
250
251
|
# File 'lib/bot_nyan/base.rb', line 248
def reply(msg)
post_proc = ->(m){ @client.update m, :in_reply_to_status_id => event.id }
update_core post_proc, msg, @json
end
|
#update(msg) ⇒ Object
244
245
246
|
# File 'lib/bot_nyan/base.rb', line 244
def update(msg)
update_core ->(m){ @client.update m }, msg, @json
end
|
#update_core(post_proc, msg, json) ⇒ Object
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
|
# File 'lib/bot_nyan/base.rb', line 253
def update_core(post_proc, msg, json)
12.times do |n|
begin
break if post_proc.call msg
rescue Twitter::Error::Forbidden
sleep 0.5
msg << " ."
if n > 10
@logger.warn "error to post reply to below"
return false
end
end
end
@logger.info "replied to #{json.id}"
true
end
|