Module: RedditKit::Client::Comments

Included in:
RedditKit::Client
Defined in:
lib/redditkit/client/comments.rb

Overview

Methods for interacting with comment threads.

Instance Method Summary collapse

Instance Method Details

#comment(comment_full_name) ⇒ RedditKit::Comment

Note:

This method does not include any replies to the comment.

Get a comment object from its full name.

Parameters:

  • comment_full_name (String)

    The full name of the comment.

Returns:



14
15
16
17
# File 'lib/redditkit/client/comments.rb', line 14

def comment(comment_full_name)
  comments = objects_from_response(:get, 'api/info.json', { :id => comment_full_name })
  comments.first
end

#comments(link, options = {}) ⇒ Array<RedditKit::Comment>

Get comments on a link.

Parameters:

  • link (String, RedditKit::Link)

    The identifier of the link, or a RedditKit::Link.

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :limit (Integer)

    The number of comments to return.

Returns:



24
25
26
27
28
29
30
31
# File 'lib/redditkit/client/comments.rb', line 24

def comments(link, options = {})
  return nil unless link

  link_id = extract_id link
  path = "comments/#{link_id}.json"

  comments_from_response(:get, path, options)
end

#submit_comment(link_or_comment, text) ⇒ RedditKit::Comment

Submit a comment on a link or comment.

Parameters:

Returns:



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/redditkit/client/comments.rb', line 38

def submit_comment(link_or_comment, text)
  object_full_name = extract_full_name link_or_comment
  parameters = { :text => text, :thing_id => object_full_name, :api_type => :json }

  response = post('/api/comment', parameters)
  response_data = response[:body][:json][:data]

  full_comment_data = response_data[:things].first
  comment_data = full_comment_data[:data]
  comment_full_name = comment_data[:id]

  comment comment_full_name
end