3

I have a rails app that calls a third party API for weather.

The problem is that the API call is generally very slow and sometimes fails. Showing the weather is not a necessity but it adds a nice bit of extra and pertinent information.

Right now I call the Wunderground API using Barometer gem in the controller which means the pages takes forever to load if the API is slow or fails.

I was hoping to move to this call to an AJAX call from the page once the page is loaded. I don't mind if the information shows but a bit delayed because as mentioned it is not hugely important.

I was just curious the best practices for making such a call? What is the Rails way?

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Macgill4444
  • 244
  • 1
  • 12

2 Answers2

7

The recommended way is to call to the API in the background (using a scheduler) and save the result in the database. Then in the controller you can get the data from the database and there won't be any delay.

Mischa
  • 42,876
  • 8
  • 99
  • 111
  • 1
    Calling the API asynchronously is the way to go, but using a scheduler to save the result to the database? This would only be possible if he knew some user data before-hand, in this case the user's location. You'd also need to be running that scheduler every few minutes since weather data is time-sensitive. An AJAX call is much more appropriate, either directly to the API or to another route in your app that queries the API – Mario May 07 '15 at 15:41
  • 2
    Well, it's unclear what kind of weather information he is showing. If it's e.g. a local paper that shows the local weather forecast then a scheduled background job would be fine. If he is indeed showing different info for different users then I agree that a background job probably wouldn't work. – Mischa May 07 '15 at 15:45
  • Thanks, I hadn't considered that. You're right that just updating the weather data as a background job would be the way to go if he's showing everyone the same location's weather – Mario May 07 '15 at 15:47
3

I would say that you are quite correct in moving to an AJAX call from the browser- that way your page load is unaffected and it can take as long as it likes without your server having to wait on it. This is a classic case for loading the data asynchronously ( through callbacks and/or jQuery's deferredapproach ) so that everything else is available while the data loads and your users aren't waiting on some information that they might not be very interested in to start with.

In terms of keeping it Rails, your main consideration is whether you can and/or want to make the call directly from the browser to the service, or whether you want to proxy it through your application to some degree, which would save on potential cross-domain request problems. Again this is very much your decision and will depend on whether you have any API keys you need to transmit with requests and so on, but if the request can run directly from the user to the weather API then that would allow you to cut out the intermediate step on your part.

glenatron
  • 11,018
  • 13
  • 64
  • 112
  • 2
    I'd also like to add that sometimes it's a good idea to [cache your API calls](https://robots.thoughtbot.com/caching-api-requests) – Mario May 07 '15 at 15:36
  • 2
    It is indeed, which would be an advantage of running the call through your own service as you can hold onto data. It depends a lot on the user cases, though- if there are likely to be few users or few requests, the benefits are somewhat limited. – glenatron May 07 '15 at 15:53