3
我是ruby的新手,目前正在構建一個需要從Tumblr博客中提取的rails應用程序。我已經在這裏閱讀了幾個問題,在線教程和Tumblr的API文檔,兩週後,我卡住了。Tumblr API with Ruby
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 %>
我收到一個標題和正文未定義的錯誤。 我收到了通過API返回的JSON,現在我該如何獲取帖子的標題和正文?
感謝您的快速反應!我放在上面,我被給了一個TypeError'沒有將字符串隱式轉換爲整型'。我需要解析JSON嗎? –
我以爲'@ post'已經是JSON了嗎?如果沒有,那麼是的,使用'.to_json'調用將其設爲json對象,然後嘗試我上面的建議。它應該工作。如果不是,請告訴我! –
它應該根據API文檔。我讀過的其他一些問題說它是JSONP或某種形式的JSONP。 –