How do I get Twitter follow request with the Tweetstream Ruby gem? - ruby

According to this Ars Technica tutorial, "the streaming API makes it easy to detect when the user gets a new follower. To detect follow event objects, look for the "event" key and check if it has the string "follow" as the value."
http://arstechnica.com/information-technology/2010/04/tutorial-use-twitters-new-real-time-stream-api-in-python/2/
The tweetstream gem supposedly exposes all methods from the API. However I can't figure out, how to get to those follow requests!
Any ideas?

The tweetstream gem is fairly complete, but sometimes you need to delve into the source to find stuff:
client.on_event(:follow) do |event|
p event[:source][:screen_name]
end
https://github.com/intridea/tweetstream/blob/master/lib/tweetstream/client.rb#L375 :)

Related

Aliasing the Module in Google-API-client Gem

Summarize the problem:
Being relatively new to Ruby/Gems and developing in general, some concepts evade me
I'm learning about the google-api-client Gem, and am attempting to understand the Basic Usage, and want to know how a developer knows which class to use, when instantiating an ojbect, during the "aliasing of the module" portion:
To use an API, include the corresponding generated file and instantiate the service. For example to use the Drive API:
require 'google/apis/drive_v2'
Drive = Google::Apis::DriveV2 # Alias the module
drive = Drive::DriveService.new # why is ::DriveService used here?
#etc
Describe what I've tried:
I've searched through the reference documentation for the google-api-client for a clue about the "decision" to instantiate drive with ::DriveService.new
The best reason I've come up with is: DriveService is instantiated because it is the "BaseService" of the "DriveV2" Class.... but I'm reaching for straws with this logic.
My specific question is:
How does a developer using APIs and this Google-API-client Gem know which object to instantiate?
I have to imagine there's a more elegant "way" to determine which object to instantiate at this point of accessing an API than digging through the documentation of the Gem....I mean...the "BaseService" information is coming from the documentation for this specific Gem.....
Maybe this is a matter of me losing "scope" per say by the Google API and the ambiguously named Gem maintained by Google...
But then again...if I'm using this Gem...then this documentation would always apply, because I wouldn't be able to use this Gem if it wasn't a Google-API....
from the documentation
The link above is the necessary detail regarding Authorization for an API key.
If you're like me, and this subject is new to you, there are three topics that you need to understand:
Authentication
Authorization
Accounting
The documentation for the google-api-client gem is robust enough to answer a lot of questions, however, my answer here is hopefully enough to get you pointed in the right direction.
I'm leaving the question up, in case anyone else needs some guidance regarding this same subject.

Replying to tweets on your timeline with twitter gem

I am trying to create a simple reply from my twitter timeline but ran into some problems and as a ruby newbie I had problems in understanding the doc.
Twitter.mentions_timeline.each do |tweet|
reply_to = tweet.from_user_name()
#Twitter.update("##{reply_to} Not today.")
end
This is what've written so far, which is not much. Unfortunately the tweet doesn't have any from_user_name when using the mentions_timeline it seems. I am using this gem and does not find any good examples of this: https://github.com/sferik/twitter
Would be very nice to have some nice example of how to grab ones tweets from the timeline and replying to those. Also does twitter have anything that distinguish "new tweets" or do I need to create some kind of storage myself to seperate new from old?
Thanks in advance :)
EDIT: to clarify, I want to get all tweets sent to me directly and reply to them. I don't really want to continue in any conversation.
A little late, but in the lastest version of twitter gem you can reply to a tweet with:
#client.update("##{reply_to.user.username} Not today.", in_reply_to_status_id: reply_to.id)
Hope this helps someone.
Here are a few examples that might be helpful for anyone using sferik's twitter gem for something like this. Note, I'm using version 5.0.0 currently, so your mileage may vary.
Assuming you've configured the REST client and assigned it to a variable named client, you can get an array of your last 20 mentions (or however many you've received in the past 2 months, if fewer) with this line:
client.mentions_timeline.map(&:full_text)
The respective mentioners of those tweets can be acquired similarly with this:
client.mentions_timeline.map(&:user).map(&:username)
You could then use client.update as suggested to reply as you see fit.
One tool I find incredibly useful when I'm exploring API's in Ruby is pry. Just require it, drop a binding.pry, and run your code in Terminal. Then you can explore to your heart's — or API's limit's — extent.

Ruby Gems Documentation

I'm just trying to understand how to use particular ruby gems. For example, take this reddit gem. It says to have this code to start:
require 'snoo'
# Create a new instance of the client
reddit = Snoo::Client.new
# Log into reddit
reddit.log_in 'Username', 'Password'
# Send a private message to me (Paradox!)
reddit.send_pm 'Paradox', 'Snoo rubygem rocks!', "Hey Paradox, I'm trying your Snoo rubygem out and it rocks. Thanks for providing such an awesome thing!"
# Log back out of reddit
reddit.log_out
Great but in the documentation you can see that the Client class doesn't have very many exciting functions. The exciting functions are in the Account class but there is no way to get to it...because if I try something like this
reddit = Snoo::Account.new
I get this error:
`initialize': undefined method `new' for Snoo::Account:Module (NoMethodError)
Okay so there's no new method but how do I make an Account object and use its functions like log_in?
Snoo::Account is a Ruby Module, and has been mixed in to Snoo::Client already by the gem. All the functions of Snoo::Account are already available to you on the reddit object.
The synopsis documentation in the readme doesn't make this very clear. But otherwise the documentation on the gem looks good to me.
Taking a short look at the source code on github makes me believe this is a fault in the documentation, as client clearly includes the functionality of many other modules, including the Account module you would like to access. In your example code, try the following methods to confirm it for yourself:
reddit.methods.sort
reddit.is_a? Snoo::Account
I assume the documentation software didn't catch the includes as they were executed using a block.

Ruby on Rails with chargify

I want to integrate chargify to my rails app. I have user object and I want the user to be able to subscribe for one month and update the boolean column on user object. I prefer to use the API not hosted pages. How can I do that?
Is there any example for chargify on ruby on rails for handling subscriptions but with details about mvc for newbies?
Based on this thread and the Googles it looks like there is not a whole lot out there.
You could try looking at the Rails 2 example here and converting it or use the gem here (gem "chargify", "~> 0.3.0").
I know none of this is aimed at newbies but the info seems to sparse.
This might get you going. It seems that Chartify itself is written in Rails, and therfore their API is ruby code, which you can use...

Ruby: How to screen-scrape the result of an Ajax request

I have written a ruby script to screen scrape something using the 'open-uri' and 'hpricot' gems - everything works great so far.
But now I have to screen scrape something which is returned after a form is submitted via a javascript function (called by an 'onchange' event handler from a drop-down menu):
function submit_form() {
document.list.action="/some/sort/of/path";
document.list.submit();
}
AFAIK, open-uri lets you submit only GET requests. And if I'm not mistaken, a POST request would be needed here.
So my question is: what do I need to install and to 'require' and how would the ruby code then look like (to make that POST request) - sorry, I'm still pretty much of a n00b...
Thank you very much for your help!
Tom
I think you definitely should use Mechanize. It provides a nifty interface to interact with remote pages, forms on them, and so forth (see this example).
The Ruby standard library has the http class, which naturally supports the POST operation.
Net::HTTP.post_form(URI.parse('http://www.example.com/some/sort/of/path')
If you find the API there less than optimal, then take a look at the httparty gem
Finally, while hpricot is a great gem, it isn't actively developed any longer. You should consider moving to nokogiri which practically replaces hpricot and improves upon it.

Resources