在 Rails 应用程序中的何处放置 Bit.ly API 调用

发布于 2024-09-08 09:50:43 字数 733 浏览 5 评论 0原文

我计划使用 Bit.ly Pro 和 Bit.ly API 在 Rails 3 项目中制作我自己的短网址。

我有一个用户和一个注释模型。 url 结构如下:'/username/1-note-title'。

现在我想给每个笔记一个简短的网址。但我不知道应该从哪里进行 API 调用。现在我在注释控制器中得到了这段代码,但我不知道这是否是正确的位置,也不知道如何获取特定注释的 url...

url = ???

parsed_json = JSON('http://api.bit.ly/v3/shorten?login=bitlyapidemo&apiKey=R_0da49e0a9118ff35f52f629d2d71bf07&longUrl=' + url + '&format=json')

@short_url = parsed_json["data"]["url"]

JSON 对象结构仅供参考:

{
  "status_code": 200, 
  "data": {
    "url": "http://bit.ly/cmeH01", 
    "hash": "cmeH01", 
    "global_hash": "1YKMfY", 
    "long_url": "http://betaworks.com/", 
    "new_hash": 0
  }, 
"status_txt": "OK"
}

需要帮助,提前致谢!

I'm planning on using Bit.ly Pro and the Bit.ly API to make my own short urls in a Rails 3 project.

I've got a User and a Note model. And a url structure like this: '/username/1-note-title'.

Now I would like to give each note a short url. But I don't know from where I should do the API call. Right now I got this code in the Note controller but I don't know if that's the right place or how to get the url of the specific note...

url = ???

parsed_json = JSON('http://api.bit.ly/v3/shorten?login=bitlyapidemo&apiKey=R_0da49e0a9118ff35f52f629d2d71bf07&longUrl=' + url + '&format=json')

@short_url = parsed_json["data"]["url"]

The JSON object structure just for reference:

{
  "status_code": 200, 
  "data": {
    "url": "http://bit.ly/cmeH01", 
    "hash": "cmeH01", 
    "global_hash": "1YKMfY", 
    "long_url": "http://betaworks.com/", 
    "new_hash": 0
  }, 
"status_txt": "OK"
}

Help wanted, thanks in advance!

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

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

发布评论

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

评论(1

扛起拖把扫天下 2024-09-15 09:50:43

我似乎应该在为给定用户创建新注释时创建短网址。在这种情况下,它会作为 NotesControllercreate 操作的结果而发生(通常)。最佳实践建议逻辑责任应采用 Note 模型,因此我建议您在保存回调中执行 bit.ly 缩短,无论是之前还是之后,具体取决于关于存在缩短的 URL 的重要性(在您的特定应用程序的上下文中)。

挑战在于处理错误情况,即 bit.ly 服务根本无法响应您的缩短请求,或者响应时间过长。此时将其放入回调中可能没有意义,因为在尝试满足请求时它可能会占用您的应用程序。

如果您不需要实时 URL 缩短,那么您可以考虑将缩短请求创建为 后台进程异步完成(必要时重试),并在 Note 模型中的上述 after_save 回调中触发

I seems that the short url should get created when a new note is created for a given user. In that context it would happen as the result of a create action in the NotesController (typically). Best practice would suggest that the logical responsibility should live the Note model so I would suggest you do the bit.ly shortening implemented in a save callback, either before or after, depending on how critical it is (in the context of your particular app) for a shortened URL to exist.

The challenge is do deal with the error case which is when the bit.ly service is unable to respond to your shortening request at all or is taking too long in so doing. That's when putting it in the callback may not make sense as it could potentially tie up your application when trying to fulfill the request.

If you don't need live URL shortening then you could consider creating shortening requests as queued jobs in a background process to be done asynchronously (retrying as necessary) and be triggered in the aforementioned after_save callback in your Note model

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