Facebook API - Using multiple requests in Rails Model - ruby-on-rails

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

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

How to get and display API like the facebook graph in rails

I've went through most of the beginner rails books and I want to try creating something on my own. For a start, I just want to create a few pages in rails that will get from Facebook's api and display something like username, likes, post. I've searched around and couldn't find an answer. My friend recommended that I use a gem called fb_graph, but reviewing the documentation, I have no clue how to use it.
Thanks stackoverflow!
You can query the Graph API directly. The responses will be in JSON which you can then parse into Ruby hash. See the Facebook Documentation for more details on to call specific and sample JSON responses. So here is general guide how to you can start playing around with this:-
Make a API Call to Facebook using Graph API
Explorer. Keep playing around with api until you get a response you want. Note the request params you passed to get that response & JSON you received from facebook.
Send a HTTP request containing those same params in rails using koala, 'fb_graph' or just plain NET:HTTP. It doesn't matter what client you use to sent the request, as long as you send same params as in step1, you will get that familar JSON as response.
Now, once you have the json, just have to parse it. Provided, if the client library is not already doing that as most fb gems will turn JSON into ruby objects/hashes. But if they don't, then you have to do it manually, its something like JSON.parse('JSON_RESPONSE_AS_STRING_GOES_HERE'). after this you will have a plain-old ruby hash which is you can save to db, display in view or whatever you want to do.
Hope it helps

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 Ruby Gem Too many requests

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.

Posting multipart form data with Ruby

I'm building a rails app that interacts with a 3rd party API
When a user uploads a file to rails, it should be forwarded on to the 3rd party site via an HTTP POST.
In some cases, the upload can be several hundred MBs.
At the moment, I've just been re-posting to the API using Net::HTTP and accessing the multipart form object like so
#tempfile = params[:video][:file_upload].tempfile
This is hella slow though and feels kinda dirty.
Is there a better way to do this?
Is it possible to have the user post directly to the 3rd party service or do you have to handle the API through your Rails stack? Ideally you would be able to do this and would not have to load the file into your stack and then re-post it to the API. If you can't post directly, I would recommend seeing if the API has a streaming service so that you can send parts of the file instead of the entire thing at once. Either way I think you'll start running into Timeout errors on your side and on the API side with large files, so you'll have to increase your own timeouts or create a different type of streaming file uploader.
Spin up a background job using DelayedJob. In the delayed job, you could try rails redirect_to.
https://github.com/tobi/delayed_job
http://apidock.com/rails/ActionController/Base/redirect_to

Resources