class Insta::API

Attributes

access_token[RW]
next_url[RW]

Public Class Methods

new(access_token) click to toggle source
# File lib/insta.rb, line 61
def initialize(access_token)
  @access_token  = access_token
  @next_url = nil
end

Public Instance Methods

me() click to toggle source
# File lib/insta.rb, line 66
def me
  url = "https://graph.instagram.com/me?fields=account_type,media_count,username,id&access_token=#{access_token}"
  get(url)
end
media(limit = 100) click to toggle source
# File lib/insta.rb, line 71
def media(limit = 100)
  url = "https://graph.instagram.com/me/media?fields=id,media_type,caption,permalink,thumbnail_url,media_url,username,timestamp&access_token=#{access_token}&limit=#{limit}"
  response  = get(url)
  puts response.dig('paging', 'next')
  @next_url = response.dig('paging', 'next') unless response.dig('paging', 'next').nil?
  response
end
next_page() click to toggle source
# File lib/insta.rb, line 79
def next_page
  return {} if next_url.nil?
  response  = get(next_url)
  if response.dig('paging', 'next').nil?
    @next_url = nil
  else
    @next_url = response.dig('paging', 'next')
  end
  response
end

Private Instance Methods

get(url) click to toggle source
# File lib/insta.rb, line 92
def get(url)
  uri = URI.parse(url)
  request = Net::HTTP::Get.new(uri)
  req_options = {
    use_ssl: uri.scheme == "https",
  }
  response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
    http.request(request)
  end
  response = JSON.parse(response.body)
  OpenStruct.new(response)
end