Fetch a tweet object with the Twitter gem - ruby

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

Related

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.

How to access Google Contacts API in ruby

I'm struggling to access the Google Contacts API.First I tried the google-api-ruby-client gem but it turned out that it does not support the Contacts API.
Next shot was the google_contacts_api gem. I used oauth2 to access the authentication key(Getting authentication token guide question). But after passing the token correctly to the api it is producing an error.
`<main>': undefined method `[]' for #<GoogleContactsApi::GroupSet:0x000000039fcad8>` (NoMethodError).
Here is my code.
# get token using oauth2 gem, and use it below in the google_contacts_api.
google_contacts_user = GoogleContactsApi::User.new(token)
contacts = google_contacts_user.contacts
groups = google_contacts_user.groups
# group methods
group = groups[0]
group.contacts
puts group.contacts
# contact methods
puts contacts.count
puts groups.count
contact = contacts[0]
contact.primary_email
contact.emails
What am I doing wrong?
UPDATE:
As #alvin suggested it is working now. But the group contacts are not being printed out. Instead it is printing #<GoogleContactsApi::ContactSet:0x000000020e49d8>.Example: here is what is printed by this code
groups = google_contacts_user.groups
# group methods
groups.each do |group|
group_contacts = group.contacts
puts group_contacts
end
Output:
#<GoogleContactsApi::ContactSet:0x000000020e49d8>
#<GoogleContactsApi::ContactSet:0x0000000504aec0>
#<GoogleContactsApi::ContactSet:0x0000000518dfd0>
#<GoogleContactsApi::ContactSet:0x000000052d9290>
#<GoogleContactsApi::ContactSet:0x000000054280d8>
#<GoogleContactsApi::ContactSet:0x0000000558c2f8>
#<GoogleContactsApi::ContactSet:0x00000005746eb8>
#<GoogleContactsApi::ContactSet:0x000000058a3ea0>
How can I print the group contacts?
Edited to add info about the Enumerable implementation
(I wrote the gem.)
There was a bug in the documentation. groups and contacts are instances of classes that implement Enumerable, which doesn't provide the [] method, but does provide the first method.
So, try groups.first instead of groups[0]. Likewise, use contacts.first instead of contacts[0]. My bad! (I probably did a to_a in my head.)
Response to Update
To answer the second half of the question, it looks like you found the relevant convenience methods for Contact and Group, in particular the Contact.primary_email method. See more methods in the (somewhat incomplete, sorry) YARD docs.
To get all the emails, you basically need to iterate over the returned contacts. As I mentioned in the updated response to the first part of your question, groups and contacts have all the methods of Enumerable. (Enumerable documentation). Here are some examples:
# What are all the groups called?
user.groups.map(&:title)
# Find group by title. (Returns nil if no such group.)
group = user.groups.select { |g| g.title = "Group Name" }
# Get all primary emails from a group
group.contacts.map(&:primary_email)
# Get all primary emails from all contacts regardless of group
user.contacts.map(&:primary_email)
You only need to use the Hashie::Mash methods to access data when no convenience accessor is provided (for example, if Google starts returning extra data the gem hasn't accounted for yet). The use case you described doesn't require this.
P.S. In the future, you might want to open a new question instead of editing your existing question.

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 -- Using facebook's Graph API Explorer in conjunction with the koala gem

I've found facebook's 'Graph API Explorer' tool (https://developers.facebook.com/tools/explorer/) to be an incredibly easy way, welcoming (for beginners) & effective way to use facebook's graph API via its GUI.
I'd like to be able to use the koala gem to pass these generated URLs to facebook's api.
Right now, lets say I had a query like this
url = "me?fields=id,name,posts.fields(likes.fields(id,name),comments.fields(parent,likes.fields(id,name)),message)"
I'd like to be able to pass that directly into koala as a single string.
#graph.get_connections(url)
It doesn't like that so I separate out the uid and the ? operator like the gem seems to want
url = "fields=id,name,posts.fields(likes.fields(id,name),comments.fields(parent,likes.fields(id,name)),message)"
#graph.get_connections("me", url)
This however, returns an error as well:
Koala::Facebook::AuthenticationError:
type: OAuthException, code: 2500,
message: Unknown path components: /fields=id,name,posts.fields(likes.fields(id,name),comments.fields(parent,likes.fields(id,name)),message) [HTTP 400]
Currently this is where I am stuck. I'd like to continue using koala because I like the gem-approach to working with API's, especially when it comes to using OAuth & OAuth2.
UPDATE:
I'm starting to break down the request into pieces which the koala gem can handle, for example
posts = #graph.get_connections("me", "posts")
postids = posts.map { |p| p['id'] }
likes = postids.inject([]) {|ary, id| ary << #graph.get_connection(id, "likes") }
So that's a long way of getting two arrays, one of posts, one of like data.
But I'd quickly burn up my API requests limit in no time using this kind of approach.
I was kind of hoping I'd just be able to pass the whole string from the Graph API Explorer and just get what I wanted rather than having to manually parse all this stuff.
I don't really know about your posts.fields(likes.fields(id,name) -this does not work in the Graph API Explorer- and stuff like that but I know you can do this:
fb_api = Koala::Facebook::API.new(access_token)
fb_api.api("/me?fields=id,name,posts")
# => => {"id"=>"71170", "name"=>"My Name", "posts"=>{"paging"=>{"next"=>"https://graph.facebook.com/71170/posts?access_token=CAAEO&limit=25&until=13705022", "previous"=>"https://graph.facebook.com/711737070/posts?access_token=CAAEOTYMZD&limit=25&since=1370723&__previous=1"}, "data"=>[{"id"=>"71170_1013572471", "comments"=>{"count"=>0}, "created_time"=>"2013-06-09T08:03:43+0000", "from"=>{"id"=>"71170", "name"=>"My Name"}, "updated_time"=>"2013-06-09T08:03:43+0000", "privacy"=>{"value"=>""}, "type"=>"status", "story_tags"=>{"0"=>[{"id"=>"71170", "name"=>" ", "length"=>8, "type"=>"user", "offset"=>0}]}, "story"=>" likes a photo."}]}}
And you will receive in a hash what you asked for.
From time to time, you must pass nil as a param to koala:
result += graph_api.batch do |batch_api|
facebook_page_ids.each do |facebook_page_id|
batch_api.get_connections(facebook_page_id, nil, {"fields"=>"posts"})
end
end

Display tweet using Twitter Ruby Gem

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

Resources