Search code examples
ruby-on-railstwitterrubygems

Using Ruby Gem for Twitter to get search results of a Hashtag


I am trying to use this Twitter gem to connect with Twitter and return a list of all tweets that have a particular hashtag. As of right now, I have been able to connect to the API and return data, so I know that the connection is working. I have read through the documentation and am just not understanding how to implement what I am seeing. I am really new to connecting and utilizing APIs and some clarification would really be appreciated. Here is the code that I have so far.

Gemfile

#####################
### My Added Gems ###
#####################

# Define Ruby Version
ruby "2.5.0"

# Social Connections
gem 'twitter'

# ENV Variables
gem 'dotenv-rails'

views/pages/home.html.erb

<h1>Tweets</h1>
<% @tweets.each do |tweet| %>
  <%= tweet.full_text %>
  <br>
<% end %>

controllers/pages_controller.rb

class PagesController < ApplicationController
  require 'twitter'

  def home
    client = Twitter::REST::Client.new do |config|
      config.consumer_key        = ENV.fetch("YOUR_CONSUMER_KEY")
      config.consumer_secret     = ENV.fetch("YOUR_CONSUMER_SECRET")
      config.access_token        = ENV.fetch("YOUR_ACCESS_TOKEN")
      config.access_token_secret = ENV.fetch("YOUR_ACCESS_SECRET")
    end

    @tweets = client.user_timeline('trendingfaith', count: 20)

  end
end

This prints out a list of the tweets for the user @TrendingFaith.

What I tried to get it to print out all tweets with the hashtag #TrendingFaith:

pages_controller.rb

class PagesController < ApplicationController
  require 'twitter'

  def home
    tweets = Twitter::REST::Search.new do |config|
      config.consumer_key        = ENV.fetch("YOUR_CONSUMER_KEY")
      config.consumer_secret     = ENV.fetch("YOUR_CONSUMER_SECRET")
      config.access_token        = ENV.fetch("YOUR_ACCESS_TOKEN")
      config.access_token_secret = ENV.fetch("YOUR_ACCESS_SECRET")
    end
    @tweets = tweets.search(q, options = {:hash => 'trendingfaith'}) ⇒ Twitter::SearchResults

  end
end

Got the following error: syntax error, unexpected tIDENTIFIER, expecting keyword_end

I tried reading through this doc on the Twitter Gem and the Twitter API docs. I just can't understand it at this time to know how to implement it. Any guidance would be very much appreciated.

UPDATED: Working Code after receiving an Answer

Per the answer by @SimonFranzen, the code that I was missing was client.search('#SpecificTag') syntax to search for a specific hashtag.

Here is what my code now looks like:

controllers/pages_controllers.rb

class PagesController < ApplicationController
  require 'twitter'

  def home
    tweets = Twitter::REST::Client.new do |config|
      config.consumer_key        = ENV.fetch("YOUR_CONSUMER_KEY")
      config.consumer_secret     = ENV.fetch("YOUR_CONSUMER_SECRET")
      config.access_token        = ENV.fetch("YOUR_ACCESS_TOKEN")
      config.access_token_secret = ENV.fetch("YOUR_ACCESS_SECRET")
    end
    @tweets = tweets.search('#TrendingFaith')
  end
end

I ran byebug on this and was able to see the data that came back from Twitter with this search. (See hash data below) and was thus able to update my view like this:

views/pages/home.html.erb

<h1>TrendingFaith.org - Coming Soon</h1>
<% @tweets.each do |tweet| %>
  User @<%= tweet.user.screen_name %> said: "<%= tweet.text %>" on <%= tweet.created_at.strftime('%-m/%d/%Y') %>
  <br>
<% end %>

This was able to pull the Twitter user's name, the text of the tweet and when they tweeted it. This is exactly what I was looking for.

Twitter hash that was returned to me, placed here for others that may want to know. Also, you can see it here in their documentation at the bottom of the page.

