class Instagrammer::User

Constants

META_RE
SHORTCODE_RE

Attributes

posts[R]

Public Class Methods

new(username) click to toggle source
# File lib/instagrammer/user.rb, line 9
def initialize(username)
  @username = username.delete_prefix("@")
  @data = nil
  @posts = []
end

Public Instance Methods

avatar() click to toggle source
# File lib/instagrammer/user.rb, line 61
def avatar
  data["image"]
end
bio() click to toggle source
# File lib/instagrammer/user.rb, line 65
def bio
  data["description"]
end
data() click to toggle source
# File lib/instagrammer/user.rb, line 42
def data
  get_data unless @data

  case @status
  when :private then raise Instagrammer::PrivateAccount.new("Private account: #{@username}")
  when :not_found then raise Instagrammer::UserNotFound.new("User not found: #{@username}")
  when :invalid then raise Instagrammer::UserInvalid.new("User invalid: #{@username}")
  else @data
  end
end
follower_count() click to toggle source
# File lib/instagrammer/user.rb, line 30
def follower_count
  meta[:followers]
end
following_count() click to toggle source
# File lib/instagrammer/user.rb, line 34
def following_count
  meta[:following]
end
get_posts(limit) click to toggle source
# File lib/instagrammer/user.rb, line 74
def get_posts(limit)
  shortcodes = []
  i = 0

  visit "https://www.instagram.com/#{@username}/"
  while i < limit
    post_links = page.all(:post_link)

    if limit > post_links.size
      page.execute_script "window.scrollTo(0,document.body.scrollHeight);"
      post_links = page.all(:post_link)
    end

    shortcode = post_links[i]["href"].match(SHORTCODE_RE)[1]
    shortcodes << shortcode
    i += 1
  end

  @posts = shortcodes.collect { |code| Instagrammer::Post.new(code) }
end
meta() click to toggle source
# File lib/instagrammer/user.rb, line 20
def meta
  get_data unless @data

  if @status == :not_found
    raise Instagrammer::UserNotFound.new("Private account: #{@username}")
  else
    @meta
  end
end
name() click to toggle source
# File lib/instagrammer/user.rb, line 53
def name
  data["name"]
end
post_count() click to toggle source
# File lib/instagrammer/user.rb, line 38
def post_count
  meta[:posts]
end
public?() click to toggle source
# File lib/instagrammer/user.rb, line 15
def public?
  get_data unless @data
  @status == :public
end
url() click to toggle source
# File lib/instagrammer/user.rb, line 69
def url
  data["mainEntityofPage"]["@id"]
end
username() click to toggle source
# File lib/instagrammer/user.rb, line 57
def username
  data["alternateName"].delete_prefix("@")
end

Private Instance Methods

get_data() click to toggle source
# File lib/instagrammer/user.rb, line 96
def get_data
  visit "https://www.instagram.com/#{@username}/"
  @status = get_page_status
  @meta = get_metadata unless @status == :not_found

  if @status == :public
    node = page.first(:json_ld, visible: false)
    @data = JSON.parse(node.text(:all))
  end
end
get_metadata() click to toggle source
# File lib/instagrammer/user.rb, line 108
def get_metadata
  @meta = page.first(:meta_description, visible: false)["content"].match META_RE
end