Display tweet using Twitter Ruby Gem - ruby

I should say I'm new to Ruby. I just managed to create a Twitter app and configure The Twitter Ruby Gem to tweet, display tweets and so on. The problem is that when I fetch tweets, the actual content is not displayed, instead, I get somehting like #<Twitter::Tweet:0x2a3de78>.
I simply use this code snippet:
puts Twitter.status(123456789)

That's because the Twitter.status method returns an instance of the class Twitter::Tweet.
To extract data from Twitter::Tweet you have to call some methods on it, like text:
puts Twitter.status(...).text

Related

Twitter Bot to Likes some tweets using ruby

I am a Ruby beginner currently working on Twitter API's.
So far I learned how to tweet and look for tweets using twitter bot with The Twitter Ruby Gem.
def follow_hello(client)
client.search("#Hello_World", result_type: "recent").take(20).map do |tweet|
client.follow(tweet.user)
end
end
How can I "Like" all the 20 tweets with the #Hello_World?
I tried this, but it doesn't work:
client.search('#Hello_World', result_type: "recent").take(25).each do |like|
There appears to be a "favorite" call that you can make, by passing it a collection of tweets.
Although I can't test it out at the moment, something like this should work:
var tweets = client.search("#Hello_World", result_type: "recent").take(20)
client.favorite(tweets)

Fetch a tweet object with the Twitter gem

I'm using the Twitter gem in a ruby project of mine and if I retweet a tweet, I want to find that retweet belonging to me. I know this is possible by using the current_user_retweet data node documented at https://dev.twitter.com/overview/api/tweets . The issue is I have to use the twitter gem.
Currently I have this where the ID is of a tweet that I does not belong to me, but has been retweeted.
x = #twitter.status("590263114714714112")
return x.current_user_retweet.id
But the error returned says undefined method 'current_user_retweet' for #
To fetch a particular tweet by id
return client.status("590263114714714112", options = {})
where client is the configured object, the string is the id and the options is an empty hash which can be configured as needed.
Source here

Ruby twitter gem. how to get people who favorite my posts

Im trying to make an app which would iterate through my own posts and get a list of users who favorited a post. Afterwards I would like the application to follow each of those users if I am not already following them. I am using Ruby for this.
This is my code now:
#client = Twitter::REST::Client.new(config)
OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE
user = #client.user()
tweets = #client.user_timeline(user).take(20)
num_of_tweets = tweets.length
puts "tweets found: #{tweets.length}"
tweets.each do |item|
puts "#{ item}" #iterating through my posts here
end
any suggestions?
That information isn't exposed in the Twitter API, either through a timeline collection or via the endpoint representing a single tweet. This'll be why the twitter gem, which provides a useable interface around the Rest API, cannot give you what you're after.
Third party sites such as Favstar do display that information, but as far as I know their own API does not expose the relevant users in any manageable way.

Pull FullContact Ruby Gem Photos?

https://github.com/fullcontact/fullcontact-api-ruby
I'm trying to use the FullContact API Wrapper for Ruby (it's a gem) instead of the pure REST API. I'm trying to figure out how to grab the person's profile pictures from email address. I know how to get them from the REST API that responds with JSON, but not sure what the example code there is doing.
person = FullContact.person(email: "brawest#gmail.com") (pulled from example in the Github linked)
So now how do I retrieve profile pictures from person? What data type is it storing?
The FullContact gem uses Hashie, and from a call it returns a Hashie::Rash object.
So if you were trying to access photos:
> person = FullContact.person(email: "email")
=> [#<Hashie::Rash contact_info=#<Hashie::Rash family_name=...
> person.photos
=> [#<Hashie::Rash is_primary=true type="facebook" type_id="facebook" type_name="Facebook"...
Hope that helps!

Ruby Twitter Array Issue

I am new to Ruby and had a quick question.
I am trying to get all of the posts from a user's timeline, so I figured I would need to do a user_timeline api call to twitter and then filter out the posts manually. Then, while reading the Ruby Twitter documentation, I found this:
puts Twitter.user_timeline("twitter_handle").first.text
...and that will return the post already parsed out.
Is there a way to get more than just the first post automatically parsed out like that, or is that just an array method for the first and last object in the array?
Thanks
It looks like user_timeline just returns an array, so you ought to be able to use Ruby's normal array methods with it.
Twitter.user_timeline("twitter_handle").each do |tweet|
puts tweet
end
You need to iterate over each object.
Twitter.user_timeline("twitter_handle").each do |tweet|
puts tweet.text
end
The first and last methods are just convenience methods on arrays.
As you can see from source, Twitter::Client#user_timeline returns array from 20 most recent Twitter::Status, so that you can use up to 20 parsed records.

Resources