Class: Instagrammer::User

Inherits:
Object
  • Object
show all
Includes:
Capybara::DSL, Utils
Defined in:
lib/instagrammer/user.rb

Constant Summary collapse

SHORTCODE_RE =
/\/p\/(\S+)\/$/

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

#get_page_status

Constructor Details

#initialize(username) ⇒ User

Returns a new instance of User.



9
10
11
12
13
# File 'lib/instagrammer/user.rb', line 9

def initialize(username)
  @username = username.delete_prefix("@")
  @data = nil
  @posts = []
end

Instance Attribute Details

#postsObject (readonly)

Returns the value of attribute posts.



7
8
9
# File 'lib/instagrammer/user.rb', line 7

def posts
  @posts
end

Instance Method Details

#avatarObject



61
62
63
# File 'lib/instagrammer/user.rb', line 61

def avatar
  data["image"]
end

#bioObject



65
66
67
# File 'lib/instagrammer/user.rb', line 65

def bio
  data["description"]
end

#dataObject



42
43
44
45
46
47
48
49
50
51
# 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_countObject



30
31
32
# File 'lib/instagrammer/user.rb', line 30

def follower_count
  meta[:followers]
end

#following_countObject



34
35
36
# File 'lib/instagrammer/user.rb', line 34

def following_count
  meta[:following]
end

#get_posts(limit) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# 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

#metaObject



20
21
22
23
24
25
26
27
28
# 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

#nameObject



53
54
55
# File 'lib/instagrammer/user.rb', line 53

def name
  data["name"]
end

#post_countObject



38
39
40
# File 'lib/instagrammer/user.rb', line 38

def post_count
  meta[:posts]
end

#public?Boolean

Returns:

  • (Boolean)


15
16
17
18
# File 'lib/instagrammer/user.rb', line 15

def public?
  get_data unless @data
  @status == :public
end

#urlObject



69
70
71
# File 'lib/instagrammer/user.rb', line 69

def url
  data["mainEntityofPage"]["@id"]
end

#usernameObject



57
58
59
# File 'lib/instagrammer/user.rb', line 57

def username
  data["alternateName"].delete_prefix("@")
end