Rails的第三方API调用的最佳实践? [英] Best practice for Rails third party API calls?

查看:117
本文介绍了Rails的第三方API调用的最佳实践?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Rails应用程序,要求气象第三方API。

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

的问题是,所述API调用通常是非常慢的,有时甚至会失败。 显示天气不是必需品,但它增加了额外的和相关的信息,一个不错位。

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.

目前使用晴雨表的宝石在控制器中,这意味着该网页需要永远载入如果API慢或失败我称之为Wunderground API。

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.

我希望从页面一旦加载页面移到此呼叫AJAX调用。我不介意,如果信息显示,但有点延迟,因为提到它不是非常重要的。

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.

我只是好奇,对发出这种呼吁的最佳实践?什么是Rails的方法是什么?

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

推荐答案

推荐的方法是调用在后台的API(使用一个调度程序),并将结果保存在数据库中。然后在控制器就可以得到从数据库中的数据,并不会有任何的延迟。

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.

这篇关于Rails的第三方API调用的最佳实践?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