Class: User

Inherits:
Object
  • Object
show all
Defined in:
lib/User.rb

Constant Summary collapse

USER_FOLLOWERS_QUERY_PATH =
File.expand_path('Queries/UserFollowersQuery.graphql', __dir__).freeze
USER_PROFILE_QUERY_PATH =
File.expand_path('Queries/UserProfileQuery.graphql', __dir__).freeze

Class Method Summary collapse

Class Method Details

.convertToUserIDFromUsername(username) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/User.rb', line 10

def self.convertToUserIDFromUsername(username)
  username = username[1..] if username.start_with?('@')

  query = [
    {
      "operationName": "UserFollowers",
      "variables": {
        "id": nil,
        "username": username,
        "paging": nil
      },
      "query": userFollowersQueryString
    }
  ]

  body = Request.body(Request.URL(Request.mediumGraphqlEndpoint, "POST", query))
  return nil if body.nil?

  json = JSON.parse(body)
  json&.dig(0, "data", "userResult", "id")
end

.extractPosts(json) ⇒ Object

Pulled out so it can be exercised by tests without hitting the network.



54
55
56
57
58
59
60
61
62
# File 'lib/User.rb', line 54

def self.extractPosts(json)
  nextInfo = json&.dig(0, "data", "userResult", "homepagePostsConnection", "pagingInfo", "next")
  postsInfo = json&.dig(0, "data", "userResult", "homepagePostsConnection", "posts")

  {
    "nextID" => nextInfo && nextInfo["from"],
    "postURLs" => (postsInfo || []).map { |post| { "url" => post["mediumUrl"], "pin" => post["pinnedByCreatorAt"].to_i > 0 } }
  }
end

.fetchUserPosts(userID, from) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/User.rb', line 32

def self.fetchUserPosts(userID, from)
  query = [
    {
      "operationName": "UserProfileQuery",
      "variables": {
        "homepagePostsFrom": from,
        "includeDistributedResponses": true,
        "id": userID,
        "homepagePostsLimit": 10
      },
      "query": userProfileQueryString
    }
  ]

  body = Request.body(Request.URL(Request.mediumGraphqlEndpoint, "POST", query))
  return { "nextID" => nil, "postURLs" => [] } if body.nil?

  json = JSON.parse(body)
  extractPosts(json)
end

.userFollowersQueryStringObject



64
65
66
# File 'lib/User.rb', line 64

def self.userFollowersQueryString
  @userFollowersQueryString ||= File.read(USER_FOLLOWERS_QUERY_PATH)
end

.userProfileQueryStringObject



68
69
70
# File 'lib/User.rb', line 68

def self.userProfileQueryString
  @userProfileQueryString ||= File.read(USER_PROFILE_QUERY_PATH)
end