Created
July 17, 2020 14:52
-
-
Save Haumer/b7dd55c4207f7bd928e85e2733ab7581 to your computer and use it in GitHub Desktop.
InstagramClient for the latest 12 posts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'json' | |
require 'http' | |
class InstagramClient | |
attr_reader :posts, :followers | |
def initialize(instagram_handle) | |
@instagram_handle = instagram_handle | |
@url = "https://www.instagram.com/#{@instagram_handle}/?__a=1".freeze | |
@data = JSON.parse HTTP.get(@url).to_s | |
set_posts_and_followers | |
compact_info | |
end | |
def set_posts_and_followers | |
@followers = valid_handle? ? @data["graphql"]["user"]["edge_followed_by"]["count"] : nil | |
@posts = posts_present? ? @data["graphql"]["user"]["edge_owner_to_timeline_media"]["edges"] : [] | |
end | |
def compact_info | |
@posts.map do |post| | |
{ | |
short_code: post["node"]["shortcode"], | |
thumbnail: post["node"]["thumbnail_resources"][1], | |
video: post["node"]["is_video"] | |
} | |
end | |
end | |
private | |
def valid_handle? | |
!@data.empty? | |
end | |
def posts_present? | |
@data["graphql"]["user"]["edge_owner_to_timeline_media"]["edges"].any? && valid_handle? | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment