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.



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

def approved_by
  @approved_by
end

#authorObject (readonly)

The username of the link’s author.



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

def author
  @author
end

#author_flair_css_classObject (readonly)

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



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

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.



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

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.



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

def banned_by
  @banned_by
end

#distinguishedObject (readonly)

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



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

def distinguished
  @distinguished
end

#domainObject (readonly)

The link’s domain.



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

def domain
  @domain
end

#editedObject (readonly)

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



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

def edited
  @edited
end

#hiddenObject (readonly)

Whether the current user has hidden this link.



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

def hidden
  @hidden
end

#is_selfObject (readonly) Also known as: self_post?

Whether the link is a self post.



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

def is_self
  @is_self
end

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



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

def link_flair_css_class
  @link_flair_css_class
end

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



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

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.



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

def media
  @media
end

#media_embedObject (readonly)

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



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

def media_embed
  @media_embed
end

#num_commentsObject (readonly) Also known as: total_comments

The number of comments on the link.



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

def num_comments
  @num_comments
end

#num_reportsObject (readonly) Also known as: total_reports

The number of reports on the link.



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

def num_reports
  @num_reports
end

#over_18Object (readonly) Also known as: nsfw?

Whether the link has been marked as NSFW.



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

def over_18
  @over_18
end

The link to the post on reddit.



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

def permalink
  @permalink
end

#savedObject (readonly)

Whether the current user has saved this link.



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

def saved
  @saved
end

#selftextObject (readonly) Also known as: text

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



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

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.



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

def selftext_html
  @selftext_html
end

#stickiedObject (readonly) Also known as: sticky?

Whether the link has been marked as sticky.



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

def stickied
  @stickied
end

#subredditObject (readonly)

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



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

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.



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

def subreddit_id
  @subreddit_id
end

#thumbnailObject (readonly) Also known as: thumbnail_url

The URL to the thumbnail for the link.



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

def thumbnail
  @thumbnail
end

#titleObject (readonly)

The title of the link.



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

def title
  @title
end

#urlObject (readonly)

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



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

def url
  @url
end

#visitedObject (readonly)

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



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

def visited
  @visited
end

Instance Method Details

#image_link?Boolean

Determines whether a link has an image URL.

Returns:

  • (Boolean)


122
123
124
125
# File 'lib/redditkit/link.rb', line 122

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)


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

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)


111
112
113
114
115
116
117
# File 'lib/redditkit/link.rb', line 111

def showing_link_score?
  current_time = Time.now
  submission_time = created_at

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