Class: SocialMiner::Instagram::PostComments

Inherits:
Base
  • Object
show all
Defined in:
lib/social_miner/instagram/post_comments.rb

Constant Summary collapse

DOC_ID =
"8845758582119845"

Constants inherited from Base

Base::API_URL, Base::DEFAULT_HEADERS, Base::GRAPHQL_URL, Base::SOURCE_URL

Instance Attribute Summary

Attributes inherited from Base

#request_headers

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from SocialMiner::Instagram::Base

Instance Method Details

#call(post_shortcode:, cursor: nil) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/social_miner/instagram/post_comments.rb', line 8

def call(post_shortcode:, cursor: nil)
  variables = { shortcode: post_shortcode }
  variables[:after] = cursor if cursor

  data = {
    "variables" => { shortcode: post_shortcode }.to_json,
    "doc_id" => DOC_ID
  }

  response = Net::HTTP.post_form(URI(GRAPHQL_URL), data)

  case response
  when Net::HTTPSuccess
    response_data = JSON.parse(response.body)

    edge_media = Helpers::Hash.fetch_nested(response_data,
                                            "data",
                                            "xdt_shortcode_media",
                                            "edge_media_to_parent_comment")

    cursor = Helpers::Hash.fetch_nested(edge_media, "page_info", "end_cursor")
                          .then { |data| JSON.parse(data) }
                          .then { |data| data.fetch("server_cursor") }

    records = edge_media.fetch("edges").map { |edge| edge.fetch("node") }

    if block_given?
      yield(records, cursor)
    else
      {
        records: records.map { |record| SocialMiner.mapper_for_klass(self.class).map(record) },
        cursor: cursor
      }
    end
  else
    raise Net::HTTPError.new("Request Failed #{response.code}", response)
  end
end