Replying to tweets on your timeline with twitter gem - ruby

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.

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.

RubyGem twitter direct message text

I start the Ruby and I want to do a bot who take the text of direct message received and tweet it. I use the twitter gem and I have no idea how to do it. Can someone help me ?
Tanks a lot
Aurélien
Good News! The twitter API allows for reading private messages.
The gem provides documentation on recieving a list of messages here.
Your call would look something like this:
direct_messages_received(options = {:count => 10})
This will return the 10 most recent DMs received.

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

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 :)

Is there a template for a website that accepts an uploaded file, does something, and lets the user download the result?

I have a few Ruby scripts that process text files in different ways, that many of my friends find useful. However, most of the people I know are not comfortable running scripts on the command line. The easiest thing for them would be to create a simple webpage where people could upload a file, select a few options, have it processed, and then download the result.
I know it wouldn't be too hard to build something like this in Rails or Merb or something like that, however it seems like a very common problem, so I was wondering if there was already some kind of template, or similar application that I could easily modify, i.e. let the user upload a file, choose a few options, then {fill in code to do something with file}, let the user download the resulting file?
In the past I used Carrierwave to upload user avatars.
If you are used to Rails it's really straightforward.
Let it be a TextFile resource:
gem 'carrierwave'
$ rails g scaffold textfile content:string title:string etc etc
$ rails g uploader textfile
class TextFile < ActiveRecord::Base
attr_accesible :content
mount_uploader :content, TextFileUploader
end
And that is is pretty much all you have to do to obtain the app's skeleton. However, to answer your real question, no, I don't think there is already a rails app that does exactly that.
https://github.com/jnicklas/carrierwave
I found sinatra-fileupload, which pretty perfectly answers my question. It's a very minimalistic framework which works perfectly, I can just plug in the file handling, and change the layout etc a bit. There were many examples of sophisticated Rails plugins linked to databases, with versioning and stuff, but I really wanted the most minimal example.

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...

Resources