Class: Redd::Models::Submission

Inherits:
LazyModel show all
Includes:
Gildable, Moderatable, Postable, Replyable
Defined in:
lib/redd/models/submission.rb

Overview

A text or link post.

Instance Attribute Summary

Attributes inherited from BasicModel

#client

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Replyable

#reply

Methods included from Postable

#delete, #disable_inbox_replies, #downvote, #edit, #enable_inbox_replies, #hide, #save, #undo_vote, #unhide, #unsave, #upvote

Methods included from Moderatable

#approve, #distinguish, #ignore_reports, #remove, #undistinguish, #unignore_reports

Methods included from Gildable

#gild

Methods inherited from LazyModel

#force_load, #initialize, #method_missing, #respond_to_missing?, #to_h

Methods inherited from BasicModel

#initialize, #inspect, #method_missing, #respond_to_missing?, #to_ary, #to_h

Constructor Details

This class inherits a constructor from Redd::Models::LazyModel

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Redd::Models::LazyModel

Class Method Details

.from_id(client, id) ⇒ Submission

Create a Subreddit from its fullname.

Parameters:

  • client (APIClient)

    the api client to initialize the object with

  • id (String)

    the fullname

Returns:


25
26
27
# File 'lib/redd/models/submission.rb', line 25

def self.from_id(client, id)
  new(client, name: id)
end

Instance Method Details

#disable_contest_modeObject

Disable the “contest mode”.


88
89
90
# 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

#duplicates(**params) ⇒ Listing<Submission>

Get all submissions for the same url.

Parameters:

  • params (Hash)

    A list of optional params to send with the request.

Options Hash (**params):

  • :after (String)

    return results after the given fullname

  • :before (String)

    return results before the given fullname

  • :count (Integer) — default: 0

    the number of items already seen in the listing

  • :limit (1..100) — default: 25

    the maximum number of things to return

Returns:


54
55
56
# File 'lib/redd/models/submission.rb', line 54

def duplicates(**params)
  @client.unmarshal(@client.get("/duplicates/#{get_attribute(:id)}", params).body[1])
end

#enable_contest_modeObject

Set the submission to “contest mode” (comments are randomly sorted)


83
84
85
# 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

#lockObject

Prevent users from commenting on the link (and hide it as well).


104
105
106
# File 'lib/redd/models/submission.rb', line 104

def lock
  @client.post('/api/lock', id: get_attribute(:name))
end

#make_sticky(slot: nil) ⇒ Object

Set the submission as the sticky post of the subreddit.

Parameters:

  • slot (1, 2) (defaults to: nil)

    which “slot” to place the sticky on


94
95
96
# 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_as_nsfwObject

Mark the link as “Not Suitable For Work”.


59
60
61
62
# 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_as_spoilerObject

Mark the link as a spoiler.


71
72
73
74
# File 'lib/redd/models/submission.rb', line 71

def mark_as_spoiler
  @client.get('/api/spoiler', id: get_attribute(:name))
  @attributes[:spoiler] = true
end

#remove_stickyObject

Unsticky the post from the subreddit.


99
100
101
# 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_suggested_sort(suggested) ⇒ Object

Set the suggested sort order for comments for all users.

Parameters:

  • suggested ('blank', 'confidence', 'top', 'new', 'controversial', 'old', 'random', 'qa', 'live')

    the sort type


116
117
118
119
120
# 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

#sort_orderSymbol

Returns the requested sort order.

Returns:

  • (Symbol)

    the requested sort order


30
31
32
# File 'lib/redd/models/submission.rb', line 30

def sort_order
  @sort_order ||= nil
end

#sort_order=(new_order) ⇒ Object

Set the sort order of the comments and reload if necessary.

Parameters:

  • new_order (:confidence, :top, :controversial, :old, :qa)

    the sort order


36
37
38
39
40
41
42
43
44
45
# 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

#unlockObject

Allow users to comment on the link again.


109
110
111
# File 'lib/redd/models/submission.rb', line 109

def unlock
  @client.post('/api/unlock', id: get_attribute(:name))
end

#unmark_as_nsfwObject

No longer mark the link as “Not Suitable For Work”.


65
66
67
68
# 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

#unmark_as_spoilerObject

No longer mark the link as a spoiler.


77
78
79
80
# File 'lib/redd/models/submission.rb', line 77

def unmark_as_spoiler
  @client.get('/api/unspoiler', id: get_attribute(:name))
  @attributes[:spoiler] = false
end