Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tumblr API with Ruby

I'm brand new to ruby and am currently building a rails application that needs to pull from a Tumblr blog. I have read through several questions here, online tutorials and Tumblr's API docs and after two weeks, I'm stuck.

https://www.tumblr.com/docs/en/api/v2

I make the call using Tumblr in my controller

require 'json'
require 'tumblr_client'
client = Tumblr::Client.new :consumer_key => 'consumerkey'
@post = client.posts 'crossfitinterchange.tumblr.com', :type => 'text', :limit => 1, :filter => 'html'

Then this is returned (short snippet)

{"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&quot;)</p>\n\n<p><br/>\nScore is total Thrusters + Box Jump Overs</p>", ...

In my view I have

<% @post.each do |p| %>
<%= p.title %>
<%= p.body %>
<% end %>

I get an error that title and body are undefined. I'm getting the JSON returned via the API, now how do I grab the title and body of the post?

like image 855
Nick Seferos Avatar asked Aug 31 '26 11:08

Nick Seferos


1 Answers

Your @post object contains JSON data. So, you don't need to loop through that i.e. no need to use .each on that object.

To grab the title and the body from the @post JSON object, you should do:

title = @post['blog']['title']
body = @post['blog']['body']
like image 130
K M Rakibul Islam Avatar answered Sep 03 '26 01:09

K M Rakibul Islam



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!