Class: SocialMiner::Instagram::ProfilePosts

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

Constant Summary collapse

PER_PAGE =
12
QUERY_HASH =
"69cba40317214236af40e7efa697781d"

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(user_id:, cursor: nil) ⇒ Object



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
# File 'lib/social_miner/instagram/profile_posts.rb', line 9

def call(user_id:, cursor: nil)
  uri = URI(GRAPHQL_URL)

  variables = { id: user_id, first: PER_PAGE }
  variables[:after] = cursor if cursor

  uri.query = URI.encode_www_form([
                                    [:query_hash, QUERY_HASH],
                                    [:variables, variables.to_json]
                                  ])

  response = Net::HTTP.get_response(uri, request_headers)

  case response
  when Net::HTTPSuccess
    response_data  = JSON.parse(response.body)
    timeline_media = Helpers::Hash.fetch_nested(response_data,
                                                "data",
                                                "user",
                                                "edge_owner_to_timeline_media")

    cursor  = Helpers::Hash.fetch_nested(timeline_media, "page_info", "end_cursor")
    records = timeline_media.fetch("edges").map { |attrs| attrs.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