Class: User
- Inherits:
-
Object
- Object
- User
- Defined in:
- lib/User.rb
Constant Summary collapse
- USER_FOLLOWERS_QUERY_PATH =
File.('Queries/UserFollowersQuery.graphql', __dir__).freeze
- USER_PROFILE_QUERY_PATH =
File.('Queries/UserProfileQuery.graphql', __dir__).freeze
Class Method Summary collapse
- .convertToUserIDFromUsername(username) ⇒ Object
-
.extractPosts(json) ⇒ Object
Pulled out so it can be exercised by tests without hitting the network.
- .fetchUserPosts(userID, from) ⇒ Object
- .userFollowersQueryString ⇒ Object
- .userProfileQueryString ⇒ Object
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 31 |
# 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 } ] host = ENV.fetch('MEDIUM_HOST', 'https://medium.com/_/graphql') body = Request.body(Request.URL(host, "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.
56 57 58 59 60 61 62 63 64 |
# File 'lib/User.rb', line 56 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
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/User.rb', line 33 def self.fetchUserPosts(userID, from) query = [ { "operationName": "UserProfileQuery", "variables": { "homepagePostsFrom": from, "includeDistributedResponses": true, "id": userID, "homepagePostsLimit": 10 }, "query": userProfileQueryString } ] host = ENV.fetch('MEDIUM_HOST', 'https://medium.com/_/graphql') body = Request.body(Request.URL(host, "POST", query)) return { "nextID" => nil, "postURLs" => [] } if body.nil? json = JSON.parse(body) extractPosts(json) end |
.userFollowersQueryString ⇒ Object
66 67 68 |
# File 'lib/User.rb', line 66 def self.userFollowersQueryString @userFollowersQueryString ||= File.read(USER_FOLLOWERS_QUERY_PATH) end |
.userProfileQueryString ⇒ Object
70 71 72 |
# File 'lib/User.rb', line 70 def self.userProfileQueryString @userProfileQueryString ||= File.read(USER_PROFILE_QUERY_PATH) end |