Class: RedditKit::Link

Inherits:
Thing show all
Includes:
Creatable, Votable
Defined in:
lib/redditkit/link.rb

Overview

A class representing a link.

Instance Attribute Summary collapse

Attributes inherited from Thing

#id, #kind

Attributes inherited from Base

#attributes

Instance Method Summary collapse

Methods included from Votable

#downvoted?, #downvotes, #score, #upvoted?, #upvotes, #voted?

Methods included from Creatable

#created_at

Methods inherited from Thing

#==, #full_name

Methods inherited from Base

#[], attr_reader, #initialize

Constructor Details

This class inherits a constructor from RedditKit::Base

Instance Attribute Details

#approved_byObject (readonly) Also known as: approved?

The username of the moderator who approved this link, if it has been approved.



14
15
16
# File 'lib/redditkit/link.rb', line 14

def approved_by
  @approved_by
end

#authorObject (readonly)

The username of the link’s author.



17
18
19
# File 'lib/redditkit/link.rb', line 17

def author
  @author
end

#author_flair_css_classObject (readonly)

The author’s flair CSS class, if they have flair assigned to them.



20
21
22
# File 'lib/redditkit/link.rb', line 20

def author_flair_css_class
  @author_flair_css_class
end

#author_flair_textObject (readonly)

The author’s flair text, if they have flair assigned to them.



23
24
25
# File 'lib/redditkit/link.rb', line 23

def author_flair_text
  @author_flair_text
end

#banned_byObject (readonly) Also known as: banned?

The username of the moderator who banned this link, if it was banned.



26
27
28
# File 'lib/redditkit/link.rb', line 26

def banned_by
  @banned_by
end

#distinguishedObject (readonly)

The distinguished status of the link. This is either ‘yes’, ‘no’, ‘admin’ or ‘special’.



29
30
31
# File 'lib/redditkit/link.rb', line 29

def distinguished
  @distinguished
end

#domainObject (readonly)

The link’s domain.



32
33
34
# File 'lib/redditkit/link.rb', line 32

def domain
  @domain
end

#editedObject (readonly)

The time of the link’s most recent edit, or nil if it has never been edited.



35
36
37
# File 'lib/redditkit/link.rb', line 35

def edited
  @edited
end

#hiddenObject (readonly)

Whether the current user has hidden this link.



38
39
40
# File 'lib/redditkit/link.rb', line 38

def hidden
  @hidden
end

#is_selfObject (readonly) Also known as: self_post?

Whether the link is a self post.



41
42
43
# File 'lib/redditkit/link.rb', line 41

def is_self
  @is_self
end

The link’s flair CSS class, if it has flair assigned to it.



44
45
46
# File 'lib/redditkit/link.rb', line 44

def link_flair_css_class
  @link_flair_css_class
end

The link’s flair text, if it has flair assigned to it.



47
48
49
# File 'lib/redditkit/link.rb', line 47

def link_flair_text
  @link_flair_text
end

#mediaObject (readonly)

A hash containing further information about the link’s media. For example, this could contain details about a YouTube video.



50
51
52
# File 'lib/redditkit/link.rb', line 50

def media
  @media
end

#media_embedObject (readonly)

A hash contains details for embedding the content in a web page.



53
54
55
# File 'lib/redditkit/link.rb', line 53

def media_embed
  @media_embed
end

#num_commentsObject (readonly) Also known as: total_comments

The number of comments on the link.



56
57
58
# File 'lib/redditkit/link.rb', line 56

def num_comments
  @num_comments
end

#num_reportsObject (readonly) Also known as: total_reports

The number of reports on the link.



59
60
61
# File 'lib/redditkit/link.rb', line 59

def num_reports
  @num_reports
end

#over_18Object (readonly) Also known as: nsfw?

Whether the link has been marked as NSFW.



62
63
64
# File 'lib/redditkit/link.rb', line 62

def over_18
  @over_18
end

The link to the post on reddit.



65
66
67
# File 'lib/redditkit/link.rb', line 65

def permalink
  @permalink
end

#savedObject (readonly)

Whether the current user has saved this link.



68
69
70
# File 'lib/redditkit/link.rb', line 68

def saved
  @saved
end

#selftextObject (readonly) Also known as: text

The text for the link, if it is a self post, in Markdown.



71
72
73
# File 'lib/redditkit/link.rb', line 71

def selftext
  @selftext
end

#selftext_htmlObject (readonly) Also known as: text_html

The text for the link, if it is a self post, in HTML.



74
75
76
# File 'lib/redditkit/link.rb', line 74

def selftext_html
  @selftext_html
end

#stickiedObject (readonly) Also known as: sticky?

Whether the link has been marked as sticky.



77
78
79
# File 'lib/redditkit/link.rb', line 77

def stickied
  @stickied
end

#subredditObject (readonly)

The name of the subreddit to which the link was posted.



80
81
82
# File 'lib/redditkit/link.rb', line 80

def subreddit
  @subreddit
end

#subreddit_idObject (readonly) Also known as: subreddit_full_name

The full name of the subreddit to which the link was posted.



83
84
85
# File 'lib/redditkit/link.rb', line 83

def subreddit_id
  @subreddit_id
end

#thumbnailObject (readonly) Also known as: thumbnail_url

The URL to the thumbnail for the link.



86
87
88
# File 'lib/redditkit/link.rb', line 86

def thumbnail
  @thumbnail
end

#titleObject (readonly)

The title of the link.



89
90
91
# File 'lib/redditkit/link.rb', line 89

def title
  @title
end

#urlObject (readonly)

Note:

This is HTML decoded as reddit encodes their JSON, meaning links which have parameters will be returned incorrectly.

The link’s URL. This will be a link to the post on reddit if the link is a self post.



92
93
94
# File 'lib/redditkit/link.rb', line 92

def url
  @url
end

#visitedObject (readonly)

Whether the link has been visited by the current user. Requires that the current user have reddit gold.



95
96
97
# File 'lib/redditkit/link.rb', line 95

def visited
  @visited
end

Instance Method Details

#image_link?Boolean

Determines whether a link has an image URL.

Returns:

  • (Boolean)


134
135
136
137
# File 'lib/redditkit/link.rb', line 134

def image_link?
  extensions = %w(.png .jpg .jpeg .gif)
  extensions.any? { |extension| url.end_with? extension }
end

Returns the short URL for a link.

Examples:

Returns:

  • (String)


147
148
149
# File 'lib/redditkit/link.rb', line 147

def short_link
  'http://redd.it/' << id
end

Determines whether a link is showing its score. reddit doesn’t display scores for links less than two hours old.

Returns:

  • (Boolean)


123
124
125
126
127
128
129
# File 'lib/redditkit/link.rb', line 123

def showing_link_score?
  current_time = Time.now
  submission_time = created_at

  difference = (current_time - submission_time) / 60 / 60
  difference >= 2
end