如何过滤Twitter API提及

时间:2017-03-15 20:04:06

标签: ruby twitter rubygems

我正在使用Twitter gem创建一个项目,需要过滤我从mentions_timeline收到的推文。

我考虑通过阅读推文文本的几个if语句进行过滤:

require 'sinatra'
require 'twitter'
require 'sinatra/reloader'

include ERB::Util

before do
  config = {
      :consumer_key => 'xxx',
      :consumer_secret => 'xxx',
      :access_token => 'xxx',
      :access_token_secret => 'xxx'
  }
    @client = Twitter::REST::Client.new(config)
end

get '/order' do
  @tweets = @client.mentions_timeline
  @collect_tweets = Array.new
  @delivery_tweets = Array.new

  @tweets.each do |tweet|
    if (tweet.text.include? "order" && tweet.text.include? "collect")
      @collect_tweets.push(tweet)
    elsif (tweet.text.include? "order" && tweet.text.include? "delivery")
      @delivery_tweets.push(tweet)
    end

  erb :order
end

但它不起作用。

我收到此错误:

/Users/me/Projects/team-13/order.rb:23: syntax error, unexpected tSTRING_BEG, expecting ')' rder" && tweet.text.include? "collect") ^ /Users/me/Projects/team-13/order.rb:25: syntax error, unexpected tSTRING_BEG, expecting ')' rder" && tweet.text.include? "delivery") ^ /Users/me/Projects/team-13/order.rb:30: syntax error, unexpected end-of-input, expecting keyword_end

2 个答案:

答案 0 :(得分:0)

你没有关闭每个街区:

'order.rb:30: syntax error, unexpected end-of-input, expecting keyword_end'

阅读错误消息,它会告诉您在哪里修复它。

   @tweets.each do |tweet|
     if (tweet.text.include? "order" && tweet.text.include? "collect")
      @collect_tweets.push(tweet)
     elsif (tweet.text.include? "order" && tweet.text.include? "delivery")
      @delivery_tweets.push(tweet)
     end
   end

答案 1 :(得分:0)

您的代码中有一些错误。

代码中第23行和第25行的第一个是include?方法的参数需要括号。当你列出像你这样的多种方法时,你需要这样做。您可以将其更改为:

if (tweet.text.include?("order") && tweet.text.include?("collect"))

第30行的另一个错误是您错过了每个循环的end

each循环应如下所示:

@tweets.each do |tweet|
    if (tweet.text.include?("order") && tweet.text.include?("collect"))
        @collect_tweets.push(tweet)
    elsif (tweet.text.include?("order") && tweet.text.include?("delivery"))
        @delivery_tweets.push(tweet)
    end
end