在 Ruby 中捕获外部 URL 错误

发布于 2024-12-26 22:34:10 字数 271 浏览 0 评论 0原文

我的脚本的一部分联系 Twitter 的 api 来获取分享数量:

twitter = JSON.parse(open("http://urls.api.twitter.com/1/urls/count.jsonurl=TheURL).read) 

我担心的是,如果 Twitter 宕机或运行缓慢,它会阻止我的网站工作,因为会返回错误。

如何捕获错误,甚至设置一个较短的超时,以便在调用时间过长时可以跳过该调用?

谢谢, 汤姆

Part of my script contacts Twitters api to get the number of shares:

twitter = JSON.parse(open("http://urls.api.twitter.com/1/urls/count.jsonurl=TheURL).read) 

My concern is if Twitter is down or slow, it will stop my site from working as an error is returned.

How can I catch the error or even set a short timeout so I can skip the call if it's taking too long?

Thanks,
Tom

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无力看清 2025-01-02 22:34:10

在 AJAX 请求中连接到 Twitter 可能是个好主意。如果你想在几秒钟后超时,那么你有 Timeout 模块。

It can be a good idea to connect to Twitter in AJAX request. If you want to get timeout after some seconds then you have Timeout module in Ruby.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文