Class: Redd::Objects::Submission

Inherits:
Thing
  • Object
show all
Includes:
Thing::Editable, Thing::Hideable, Thing::Moderatable, Thing::Refreshable, Thing::Saveable, Thing::Votable
Defined in:
lib/redd/objects/submission.rb

Overview

A submission made in a subreddit.

Instance Attribute Summary

Attributes inherited from Base

#client

Instance Method Summary collapse

Methods included from Thing::Votable

#clear_vote, #downvote, #upvote

Methods included from Thing::Saveable

#save, #unsave

Methods included from Thing::Moderatable

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

Methods included from Thing::Hideable

#hide, #unhide

Methods included from Thing::Editable

#delete!, #edit

Methods inherited from Thing

#==, #fullname

Methods inherited from Base

alias_property, #initialize

Constructor Details

This class inherits a constructor from Redd::Objects::Base

Instance Method Details

#add_comment(text) ⇒ Objects::Comment

Reply to the thing.

Parameters:

  • text (String)

    The text to comment.

Returns:



44
45
46
# File 'lib/redd/objects/submission.rb', line 44

def add_comment(text)
  client.add_comment(self, text)
end

#commentsListing Also known as: replies

TODO:

Allow for various depths and contexts and what not. Maybe a get_comment method?

Returns The submission’s comments.

Returns:

  • (Listing)

    The submission’s comments.



73
74
75
76
# File 'lib/redd/objects/submission.rb', line 73

def comments
  refresh! unless @comments
  @comments
end

#expand_more(more) ⇒ Listing

Take a MoreComments and return a listing of comments. rubocop:disable Metrics/MethodLength

Parameters:

Returns:

  • (Listing)

    A listing of the expanded comments.



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/redd/objects/submission.rb', line 91

def expand_more(more)
  response = client.get(
    '/api/morechildren',
    children: more.join(','),
    link_id: fullname
  )

  client.object_from_body(
    kind: 'Listing',
    data: {
      before: '', after: '',
      children: response.body[:json][:data][:things]
    }
  )
end

#get_duplicates(**params) ⇒ Objects::Listing<Objects::Submission>

Get other articles with the same URL.

Parameters:

  • params (Hash)

    A list of 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)

    The number of items already seen in the listing.

  • :limit (1..100)

    The maximum number of things to return.

Returns:



134
135
136
137
# File 'lib/redd/objects/submission.rb', line 134

def get_duplicates(**params)
  duplicates = get("/duplicates/#{id}.json", params).body[1]
  client.object_from_body(duplicates)
end

Get the related articles.

Parameters:

  • params (Hash)

    A list of 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)

    The number of items already seen in the listing.

  • :limit (1..100)

    The maximum number of things to return.

Returns:



118
119
120
121
# File 'lib/redd/objects/submission.rb', line 118

def get_related(**params)
  related = get("/related/#{id}.json", params).body[1]
  client.object_from_body(related)
end

#gilded?Boolean

Whether the comment was gilded.

Returns:

  • (Boolean)


24
25
26
# File 'lib/redd/objects/submission.rb', line 24

def gilded?
  self[:gilded] > 0
end

#mark_as_nsfwObject

Mark the thing as Not Suitable For Work.



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

def mark_as_nsfw
  get('/api/marknsfw', id: fullname)
  self[:over_18] = true
end

#refresh!Submission

Refresh the submission AND its comments.

Returns:



81
82
83
84
85
# File 'lib/redd/objects/submission.rb', line 81

def refresh!
  body = get("/comments/#{id}.json").body
  @comments = client.object_from_body(body[1])
  deep_merge!(body[0])
end

#set_contest_modeObject

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



49
50
51
# File 'lib/redd/objects/submission.rb', line 49

def set_contest_mode
  post('/api/set_contest_mode', id: fullname, state: true)
end

#set_stickyObject

Set the submission as the sticky post of the subreddit



59
60
61
62
# File 'lib/redd/objects/submission.rb', line 59

def set_sticky
  post('/api/set_subreddit_sticky', id: fullname, state: true)
  self[:stickied] = true
end

#short_urlObject

The shorter url for sharing.



19
20
21
# File 'lib/redd/objects/submission.rb', line 19

def short_url
  "http://redd.it/#{self[:id]}"
end

#unmark_as_nsfwObject Also known as: mark_as_safe

No longer mark the thing as Not Suitable For Work.



35
36
37
38
# File 'lib/redd/objects/submission.rb', line 35

def unmark_as_nsfw
  get('/api/unmarknsfw', id: fullname)
  self[:over_18] = false
end

#unset_contest_modeObject

Unset the “contest mode”.



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

def unset_contest_mode
  post('/api/set_contest_mode', id: fullname, state: false)
end

#unset_stickyObject

Unsticky the post from the subreddit



65
66
67
68
# File 'lib/redd/objects/submission.rb', line 65

def unset_sticky
  post('/api/set_subreddit_sticky', id: fullname, state: false)
  self[:stickied] = false
end