Class: IGScrape::Post
- Inherits:
-
Object
- Object
- IGScrape::Post
- Defined in:
- lib/ig_scrape/post.rb
Instance Attribute Summary collapse
-
#caption ⇒ Object
Returns the value of attribute caption.
-
#code ⇒ Object
Returns the value of attribute code.
-
#comment_count ⇒ Object
Returns the value of attribute comment_count.
-
#comments ⇒ Object
Returns the value of attribute comments.
-
#created_at ⇒ Object
Returns the value of attribute created_at.
-
#display_src ⇒ Object
Returns the value of attribute display_src.
-
#id ⇒ Object
Returns the value of attribute id.
-
#is_video ⇒ Object
Returns the value of attribute is_video.
-
#likes ⇒ Object
Returns the value of attribute likes.
-
#type ⇒ Object
Returns the value of attribute type.
Class Method Summary collapse
Instance Method Summary collapse
- #has_more_comments? ⇒ Boolean
-
#initialize(payload) ⇒ Post
constructor
A new instance of Post.
- #load_comments ⇒ Object
- #to_json(*args) ⇒ Object
Constructor Details
#initialize(payload) ⇒ Post
Returns a new instance of Post.
6 7 8 9 |
# File 'lib/ig_scrape/post.rb', line 6 def initialize payload @comments = [] load_from_payload(payload) end |
Instance Attribute Details
#caption ⇒ Object
Returns the value of attribute caption.
4 5 6 |
# File 'lib/ig_scrape/post.rb', line 4 def @caption end |
#code ⇒ Object
Returns the value of attribute code.
4 5 6 |
# File 'lib/ig_scrape/post.rb', line 4 def code @code end |
#comment_count ⇒ Object
Returns the value of attribute comment_count.
4 5 6 |
# File 'lib/ig_scrape/post.rb', line 4 def comment_count @comment_count end |
#comments ⇒ Object
Returns the value of attribute comments.
4 5 6 |
# File 'lib/ig_scrape/post.rb', line 4 def comments @comments end |
#created_at ⇒ Object
Returns the value of attribute created_at.
4 5 6 |
# File 'lib/ig_scrape/post.rb', line 4 def created_at @created_at end |
#display_src ⇒ Object
Returns the value of attribute display_src.
4 5 6 |
# File 'lib/ig_scrape/post.rb', line 4 def display_src @display_src end |
#id ⇒ Object
Returns the value of attribute id.
4 5 6 |
# File 'lib/ig_scrape/post.rb', line 4 def id @id end |
#is_video ⇒ Object
Returns the value of attribute is_video.
4 5 6 |
# File 'lib/ig_scrape/post.rb', line 4 def is_video @is_video end |
#likes ⇒ Object
Returns the value of attribute likes.
4 5 6 |
# File 'lib/ig_scrape/post.rb', line 4 def likes @likes end |
#type ⇒ Object
Returns the value of attribute type.
4 5 6 |
# File 'lib/ig_scrape/post.rb', line 4 def type @type end |
Class Method Details
.edge_timeline_to_payload(node) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/ig_scrape/post.rb', line 36 def self.edge_timeline_to_payload node { "id" => node["id"], "__typename" => node["__typename"], "is_video" => node["is_video"], "code" => node["shortcode"], "display_src" => node["display_url"], "caption" => (node["edge_media_to_caption"]["edges"].length > 0 ? node["edge_media_to_caption"]["edges"].first["node"]["text"] : ""), "date" => node["taken_at_timestamp"], "comments" => node["edge_media_to_comment"], "likes" => node["edge_media_preview_like"] } end |
.load_from_shortcode(code) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/ig_scrape/post.rb', line 11 def self.load_from_shortcode code url = "https://www.instagram.com/p/#{code}/?__a=1" resp = HTTParty.get(url) case resp.code when 200 response = JSON.parse(resp.body) payload = response["graphql"]["shortcode_media"] post = IGScrape::Post.new(self.edge_timeline_to_payload(payload)) when 404 raise ArgumentError.new("Post with #{code} does not exist!") end end |
Instance Method Details
#has_more_comments? ⇒ Boolean
26 27 28 |
# File 'lib/ig_scrape/post.rb', line 26 def has_more_comments? @comments.length < @comment_count end |
#load_comments ⇒ Object
30 31 32 33 34 |
# File 'lib/ig_scrape/post.rb', line 30 def load_comments while has_more_comments? do load_more_comments end end |
#to_json(*args) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/ig_scrape/post.rb', line 50 def to_json(*args) JSON.generate({ id: @id, code: @code, caption: @caption, type: @type, created_at: @created_at, comment_count: @comment_count, likes: @likes }) end |