ruby-on-rails – 在Rails应用程序内进行JSON API调用

前端之家收集整理的这篇文章主要介绍了ruby-on-rails – 在Rails应用程序内进行JSON API调用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我们部署了一个wordpress的实例,并安装了一个 JSON API plug in

使用这个插件,你可以做出简单的HTTP GET请求,比如:

http://www.example.org/?json=get_recent_posts
http://www.example.org/?json=get_post&post_id=47
http://www.example.org/?json=get_tag_posts&tag_slug=banana

JSON中还有一个回应:

{
"status": "ok","count": 1,"count_total": 1,"pages": 1,"posts": [
{...}}

我的问题是如何在我的Rails应用程序中进行这些类型的请求(然后解析返回的JSON).我即将开始从头开始写东西,但我想有一些很好的现有工具可以管理这样的东西.基本上,当Ruby包装器尚未可用时,我从未尝试连接到API.你能帮助noob开始吗?

解决方法

看起来像HTTParty可能是你这样的最好的例子.我认为这将是一个简单的方法,您可以轻松地开始处理JSON.

将“httparty添加到您的Gemfile并捆绑.

recent_posts = HTTParty.get "http://www.example.org/?json=get_recent_posts"
puts recent_posts.status,recent_posts.count

recent_posts.posts.each do |post|
  puts post.title #guessing
end
原文链接:https://www.f2er.com/ruby/273652.html

猜你在找的Ruby相关文章