将数据从api请求保存到模型(JSON)

时间:2013-01-02 15:13:15

标签: ruby-on-rails ruby-on-rails-3 json api model

好的,所以在过去一周左右的时间里,我一直在处理api调用,然后将响应作为JSON返回,然后将我想要的部分输出到我的视图中(我认为,尽管很慢)。我的学习曲线的下一部分是获取这些数据并将其保存到模型中,以便我可以在我的应用程序的其他地方使用它。

我想要实现的是在完成api请求并显示结果后我想点击一个按钮,然后将数据发布到我的模型

在这个例子中,我通过itunes api的ISBN号获取书籍数据

这是返回数据的示例

 {
 "resultCount":1,
  "results": [
 {"kind":"ebook", "artistId":545975179, "artistName":"Gareth Halfacree", "price":9.99, 
 "description":"<p><b>Make the most out of the world&rsquo;s first truly compact  computer<\/b><\/p><p>It's the size of a credit card, it can be charged like a smartphone,  it runs on open-source Linux, and it holds the promise of bringing programming and playing  to millions at low cost. And now you can learn how to use this amazing computer from its co-creator, Eben Upton, in <i>Raspberry Pi User Guide<\/i>. Cowritten with Gareth Halfacree, this guide gets you up and running on Raspberry Pi, whether you're an educator, hacker, hobbyist, or kid. Learn how to connect your Pi to other hardware, install software, write basic programs, and set it up to run robots, multimedia centers, and more.<\/p><ul><li>Gets you up and running on Raspberry Pi, a high-tech computer the size of a credit card <\/li><li>Helps educators teach students how to program <\/li><li>Covers connecting Raspberry Pi to other hardware, such as monitors and keyboards, how to install software, and how to configure Raspberry Pi <\/li><li>Shows you how to set up Raspberry Pi as a simple productivity computer, write basic programs in Python, connect to servos and sensors, and drive a robot or multimedia center <\/li><\/ul><p>Adults, kids, and devoted hardware hackers, now that you've got a Raspberry Pi, get the very most out of it with <i>Raspberry Pi User Guide<\/i>.<\/p>", "genreIds":["10017", "38", "9027"], "releaseDate":"2012-08-30T07:00:00Z", "currency":"USD", "genres":["Computers", "Books", "Computers & Internet"], "trackId":559783692, "trackName":"Raspberry Pi User Guide",  "artistIds":[545975179],  "artworkUrl60":"http://a2.mzstatic.com/us/r30/Publication/v4/ba/a8/2c/baa82ce0-2ac7-7026-04da-6f74bc97b403/9781118464496.60x60-50.jpg", "artistViewUrl":"https://itunes.apple.com/us/artist/gareth-halfacree/id545975179?mt=11&uo=4", "trackCensoredName":"Raspberry Pi User Guide", "formattedPrice":"$9.99", "artworkUrl100":"http://a4.mzstatic.com/us/r30/Publication/v4/ba/a8/2c/baa82ce0-2ac7-7026-04da-6f74bc97b403/9781118464496.100x100-75.jpg", "trackViewUrl":"https://itunes.apple.com/us/book/raspberry-pi-user-guide/id559783692?mt=11&uo=4", "averageUserRating":2.5, "userRatingCount":5}]
}

我想保存artistName,Description,图片(我需要回形针吗?)和trackName

任何人都可以提供一些关于我如何进行此操作的建议,显然我创建了一个模型并设置了表格列(我知道列名称可以是任何东西吗?)但是在此之后我有点失落

如果有人能提供一个很好的例子,那么我就可以按照这个过程理解发生了什么

任何帮助表示赞赏

1 个答案:

答案 0 :(得分:2)

一个选项是在控制器上创建before_filter,您可以列出您希望它执行的控制器操作,即。您的API调用。

在从过滤器调用的方法中,您可以将请求的详细信息保存到模型中。这样就可以避免手动保存信息。

基于示例响应的图像具有图像的URL,因此您可以将其保存到模型中,前提是您确信图像URL不会更改。如果您想保存图像本身,是的,我会推荐像paperclip或carrierwave这样的东西。

如果您不想这样做,那么另一种选择,在我看来,不那么整洁的方式是将JSON发回一个按钮,然后执行我上面提到的控制器操作。

编辑:因此,为了保存模型,例如名为ApiCall,您可以在每次执行控制器操作时创建新的数据库条目。假设您的控制器操作被称为get_info并且它传递了params,使您能够构建您所描述的JSON响应。你可以做类似下面的事情。

json_response = JSON.parse(your_response_object)
ApiCall.create(:artist_name => json_response["results"]["artistName"])

您显然可以包含您拥有的任何/所有信息。或者,您可以将整个响应作为YAML字符串存储在一个db属性中,并在检索时解析信息,由您决定。

yaml = your_response_object.to_yaml
ApiCall.create(:payload => yaml)