How to reduce external API calls in rails - ruby-on-rails

I have integrated facebook, twitter API's in my rails application for getting hashtag records. Everything is working fine but API calls are taking too long too execute which is really very frustrating. I googled around to get rid of this. There are curl_multi functions available in PHP. Is there something available in rails too?

Not sure about the PHP thing, but if you want to asynchronously process API calls use a queue. Rails provides us with the ActiveJob facade that allows us to use a variety of queues on the back-end. A good tutorial on how to do this can be found here.

Related

Ruby on rails microservices using HTTP explanation needed

So, i currently have a pretty big monolithic rails app. I want to split the app up into separate micro-service APIs, with a main app as the gateway. I've been reading a lot about how to do this and think i understand, however there isn't much info on how the communication between the main app and the micro-services. I understand that i can do this in a variety of ways but i want to use HTTP.
Can someone provide an example of how this setup might look and how the communication works (in code) between the main app and the micro-service?
Thanks
For APIs in Rails, see http://edgeguides.rubyonrails.org/api_app.html. I suggest to use a RESTful API, because REST uses the normal HTTP verbs and fits well to the Rails philosophy, see e.g. https://www.airpair.com/ruby-on-rails/posts/building-a-restful-api-in-a-rails-application

How to use the Appery.io REST API with Ruby on Rails to retrieve information from the Appery.io database?

How can I access the Appery.io (or any future db) that is exposed to the REST API using RoR?
So I have an app i built using Appery.io and I also created a test app using RoR that I would like to use to pull information from the Appery.io db and display it on my RoR app.
I am somewhat familiar with REST and get the idea of what it is doing but I am not to certain on how to connect or make a connection from my RoR app to my Appery.io app. Appery.io has the following documentation for their db api, Appery.io DB API .
I have been looking around and also have seen people mention the following gems for HTTP request:
Weary
HTTParty
RestClient
Would I use one of those? I also read about using Active Resource as a possible solution?
Any help with getting started or a tutorial or article to point me in the right direction would be very helpful.
Thanks!
You won't be establishing an ongoing connection, each request/response will be a single query to your Appery DB. You authenticate those calls using a custom header with API key as defined in the documentation. There's an example using cURL that might be a good place to start playing with the API before you pull it into your RoR app. That example shows you how to get your key, too.
It looks like you can use the predefined APIs, or you can define a custom REST API associated with your Appery app? Instructions for building an API appear to be here.
Once you get the calls working from cURL (or other web request client of your choice), adding the calls to the RoR app should be more straightforward. Any of those gems could probably ease that process: I've only used RestClient personally, but found it very straightforward.
Any of those call methods (cURL, other clients, the gems, etc) will allow you specify your URI, method (e.g. GET or POST), headers, request body (where appropriate), and will allow you to examine your response. Take a look at the gem documentation to see how those map exactly - it will vary slightly from tool to tool.
If you don't have prior experience with calling external APIs, and would like a conceptual explanation, I like this article as a (very short!) beginner's guide.

How to stub external javascript libraries when writing Rails integration tests/specs?

My app interfaces with Google Maps for geocoding and Stripe for payments. Using VCR I've mocked all requests to these services, which works great. But the libraries for both are still being loaded in javascript_include_tags. What's the best way to deal with this so that integration tests can run completely disconnected from the internet?
If you want to stub the javascript, then why record the interactions in the first place? Am I missing something?
I'm pretty sure geocoding from a test is a violation of the google maps ToS. I think what you want to do is stub the geocoding itself.
Have never used Stripe, but I might take this approach: add a method in a spec helper to cache the Stripe javascript locally, i.e., when a connection is available, download the Stripe javascript. Then in your view load the javascript locally if Rails.env.test?.
For anyone looking for a solution these days, there is a nice gem called Puffing Billy.
Similarly to how VCR allows you to record and replay external requests made by your server code, this library allows you to do the same for external requests made by your client code.

Heroku: encapsulate data access in an API

I have a Heroku app with a PostgreSQL DB. Now I want to have a seperate process, possibly on a different machine, to access the DB. The suggestion on the Herkou site is actually what I wanted to do myself:
No, connecting to your database from machines outside of Heroku is not supported.
We recommend that you encapsulate data access in an API to manipulate it.
But I'm not sure how that's done best. I'd like to be able to send some JSON to the API and get back JSON as a result of that request. Like "give me all posts that are expired for a bunch of given userIds" or "update all users to be suspended if their posts contain any of the given words" in an example micropost app.
What's the best way to achieve that? Can I write an extra ruby program that is accessible via TCP and accepts JSON input? Is that even possible with Heroku? Or do I have to integrate the API into my rails app somehow? How?
Thankful for any ideas,
Tom
You will have to integrate the API to your rails app, there are many solutions for this, one of them is using a tool specialized for API building like Grape and mount it with your Rails app. This way you could have your Rails app running and also the API, both sharing the same codebase.

How do I write tests for facebook graph api in my rails app?

I'm using rails 3 and experimenting with the facebook graph api. I'm trying to do things in a TDD/BDD way but I'm not sure how to test my calls to the api. Do I simply wrap the facebook api methods into stubs and return mock objects?
I'm actually faced with a similar problem. I'm looking at https://github.com/mislav/fakeweb as a way of mocking out http responses. Haven't used it for real yet, but based on my preliminary experiments I think it'll work for my purposes. Maybe it'll work for yours too.
i've recently released a utility to help with this. it's a standalone implementation of Facebook's FQL and Graph API that you can run on your development machine.
http://code.google.com/p/mockfacebook/

Resources