Using Ruby Gem for Twitter to get search results of a Hashtag
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
Ruby Twitter, Retrieving Full Tweet Text
Here's a workaround I found helpful:
Below is the way I am handling this issue ATM. Seems to be working. I am using both Streaming (with Tweetstream) and REST APIs.
status = @client.status(1234567890, tweet_mode: "extended")
if status.truncated? && status.attrs[:extended_tweet]
# Streaming API, and REST API default
t = status.attrs[:extended_tweet][:full_text]
else
# REST API with extended mode, or untruncated text in Streaming API
t = status.attrs[:text] || status.attrs[:full_text]
end
From https://github.com/sferik/twitter/pull/848#issuecomment-329425006
Send direct messages with twitter gem
get the id of the twitter you want to send a message with. You can use this link to get an Id.
Then you can execute the command:
client.create_direct_message('<replace with the twitter Id you obtained>',"hello this is a test!",options={})
the tweet.user in your example means, there is a tweets-table(a model class) and the twitter Id is saved in the user-column.
Making parallel calls with twitter gem
Wrap your API calls in Ruby threads to run them concurrently:
tweets, threads = [], []
threads = user_ids.map do |user_id|
Thread.new { tweets << Client.user_timeline(user_id) }
end
threads.each(&:join)
# All the tweets are in the `tweets` array
puts tweets
Stream Live tweets from a specific user (Ruby)
If you want live updates of when a specific account posts then take a look at webhooks. Webhooks allow you to build or set up Twitter App which subscribes to certain events on twitter.com. The advantage of using webhooks is that Twitter would send you a payload as a POST request to your app and then your job would to handle this response in your app accordingly. This assumes that you have an app server running locally, like rails server. The advantage is also that it happens asynchronoysly and you do not need to manually hit Twitter API every now and then to know if there were any updates
Tweet photo with twitter gem
Assuming you're talking about the Twitter gem by sferik, I think this method here might be helpful. http://rdoc.info/gems/twitter/Twitter/API/Tweets#update_with_media-instance_method
Basically you want to use the update_with_media method. More information from Twitter is on their site https://dev.twitter.com/docs/api/1/post/statuses/update_with_media. Hope that helps!
Related Topics
Ruby: Append Text to the 2Nd Line of a File
Illegal Quoting in Line 1 Using Ruby CSV
Writing a Pre-Push Hook in Git to Grep All Files for Regex Want to Reject Push If Regex Not Found
Sublime Text 2 Doesn't Save Built SASS File
Ruby Autoload Conflicts Between Gmail and Parse_Resource Gems
How to Overwrite the '{ }' Object
Gem::Loaderror: When Trying to Deploy on Heroku - Rails 4
What Is the Fully Qualified Name of a Model in Ruby on Rails
No Route Found in Rspec Test for an Existing Route
Why Sinatra Request Takes Em Thread
Certificate to Pem to Certificate Not Working: Nested Asn1 Error
Rails - How to Add Contacts to Sendgrid Marketing Campaigns via API
{|_, E| E.Length>1} What Is the Use of Underscore ( _ ) in Ruby
Check If String Is Repetition of an Unknown Substring
Disabling Flash Message Without Disabling Cache on Click on Back Button in Rails
How to Insert Erb-Tags with Vim