{
  :created_at=>"Sun Mar 04 18:20:42 +0000 2018", 
  :id=>970363418858332160, 
  :id_str=>"970363418858332160", 
  :text=>"A place where you can find things trending in the Christian faith. Questions and stories. #TrendingFaith", 
  :truncated=>false, 
  :entities=>
    {
      :hashtags=>[{
        :text=>"TrendingFaith", 
        :indices=>[90, 104]
      }], 
      :symbols=>[], 
      :user_mentions=>[], 
      :urls=>[]
    }, 
  :metadata=>
    {
      :iso_language_code=>"en", 
      :result_type=>"recent"
    }, 
  :source=>"<a href=\"http://twitter.com\" rel=\"nofollow\">Twitter Web Client</a>", 
  :in_reply_to_status_id=>nil, 
  :in_reply_to_status_id_str=>nil, 
  :in_reply_to_user_id=>nil, 
  :in_reply_to_user_id_str=>nil, 
  :in_reply_to_screen_name=>nil, 
  :user=>
    {
      :id=>970155879180873733, 
      :id_str=>"970155879180873733", 
      :name=>"Trending Faith", 
      :screen_name=>"TrendingFaith", 
      :location=>"", 
      :description=>"", 
      :url=>#Twitter URL Shortener (Had to remove for Stackoverflow),  
      :entities=>
        {
          :url=>
            {
              :urls=>[
                {
                  :url=>#Twitter URL Shortener (Had to remove for Stackoverflow), 
                  :expanded_url=>"http://trendingfaith.org", 
                  :display_url=>"trendingfaith.org", 
                  :indices=>[0, 23]
                }
              ]
            }, 
          :description=>
            {
              :urls=>[]
            }
        }, 
      :protected=>false, 
      :followers_count=>0, 
      :friends_count=>3, 
      :listed_count=>0, 
      :created_at=>"Sun Mar 04 04:36:00 +0000 2018", 
      :favourites_count=>0, 
      :utc_offset=>nil, 
      :time_zone=>nil, 
      :geo_enabled=>false, 
      :verified=>false, 
      :statuses_count=>1, 
      :lang=>"en", 
      :contributors_enabled=>false, 
      :is_translator=>false, 
      :is_translation_enabled=>false, 
      :profile_background_color=>"F5F8FA", 
      :profile_background_image_url=>nil, 
      :profile_background_image_url_https=>nil, 
      :profile_background_tile=>false, 
      :profile_image_url=>"http://abs.twimg.com/sticky/default_profile_images/default_profile_normal.png", 
      :profile_image_url_https=>"https://abs.twimg.com/sticky/default_profile_images/default_profile_normal.png", 
      :profile_link_color=>"1DA1F2", 
      :profile_sidebar_border_color=>"C0DEED", 
      :profile_sidebar_fill_color=>"DDEEF6", 
      :profile_text_color=>"333333", 
      :profile_use_background_image=>true, 
      :has_extended_profile=>false, 
      :default_profile=>true, 
      :default_profile_image=>true, 
      :following=>false, 
      :follow_request_sent=>false, 
      :notifications=>false, 
      :translator_type=>"none"
    }, 
  :geo=>nil, 
  :coordinates=>nil, 
  :place=>nil, 
  :contributors=>nil, 
  :is_quote_status=>false, 
  :retweet_count=>0, 
  :favorite_count=>0, 
  :favorited=>false, 
  :retweeted=>false, 
  :lang=>"en"
}

New Question

Now to figure out why only one tweet is coming back as there are hundreds with the hashtag that I am searching for, but only one for me. I checked by searching the hashtag '#HelloWorld' and it came back with many. So, it isn't just searching my tweets. I am guessing that all of the others with the hashtag I am looking for may be private tweets unable to be pulled except through Twitter? Or is this another issue with my code? If this is an unrelated issue to my original question, I can create a new question and ask.

FINAL UPDATE: Answer to last question

The reason that only my tweet is being pulled for the hashtag 'TrendingFaith' but I get a lot more tweets when pulling the hashtag 'HelloWorld' is because only my tweet was made within the past week with '#TrendingFaith'. The Standard Twitter API search only pulls the past 7-days of tweets. You have to have a Premium or Enterprise level account to search for 30-day or full archived posts. Their Premium level is currently (as of 3/4/2018) in beta and taking requests to be on their waitlist.

Link to Twitter Page


Solution

  • Use twitter gem

    gem install twitter
    

    or include it in your Gemfile

    Create Twitter App, get auth codes

    Init API

    require 'twitter'
    
    client = Twitter::REST::Client.new do |config|
      config.consumer_key        = "YOUR_CONSUMER_KEY"
      config.consumer_secret     = "YOUR_CONSUMER_SECRET"
      config.access_token        = "YOUR_ACCESS_TOKEN"
      config.access_token_secret = "YOUR_ACCESS_SECRET"
    end
    

    Make a request "Tweets for user"

    // get from user
    tweets = client.user_timeline('rubyinside', count: 20)
    tweets.each { |tweet| puts tweet.full_text }
    

    Make a request "Search by hashtag"

    client.search('#searchHash').take(3).each do |tweet|
      puts tweet.inspect
    end
    

    Checkout this to buildup your query: https://www.hitchhq.com/twitter/activities/592c37f4da4746638900618e or https://developer.twitter.com/en/docs/tweets/search/api-reference/get-search-tweets