Class: DownloadSolutions::Api::Reddit::GetSerialComments

Inherits:
Object
  • Object
show all
Defined in:
lib/download_solutions/api/reddit/get_serial_comments.rb

Constant Summary collapse

MAX_SLEEP_COUNT =
10

Class Method Summary collapse

Class Method Details

.call(params:, parent_id: params.thread_id) ⇒ Array(Array<Hash>, Array<Hash>)

Equivalent to repeatedly pressing “View more comments” in a thread’s top level (or the “+” below a comment) until reaching the end. “Serial” because this doesn’t fetch all replies; many (not all) replies are in “more children” nodes and not yet fetched.

Parameters:

  • params (Reddit::Params)
  • parent_id (String) (defaults to: params.thread_id)

    the ID of the parent, by default the thread.

Returns:

  • (Array(Array<Hash>, Array<Hash>))

    comments and “more children” nodes.

Raises:

  • (MaxSleepCountReachedError)

    if Reddit seemingly throttled for an unusually long time, “seemingly” because the only sign is an empty JSON response body after loading additional comments.

  • (MultipleMoreChildrensError)

    if there are multiple “more children” nodes for the thread or a comment; only one at a time is expected. If there are ever more, this algorithm will need to change from a serial loop to recursion.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/download_solutions/api/reddit/get_serial_comments.rb', line 23

def self.call(params:, parent_id: params.thread_id)
  comments = initial_comments_or_replies(params, parent_id) || (return [[], []])

  loop do
    more_top_level_childrens, comments = comments.partition {
      it[:children] && it[:parent_id] == parent_id
    }
    break unless more_top_level_childrens.any?
    raise Reddit::MultipleMoreChildrensError if more_top_level_childrens.count > 1

    # Loop again to fetch more if there are more top-level comments.
    comments += fetch_more_children(params, more_top_level_childrens.first, parent_id)
  end

  more_childrens, comments = comments.partition { it[:children] }

  [comments, more_childrens]
end