lib/ig_scrape/client.rb in ig_scrape-0.0.2 vs lib/ig_scrape/client.rb in ig_scrape-0.0.3

- old
+ new

@@ -28,24 +28,30 @@ def load_profile url = "https://www.instagram.com/#{@username}/?__a=1" resp = HTTParty.get(url) - response = JSON.parse(resp.body) - user = response["user"] - @full_name = user["full_name"] - @follower_count = user["followed_by"]["count"] - @follows_count = user["follows"]["count"] - @id = user["id"] - @post_count = user["media"]["count"] - @page_info = user["media"]["page_info"] - @profile_pic_url = user["profile_pic_url"] + case resp.code + when 200 + response = JSON.parse(resp.body) + user = response["user"] + @full_name = user["full_name"] + @follower_count = user["followed_by"]["count"] + @follows_count = user["follows"]["count"] + @id = user["id"] + @post_count = user["media"]["count"] + @page_info = user["media"]["page_info"] + @profile_pic_url = user["profile_pic_url"] - media = user["media"]["nodes"] - if media - @posts = media.collect do |node| - IGScrape::Post.new(node) - end + media = user["media"]["nodes"] + if media + @posts = media.collect do |node| + IGScrape::Post.new(node) + end + end + when 404 + # the client does not exist + raise ArgumentError.new("#{@username} does not exist!") end end def load_more_posts cursor = @page_info["end_cursor"]