class Instagrammer::Post

Constants

IMAGE_URLS_RE

Attributes

image_url[R]
image_urls[R]
shortcode[R]

Public Class Methods

new(shortcode) click to toggle source
# File lib/instagrammer/post.rb, line 9
def initialize(shortcode)
  @shortcode = shortcode
  @data = nil
end

Public Instance Methods

caption() click to toggle source
# File lib/instagrammer/post.rb, line 46
def caption
  data["caption"]
end
comment_count() click to toggle source
# File lib/instagrammer/post.rb, line 54
def comment_count
  data["commentCount"].to_i
end
data() click to toggle source
# File lib/instagrammer/post.rb, line 19
def data
  get_data unless @data

  case @status
  when :private then raise Instagrammer::PrivatePost.new("Private post: #{@shortcode}")
  when :not_found then raise Instagrammer::PostNotFound.new("Post not found: #{@shortcode}")
  when :invalid then raise Instagrammer::PostInvalid.new("Post invalid: #{@shortcode}")
  else @data
  end
end
like_count() click to toggle source
# File lib/instagrammer/post.rb, line 58
def like_count
  data["interactionStatistic"]["userInteractionCount"].to_i if photo?
end
photo?() click to toggle source
# File lib/instagrammer/post.rb, line 34
def photo?
  type == :photo
end
public?() click to toggle source
# File lib/instagrammer/post.rb, line 14
def public?
  get_data unless @data
  @status == :public
end
type() click to toggle source
# File lib/instagrammer/post.rb, line 30
def type
  data["@type"] == "ImageObject" ? :photo : :video
end
upload_date() click to toggle source
# File lib/instagrammer/post.rb, line 50
def upload_date
  DateTime.parse data["uploadDate"]
end
user() click to toggle source
# File lib/instagrammer/post.rb, line 42
def user
  Instagrammer::User.new data["author"]["alternateName"]
end
video?() click to toggle source
# File lib/instagrammer/post.rb, line 38
def video?
  type == :video
end
watch_count() click to toggle source
# File lib/instagrammer/post.rb, line 62
def watch_count
  data["interactionStatistic"]["userInteractionCount"].to_i if video?
end

Private Instance Methods

get_data() click to toggle source
# File lib/instagrammer/post.rb, line 67
def get_data
  visit "https://www.instagram.com/p/#{@shortcode}/"
  @status = get_page_status

  if @status == :public
    node = page.first(:json_ld, visible: false)
    @data = JSON.parse(node.text(:all))
    set_image_attributes if photo?
  end
end
set_image_attributes() click to toggle source
# File lib/instagrammer/post.rb, line 79
def set_image_attributes
  node = page.find(:image)
  @image_url = node["src"]
  @image_urls = node["srcset"].scan(IMAGE_URLS_RE).collect do |match|
    {
      url: match[0],
      width: match[1].to_i
    }
  end
end