shinjin
shinjin

Reputation: 364

How to pull Instagram image caption text using the Instagram API?

I'm using the Instagram api to build a site in Ruby on Rails. I can't seem to figure out how to pull caption text with any of the images, and it doesn't seem to state how to do this anywhere in the documentation. Is it possible using the api or do I have to use something else?

I've attached 2 very simple code snippets to illustrate my problem below

Ruby:

class HomeController < ApplicationController
  def index
    def index
        @instagram = Instagram.tag_recent_media("blablabla", {:count => 50})
    end
  end
end

Here is the HTML:

  <div class = "tagged-images-container-position">
    <% @instagram.each do |instagram| %>
            <%= instagram.caption.text %> <!-- THIS LINE DOESN'T WORK -->
            <%= instagram.user.username %> 
            <%= image_tag(instagram.images.standard_resolution.url, class: "tagged-images") %>
    <% end %>
  </div>

Upvotes: 1

Views: 3366

Answers (2)

wrydere
wrydere

Reputation: 678

Specifically, check the entire caption to see if it is nil, before digging down to caption.text

This technique has been working for me:

if instagram.caption
  caption = instagram.caption.text
else
  caption = "Oops, no caption."
end

Upvotes: 1

S&#233;bastien Richer
S&#233;bastien Richer

Reputation: 1691

Try checking the caption text for null before going for that text propoerty. I found that sometimes it pops up as null.

Upvotes: 1

Related Questions