Skip to content

Instantly share code, notes, and snippets.

@alexbaldwin
Created March 4, 2015 17:35
Show Gist options
  • Save alexbaldwin/1c9ce0291c003b0450b5 to your computer and use it in GitHub Desktop.
Save alexbaldwin/1c9ce0291c003b0450b5 to your computer and use it in GitHub Desktop.
get "/user_media_feed" do
client = Instagram.client(:access_token => session[:access_token])
user = client.user
html = "<h1>#{user.username}'s media feed</h1>"
page_1 = client.user_media_feed(777)
page_2_max_id = page_1.pagination.next_max_id
page_2 = client.user_recent_media(777, :max_id => page_2_max_id ) unless page_2_max_id.nil?
html << "<h2>Page 1</h2><br/>"
for media_item in page_1
html << "<img src='#{media_item.images.thumbnail.url}'>"
end
html << "<h2>Page 2</h2><br/>"
for media_item in page_2
html << "<img src='#{media_item.images.thumbnail.url}'>"
end
html
end
@croaky
Copy link

croaky commented Mar 5, 2015

@alexbaldwin Maybe something like this?

get "/user_media_feed" do
  client = Instagram.client(access_token: session[:access_token])
  user = client.user
  html = "<h1>#{user.username}'s media feed</h1>"
  html << get_media(client)
  html
end

def get_media(client, html = "", min_id = nil)
  items = client.user_recent_media(min_id: min_id)

  items.each do |item|
    html << "<img src='#{item.images.thumbnail.url}'>"
    min_id = item.id
  end

  if min_id
    get_media(client, html, min_id)
  else
    html
  end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment