Fix deprecations for twitter gem 5

This commit is contained in:
Jaiden Mispy 2014-10-18 22:25:39 -07:00
parent 228e0caa65
commit f4dbf89c15

View file

@ -46,8 +46,8 @@ module Ebooks
@twitter = Twitter::REST::Client.new do |config| @twitter = Twitter::REST::Client.new do |config|
config.consumer_key = @consumer_key config.consumer_key = @consumer_key
config.consumer_secret = @consumer_secret config.consumer_secret = @consumer_secret
config.oauth_token = @oauth_token config.access_token = @access_token
config.oauth_token_secret = @oauth_token_secret config.access_token_secret = @access_token_secret
end end
needs_stream = [@on_follow, @on_message, @on_mention, @on_timeline].any? {|e| !e.nil?} needs_stream = [@on_follow, @on_message, @on_mention, @on_timeline].any? {|e| !e.nil?}
@ -88,19 +88,19 @@ module Ebooks
end end
@stream.userstream do |ev| @stream.userstream do |ev|
next unless ev[:text] # If it's not a text-containing tweet, ignore it next unless ev.text # If it's not a text-containing tweet, ignore it
next if ev[:user][:screen_name] == @username # Ignore our own tweets next if ev.user.screen_name == @username # Ignore our own tweets
meta = {} meta = {}
mentions = ev.attrs[:entities][:user_mentions].map { |x| x[:screen_name] } mentions = ev.attrs[:entities][:user_mentions].map { |x| x[:screen_name] }
reply_mentions = mentions.reject { |m| m.downcase == @username.downcase } reply_mentions = mentions.reject { |m| m.downcase == @username.downcase }
reply_mentions = [ev[:user][:screen_name]] + reply_mentions reply_mentions = [ev.user.screen_name] + reply_mentions
meta[:reply_prefix] = reply_mentions.uniq.map { |m| '@'+m }.join(' ') + ' ' meta[:reply_prefix] = reply_mentions.uniq.map { |m| '@'+m }.join(' ') + ' '
meta[:limit] = 140 - meta[:reply_prefix].length meta[:limit] = 140 - meta[:reply_prefix].length
mless = ev[:text] mless = ev.text
begin begin
ev.attrs[:entities][:user_mentions].reverse.each do |entity| ev.attrs[:entities][:user_mentions].reverse.each do |entity|
last = mless[entity[:indices][1]..-1]||'' last = mless[entity[:indices][1]..-1]||''
@ -108,7 +108,7 @@ module Ebooks
end end
rescue Exception rescue Exception
p ev.attrs[:entities][:user_mentions] p ev.attrs[:entities][:user_mentions]
p ev[:text] p ev.text
raise raise
end end
meta[:mentionless] = mless meta[:mentionless] = mless
@ -117,8 +117,8 @@ module Ebooks
# - The tweet mentions list contains our username # - The tweet mentions list contains our username
# - The tweet is not being retweeted by somebody else # - The tweet is not being retweeted by somebody else
# - Or soft-retweeted by somebody else # - Or soft-retweeted by somebody else
if mentions.map(&:downcase).include?(@username.downcase) && !ev[:retweeted_status] && !ev[:text].start_with?('RT ') if mentions.map(&:downcase).include?(@username.downcase) && !ev.retweeted_status && !ev.text.start_with?('RT ')
log "Mention from @#{ev[:user][:screen_name]}: #{ev[:text]}" log "Mention from @#{ev.user.screen_name}: #{ev.text}"
@on_mention.call(ev, meta) if @on_mention @on_mention.call(ev, meta) if @on_mention
else else
@on_timeline.call(ev, meta) if @on_timeline @on_timeline.call(ev, meta) if @on_timeline
@ -139,11 +139,11 @@ module Ebooks
opts = opts.clone opts = opts.clone
if ev.is_a? Twitter::DirectMessage if ev.is_a? Twitter::DirectMessage
log "Sending DM to @#{ev[:sender][:screen_name]}: #{text}" log "Sending DM to @#{ev.sender.screen_name}: #{text}"
@twitter.direct_message_create(ev[:sender][:screen_name], text, opts) @twitter.direct_message_create(ev.sender.screen_name, text, opts)
elsif ev.is_a? Twitter::Tweet elsif ev.is_a? Twitter::Tweet
log "Replying to @#{ev[:user][:screen_name]} with: #{text}" log "Replying to @#{ev.user.screen_name} with: #{text}"
@twitter.update(text, in_reply_to_status_id: ev[:id]) @twitter.update(text, in_reply_to_status_id: ev.id)
else else
raise Exception("Don't know how to reply to a #{ev.class}") raise Exception("Don't know how to reply to a #{ev.class}")
end end