Skip to content

Instantly share code, notes, and snippets.

@breim
Created December 4, 2019 21:16
Show Gist options
  • Save breim/102d0cd949e2afce47cfa77dea59f4e0 to your computer and use it in GitHub Desktop.
Save breim/102d0cd949e2afce47cfa77dea59f4e0 to your computer and use it in GitHub Desktop.
# frozen_string_literal: true
class Instagram
include HTTParty
base_uri 'https://graph.facebook.com/v4.0'
def initialize(access_token)
@options = { query: { access_token: access_token } }
end
def instagram_business_accounts
response = self.class.get("/me/accounts?fields=instagram_business_account{name,username,followers_count,follows_count,\
ig_id,media_count,profile_picture_url}", @options)
response['data'].map { |account| account['instagram_business_account'] }.compact!
end
def metadata(instagram_business_account_id)
self.class.get("/#{instagram_business_account_id}?fields=name,biography,followers_count,follows_count,id,\
ig_id,media_count,username,profile_picture_url,website", @options)
end
def medias(instagram_business_account_id, _fields = nil)
self.class.get("/#{instagram_business_account_id}/media?fields=caption,comments_count,id,ig_id,\
is_comment_enabled,like_count,media_type,media_url,\
permalink,owner,shortcode,thumbnail_url,timestamp,username,\
comments&limit=22", @options)
end
def media(media_id, fields)
self.class.get("/#{media_id}?fields=#{fields}", @options)
end
def insights_media(media_id)
self.class.get("/#{media_id}/insights?metric=engagement,impressions,reach", @options)
end
def stories(instagram_business_account_id, fields = nil)
self.class.get("/#{instagram_business_account_id}/stories?fields=#{fields}", @options)
end
def insights(instagram_business_account_id, metrics, period, start_date = nil, end_date = nil)
url = "/#{instagram_business_account_id}/insights?metric=#{metrics}&period=#{period}"
url += "&since=#{start_date.to_i}&until=#{end_date.to_i}" unless start_date.nil? && end_date.nil?
self.class.get(url, @options)
end
def business_discovery(instagram_business_account_id, username)
self.class.get("/#{instagram_business_account_id}?fields=business_discovery.username(#{username})"\
'{followers_count,following_count,media_count,media{comments_count,follows_count,like_count}}', @options)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment