Tumblr API 和 Ruby
Tumblr API with Ruby
我是 ruby 的新手,目前正在构建一个需要从 Tumblr 博客提取的 rails 应用程序。我已经阅读了这里的几个问题、在线教程和 Tumblr 的 API 文档,两周后,我被卡住了。
https://www.tumblr.com/docs/en/api/v2
我在控制器中使用 Tumblr 进行调用
require 'json'
require 'tumblr_client'
client = Tumblr::Client.new :consumer_key => 'consumerkey'
@post = client.posts 'crossfitinterchange.tumblr.com', :type => 'text', :limit => 1, :filter => 'html'
然后返回(短片段)
{"blog"=>{"title"=>"Today's Workout", "name"=>"crossfitinterchange", "posts"=>118, "url"=>"http://crossfitinterchange.tumblr.com/", "updated"=>1444971275, "description"=>"", "is_nsfw"=>false, "ask"=>false, "ask_page_title"=>"Ask me anything", "ask_anon"=>false, "share_likes"=>true, "likes"=>0}, "posts"=>[{"blog_name"=>"crossfitinterchange", "id"=>131266609424, "post_url"=>"http://crossfitinterchange.tumblr.com/post/131266609424/friday-oct-16th-wod", "slug"=>"friday-oct-16th-wod", "type"=>"text", "date"=>"2015-10-16 04:54:35 GMT", "timestamp"=>1444971275, "state"=>"published", "format"=>"html", "reblog_key"=>"OWmgAbMO", "tags"=>[], "short_url"=>"http://tmblr.co/ZE0_uk1wG6O4G", "recommended_source"=>nil, "recommended_color"=>nil, "highlighted"=>[], "note_count"=>0, "title"=>"Friday Oct 16th WOD", "body"=>"<p>[STRENGTH]</p>\n\n<p>5 x 3 Push Press to find 3RM</p>\n\n<p>* complete 5 ring rows after each set</p>\n\n<p><br/>\n[CONDITIONING]</p>\n\n<p>7 mins EMOM:</p>\n\n<p>5 sit ups<br/>\nME Thrusters (115/80#) for duration of round</p>\n\n<p><br/>\n- REST 2 mins -</p>\n\n<p><br/>\n2 mins AMRAP Box Jump Overs (24/20")</p>\n\n<p><br/>\nScore is total Thrusters + Box Jump Overs</p>", ...
在我看来我有
<% @post.each do |p| %>
<%= p.title %>
<%= p.body %>
<% end %>
我收到标题和 body 未定义的错误。
我正在通过 API 返回 JSON,现在如何获取 post 的标题和 body?
您的 @post
对象包含 JSON 数据。所以,你不需要遍历它,即不需要在那个对象上使用 .each
。
要从 @post
JSON 对象中获取 title
和 body
,您应该这样做:
title = @post['blog']['title']
body = @post['blog']['body']
我是 ruby 的新手,目前正在构建一个需要从 Tumblr 博客提取的 rails 应用程序。我已经阅读了这里的几个问题、在线教程和 Tumblr 的 API 文档,两周后,我被卡住了。
https://www.tumblr.com/docs/en/api/v2
我在控制器中使用 Tumblr 进行调用
require 'json'
require 'tumblr_client'
client = Tumblr::Client.new :consumer_key => 'consumerkey'
@post = client.posts 'crossfitinterchange.tumblr.com', :type => 'text', :limit => 1, :filter => 'html'
然后返回(短片段)
{"blog"=>{"title"=>"Today's Workout", "name"=>"crossfitinterchange", "posts"=>118, "url"=>"http://crossfitinterchange.tumblr.com/", "updated"=>1444971275, "description"=>"", "is_nsfw"=>false, "ask"=>false, "ask_page_title"=>"Ask me anything", "ask_anon"=>false, "share_likes"=>true, "likes"=>0}, "posts"=>[{"blog_name"=>"crossfitinterchange", "id"=>131266609424, "post_url"=>"http://crossfitinterchange.tumblr.com/post/131266609424/friday-oct-16th-wod", "slug"=>"friday-oct-16th-wod", "type"=>"text", "date"=>"2015-10-16 04:54:35 GMT", "timestamp"=>1444971275, "state"=>"published", "format"=>"html", "reblog_key"=>"OWmgAbMO", "tags"=>[], "short_url"=>"http://tmblr.co/ZE0_uk1wG6O4G", "recommended_source"=>nil, "recommended_color"=>nil, "highlighted"=>[], "note_count"=>0, "title"=>"Friday Oct 16th WOD", "body"=>"<p>[STRENGTH]</p>\n\n<p>5 x 3 Push Press to find 3RM</p>\n\n<p>* complete 5 ring rows after each set</p>\n\n<p><br/>\n[CONDITIONING]</p>\n\n<p>7 mins EMOM:</p>\n\n<p>5 sit ups<br/>\nME Thrusters (115/80#) for duration of round</p>\n\n<p><br/>\n- REST 2 mins -</p>\n\n<p><br/>\n2 mins AMRAP Box Jump Overs (24/20")</p>\n\n<p><br/>\nScore is total Thrusters + Box Jump Overs</p>", ...
在我看来我有
<% @post.each do |p| %>
<%= p.title %>
<%= p.body %>
<% end %>
我收到标题和 body 未定义的错误。 我正在通过 API 返回 JSON,现在如何获取 post 的标题和 body?
您的 @post
对象包含 JSON 数据。所以,你不需要遍历它,即不需要在那个对象上使用 .each
。
要从 @post
JSON 对象中获取 title
和 body
,您应该这样做:
title = @post['blog']['title']
body = @post['blog']['body']