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



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

def avatar
  data["image"]
end

#bioObject



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

def bio
  data["description"]
end

#dataObject



47
48
49
50
51
52
53
54
55
56
# File 'lib/instagrammer/user.rb', line 47

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



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

def follower_count
  meta[:followers]
end

#following_countObject



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

def following_count
  meta[:following]
end

#get_posts(limit) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/instagrammer/user.rb', line 79

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

#inspectObject



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

def inspect
  attributes = i(follower_count following_count post_count name username avatar bio url posts)
  "#<#{self.class.name}:#{object_id} #{attributes.map { |attr| "#{attr}:#{send(attr).inspect}" }.join(", ")}>"
end

#metaObject



25
26
27
28
29
30
31
32
33
# File 'lib/instagrammer/user.rb', line 25

def meta
  get_data unless @data

  if @status == :not_found
    raise Instagrammer::UserNotFound.new("Private account: #{@username}")
  else
    @meta
  end
end

#nameObject



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

def name
  data["name"]
end

#post_countObject



43
44
45
# File 'lib/instagrammer/user.rb', line 43

def post_count
  meta[:posts]
end

#public?Boolean

Returns:

  • (Boolean)


20
21
22
23
# File 'lib/instagrammer/user.rb', line 20

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

#urlObject



74
75
76
# File 'lib/instagrammer/user.rb', line 74

def url
  data["url"]
end

#usernameObject



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

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