Class: Instagrammer::User
- Inherits:
-
Object
- Object
- Instagrammer::User
- Includes:
- Capybara::DSL, Utils
- Defined in:
- lib/instagrammer/user.rb
Constant Summary collapse
- SHORTCODE_RE =
/\/p\/(\S+)\/$/
Instance Attribute Summary collapse
-
#posts ⇒ Object
readonly
Returns the value of attribute posts.
Instance Method Summary collapse
- #avatar ⇒ Object
- #bio ⇒ Object
- #data ⇒ Object
- #follower_count ⇒ Object
- #following_count ⇒ Object
- #get_posts(limit) ⇒ Object
-
#initialize(username) ⇒ User
constructor
A new instance of User.
- #meta ⇒ Object
- #name ⇒ Object
- #post_count ⇒ Object
- #public? ⇒ Boolean
- #url ⇒ Object
- #username ⇒ Object
Methods included from Utils
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
#posts ⇒ Object (readonly)
Returns the value of attribute posts.
7 8 9 |
# File 'lib/instagrammer/user.rb', line 7 def posts @posts end |
Instance Method Details
#avatar ⇒ Object
61 62 63 |
# File 'lib/instagrammer/user.rb', line 61 def avatar data["image"] end |
#bio ⇒ Object
65 66 67 |
# File 'lib/instagrammer/user.rb', line 65 def bio data["description"] end |
#data ⇒ Object
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_count ⇒ Object
30 31 32 |
# File 'lib/instagrammer/user.rb', line 30 def follower_count [:followers] end |
#following_count ⇒ Object
34 35 36 |
# File 'lib/instagrammer/user.rb', line 34 def following_count [: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 |
#meta ⇒ Object
20 21 22 23 24 25 26 27 28 |
# File 'lib/instagrammer/user.rb', line 20 def get_data unless @data if @status == :not_found raise Instagrammer::UserNotFound.new("Private account: #{@username}") else @meta end end |
#name ⇒ Object
53 54 55 |
# File 'lib/instagrammer/user.rb', line 53 def name data["name"] end |
#post_count ⇒ Object
38 39 40 |
# File 'lib/instagrammer/user.rb', line 38 def post_count [:posts] end |
#public? ⇒ Boolean
15 16 17 18 |
# File 'lib/instagrammer/user.rb', line 15 def public? get_data unless @data @status == :public end |
#url ⇒ Object
69 70 71 |
# File 'lib/instagrammer/user.rb', line 69 def url data["mainEntityofPage"]["@id"] end |
#username ⇒ Object
57 58 59 |
# File 'lib/instagrammer/user.rb', line 57 def username data["alternateName"].delete_prefix("@") end |