Last Updated: January 28, 2019
·
2.086K
· weyus

Minimize API calls to Instagram when using instagram-ruby-gem

Assuming that you are using Ruby and instagram-ruby-gem

If you have an individual photo's JSON that was pulled from the "usermediafeed" or similar API method to Instagram, it will contain some likes and comments for that photo. Depending on how many likes and comments there are, it may only contain a subset of them. If that's the case, you will need to make a separate request to Instagram to get all of the likes/comments.

The snippet of code below allows you to make this distinction so that you minimize calls to the Instagram API:

#Only make API calls for likes and comments if necessary
@likes = photo.likes[:count] > photo.likes.data.size ? likes(photo) : photo.likes.data
@comments = photo.comments[:count] > photo.comments.data.size ? comments(photo) : photo.comments.data

where:

photo is the photo JSON

likes(photo) is a method that will call the "media_likes" method in instagram-ruby-gem to get all of the likes for a particular photo

comments(photo) is a method that will call the "media_comments" method in instagram-ruby-gem to get all of the comments for a particular photo