在 Rails 应用程序中的何处放置 Bit.ly API 调用
我计划使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我似乎应该在为给定用户创建新注释时创建短网址。在这种情况下,它会作为
NotesController
中create
操作的结果而发生(通常)。最佳实践建议逻辑责任应采用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 theNotesController
(typically). Best practice would suggest that the logical responsibility should live theNote
model so I would suggest you do thebit.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 yourNote
model