class Redd::Models::Submission
A text or link post.
Public Class Methods
Create a Subreddit
from its fullname. @param client [APIClient] the api client to initialize the object with @param id [String] the fullname @return [Submission]
# File lib/redd/models/submission.rb, line 25 def self.from_id(client, id) new(client, name: id) end
Public Instance Methods
Disable the “contest mode”.
# File lib/redd/models/submission.rb, line 88 def disable_contest_mode @client.post('/api/set_contest_mode', id: get_attribute(:name), state: false) 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 @option params [1..100] :limit (25) the maximum number of things to return @return [Listing<Submission>]
# File lib/redd/models/submission.rb, line 54 def duplicates(**params) @client.unmarshal(@client.get("/duplicates/#{get_attribute(:id)}", params).body[1]) end
Set the submission to “contest mode” (comments are randomly sorted)
# File lib/redd/models/submission.rb, line 83 def enable_contest_mode @client.post('/api/set_contest_mode', id: get_attribute(:name), state: true) end
Prevent users from commenting on the link (and hide it as well).
# File lib/redd/models/submission.rb, line 104 def lock @client.post('/api/lock', id: get_attribute(:name)) end
Set the submission as the sticky post of the subreddit. @param slot [1, 2] which “slot” to place the sticky on
# File lib/redd/models/submission.rb, line 94 def make_sticky(slot: nil) @client.post('/api/set_subreddit_sticky', id: get_attribute(:name), num: slot, state: true) end
Mark the link as “Not Suitable For Work”.
# File lib/redd/models/submission.rb, line 59 def mark_as_nsfw @client.get('/api/marknsfw', id: get_attribute(:name)) @attributes[:over_18] = true end
Mark the link as a spoiler.
# File lib/redd/models/submission.rb, line 71 def mark_as_spoiler @client.get('/api/spoiler', id: get_attribute(:name)) @attributes[:spoiler] = true end
Unsticky the post from the subreddit.
# File lib/redd/models/submission.rb, line 99 def remove_sticky @client.post('/api/set_subreddit_sticky', id: get_attribute(:name), state: false) end
Set the suggested sort order for comments for all users. @param suggested ['blank', 'confidence', 'top', 'new', 'controversial', 'old', 'random',
'qa', 'live'] the sort type
# File lib/redd/models/submission.rb, line 116 def set_suggested_sort(suggested) # rubocop:disable Style/AccessorMethodName # Style/AccessorMethodName is disabled because it feels wrong for accessor methods to make # HTTP requests. @client.post('/api/set_suggested_sort', id: get_attribute(:name), sort: suggested) end
@return [Symbol] the requested sort order
# File lib/redd/models/submission.rb, line 30 def sort_order @sort_order ||= nil end
Set the sort order of the comments and reload if necessary. @param new_order [:confidence, :top, :controversial, :old, :qa] the sort order
# File lib/redd/models/submission.rb, line 36 def sort_order=(new_order) # If the comments were loaded in a different sort order, delete them and invalidate this # model. if @attributes.key?(:comments) && @sort_order != new_order @attributes.delete(:comments) @definitely_fully_loaded = false end @sort_order = new_order end
Allow users to comment on the link again.
# File lib/redd/models/submission.rb, line 109 def unlock @client.post('/api/unlock', id: get_attribute(:name)) end
No longer mark the link as “Not Suitable For Work”.
# File lib/redd/models/submission.rb, line 65 def unmark_as_nsfw @client.get('/api/unmarknsfw', id: get_attribute(:name)) @attributes[:over_18] = false end
No longer mark the link as a spoiler.
# File lib/redd/models/submission.rb, line 77 def unmark_as_spoiler @client.get('/api/unspoiler', id: get_attribute(:name)) @attributes[:spoiler] = false end
Private Instance Methods
# File lib/redd/models/submission.rb, line 138 def after_initialize if @attributes[:subreddit] @attributes[:subreddit] = Subreddit.from_id(@client, @attributes[:subreddit]) end @attributes[:author] = User.from_id(@client, @attributes[:author]) if @attributes[:author] end
# File lib/redd/models/submission.rb, line 124 def default_loader # Ensure we have the link's id. id = @attributes[:id] ? @attributes[:id] : @attributes.fetch(:name).sub('t3_', '') # 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