lib/redd/models/submission.rb in redd-0.8.3 vs lib/redd/models/submission.rb in redd-0.8.4
- old
+ new
@@ -24,10 +24,23 @@
# @return [Submission]
def self.from_id(client, id)
new(client, name: id)
end
+ # @return [Symbol] the requested sort order
+ def sort_order
+ @sort_order ||= nil
+ end
+
+ # Set the sort order of the comments and reload if necessary.
+ # @param order [:confidence, :top, :controversial, :old, :qa] the sort order
+ def sort_order=(order)
+ @sort_order = order
+ reload if @definitely_fully_loaded
+ order
+ end
+
# Get all submissions for the same url.
# @param params [Hash] A list of optional params to send with the request.
# @option params [String] :after return results after the given fullname
# @option params [String] :before return results before the given fullname
# @option params [Integer] :count (0) the number of items already seen in the listing
@@ -95,13 +108,17 @@
private
def default_loader
# Ensure we have the link's id.
id = @attributes[:id] ? @attributes[:id] : @attributes.fetch(:name).sub('t3_', '')
- response = @client.get("/comments/#{id}").body
+ # If a specific sort order was requested, provide it.
+ params = {}
+ params[:sort] = @sort_order if @sort_order
+
# `response` is a pair (2-element array):
# - response[0] is a one-item listing containing the submission
# - response[1] is listing of comments
+ response = @client.get("/comments/#{id}", params).body
response[0][:data][:children][0][:data].merge(comments: @client.unmarshal(response[1]))
end
def after_initialize
if @attributes[:subreddit]