Question

I am using grackle gem to get the tweets on rails 3.i am able to get the tweets and show them on the view. Now I want to search the hashtags using this gem. Is it possible. And if yes,then please tell me how because I have already registered to twitter API and got the secret token and I am still trying to modify the code so as to get the hashtags, I already made use of the twitter API and I am able to get the output but how to modify/implement it in my working code.

my tweet.rb file

  Class Tweet < ActiveRecord::Base

  require 'grackle'
  require 'twitter'
  require "rubygems"
   MY_APPLICATION_NAME = "mike1011"


attr_accessible :content, :created


 ####Connect to the Twitter API and pull down the latest tweets"""
  def self.get_latest 
   ##the call to twitter api works but how to modify this further to use the search            api**
    tweets = client.search.home_timeline? :screen_name => MY_APPLICATION_NAME # hit the API

    ##this works on twitter console    
    ##https://api.twitter.com/1.1/search/tweets.json?q=%23haiku


  end

  private
  def self.client
    Grackle::Client.new(:auth=>{
      :type=>:oauth,
      :consumer_key=>'xxxxxxxxxxxxxxx',
      :consumer_secret=>'xxxxxxxxxxxxxxx',
      :token=>"2227985911-xxxxxxxxxxxxxxxxxx",
      :token_secret=>"xxxxxxxxxxxxxxxxxxx"
    })

  end   
end

My view file where I want the user to search the hashtags by using the search button and value filled in the textbox

<fieldset style="height:auto;width:auto">
<%= text_field_tag 'Search', 'Enter your hashtags'%>
<%= button_tag(type: 'button') do
  content_tag(:strong, 'Search in twitter!',:id=>"search_button")
end
%>

</fieldset>
Was it helpful?

Solution

Yes, it is possible. If you are not going to save the tweets to a database it is better to create an api so you can call it from any place or put it all inside of a controller.

A benefit of doing so is that you can then use parameters.

Take a look at my code: https://github.com/eflores1975/stock-dashboard/blob/master/app/controllers/tweets_controller.rb

Basically it loads the tweets to an array and then using ActiveSupport::JSON.decode I convert it in a consumable object and start to massage it to suit my needs.

And,finally, I can use it as: http://gentle-tor-3942.herokuapp.com/tweets/get_tweets?symbol=$aapl

Where symbol=$aapl is the hash tag that I am looking for.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top