Module: Redd::Client::Unauthenticated::LinksComments

Included in:
Redd::Client::Unauthenticated
Defined in:
lib/redd/client/unauthenticated/links_comments.rb

Instance Method Summary collapse

Instance Method Details

#get_info(params = {}) ⇒ Object

Note:

Reddit does accept a subreddit, but with fullnames and urls, I assumed that was unnecessary.



7
8
9
# File 'lib/redd/client/unauthenticated/links_comments.rb', line 7

def get_info(params = {})
  object_from_response :get, "/api/info.json", params
end

#get_replies(comment) ⇒ Object



16
17
18
19
20
# File 'lib/redd/client/unauthenticated/links_comments.rb', line 16

def get_replies(comment)
  replies = comment.attributes[:replies]
  return [] unless replies.is_a?(Hash) and replies.has_key?(:kind)
  object_from_body(replies)
end

#replace_morecomments(morecomments, submission = nil) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/redd/client/unauthenticated/links_comments.rb', line 22

def replace_morecomments(morecomments, submission = nil)
  parent_id = morecomments.parent_id
  link_id =
    if submission
      submission
    elsif parent_id.start_with?("t3_")
      parent_id
    elsif parent_id.start_with?("t1_")
      get_info(id: parent_id).first.link_id
    end

  response = post "/api/morechildren", api_type: "json",
    link_id: link_id, children: morecomments.children.join(",")
  comments = response[:json][:data][:things]

  # No idea how to increase the depth of the comments.
  comments.select! { |comment| comment[:kind] == "t1" }

  comments.map do |comment|
    object_from_body(
      kind: comment[:kind],
      data: {
        parent_id: comment[:data][:parent],
        body: comment[:data][:contentText],
        body_html: comment[:data][:contentHTML],
        link_id: comment[:data][:link],
        name: comment[:data][:id]
      }
    )
  end
end

#submission_comments(submission) ⇒ Object



11
12
13
14
# File 'lib/redd/client/unauthenticated/links_comments.rb', line 11

def submission_comments(submission)
  id = extract_id(submission)
  comments_from_response :get, "/comments/#{id}.json"
end