Twitter Ruby Gem Too many requests - ruby-on-rails

I'm currently using TDD with rspec and Ruby on Rails (with twitter gem) to pull tweets and trends for analytics. However, I constantly get Twitter::Error::TooManyRequests and have to sit around twiddling thumbs till I can make another request. Is this a limitation of Twitter search API or am I doing something wrong?
Right now I have a simple index page that invokes Twitter.config once and follows it with Twitter.trends once. Running rspec spec on this to generate one index page results in the previously mentioned error. Am I missing something here? I can't find anywhere (application, framework, or full traces) where the twitter requests are made in abundance.
I'm not using the stream API just yet because it only provides raw tweets and not trending information.

Twitter has a rate limit of something like 150 requests when not authenticated and 250 requests for authenticated users.
Perhaps you could figure a way to gather your data in chunks instead of constant calls.

You may want check out the VCR gem which can record HTTP interactions so that you're not continually hitting the external service.

Related

In a Rails API, is there a standard way to "paginate" or "chunk" large API requests responses?

Scenario:
I have a Rails API with a /tasks.json endpoint that serves all the Tasks.
For large Accounts this can sometimes be close to 1,000,000 records.
Sending 1,000,000 records over the wire in JSON generally doesn't work due to a network timeout, etc.
I can easily "paginate" the number of tasks being sent so the endpoint uses something like /tasks.json?limit=10000&page=1 but how does the client know to send this?
Is there a "standard" way to handle large requests like this and break them up naturally into chunks?
Does each client need to handle this on their end manually?
Thanks!
You should use kaminari gem. It handles all requests and paginates it. It works both on Rails Api apps and rails standard apps.
https://github.com/kaminari/kaminari

Rails Google analytics from the controller

Is there a way, or a gem, that can send information to Google Analytics without the page load occurring?
I have a URL shortener, that redirects to the original URL (obviously), but I would like to track who clicked it.
Is there a way to send Google Analytics the request/headers or whatever it needs, from the controller, just prior to the redirect, without them having to actually load a page?
You can use the measurement protocol to do that. There may be a gem that wraps the functionality, but it's pretty basic as is. Essentially, you're just sending sending HTTP hits to the GA servers with your data in the query parameters.
Here are the docs:
https://developers.google.com/analytics/devguides/collection/protocol/

How to get results with twitter search api?

I wish to get tweets with a keyword. But There is no result with any keyword.
http://search.twitter.com/search.json?q=summer
How to get results with Twitter Search API?
Version 1 of the twitter API has been deprecated and is being removed. Not sure how you can miss the giant warnings on the twitter dev site ;) This means simple code like the above will not work any more.
So, you now need to make authenticated requests (OAuth) using the 1.1 API, and it's nowhere near as simple as just doing a (in PHP) file_get_contents(http://search.twitter.com/ ...).
I couldn't see any server-side languages you use from your profile, but I wrote a lengthy post explaining the issue (with pictures) and how to use a php library to perform authenticated requests.

Twitter Api giving only tweets of Last Couple of days

Recently I implemented OAuth in our Ruby on Rails application with OAuth gem and started using it.
Technology :- Ruby On Rails
Before Implementing OAuth, I use to call the below URL it use to give the tweets properly,
http://search.twitter.com/search.json?q=moxie&count=100
But, After Implementing Oauth, I am calling the Below URL :-
https://api.twitter.com/1.1/search/tweets.json?q=moxie&count=100
Which is not giving the Tweets properly, i.e the result set is containing only Tweets of last couple of Days.
How to retrieve tweets from last two months or more than that?
Thanks in advance
check your api request limit, o try to split your request, by 50 tweats

Facebook API - Using multiple requests in Rails Model

I have written an app that I am not happy with. Essentially it queries the Facebook API in a few ways:
Gets a list of all posts
Retrieves each post
Retrieves each posts comments
I can get a list of all posts in 1 API request, but I've nested each subsequent API request in a loop. I'd like to do this all at once, and is there any reading on sending all the requests at once, and then handling the API requests as they come in?
I'm looking for reading, GEMS, suggestions at strategies.
Thanks!
Take a look at the answers here: What is the preferred way of performing non blocking I/O in Ruby?
Good options seems to be:
Typhoeus
EventMachine + em-http-request

Resources