Module: RedditKit::Client::Links

Included in:
RedditKit::Client
Defined in:
lib/redditkit/client/links.rb

Overview

Methods for retrieving, submitting and interacting with links.

Instance Method Summary collapse

Instance Method Details

#front_page(options = {}) ⇒ RedditKit::PaginatedResponse

Gets the links currently on the front page.

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :category (hot, new, rising, controversial, top, ads)

    The category from which to retrieve links.

  • :time (hour, day, week, month, year, all)

    The time from which to retrieve links. Defaults to all time.

  • :limit (1..100)

    The number of links to return.

  • :before (String)

    Only return links before this identifier.

  • :after (String)

    Only return links after this identifier.

Returns:



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

def front_page(options = {})
  links nil, options
end

#hide(link) ⇒ Object

Hides a link.

Parameters:

  • link (String, RedditKit::Link)

    A link’s full name, or a RedditKit::Link.



122
123
124
# File 'lib/redditkit/client/links.rb', line 122

def hide(link)
  post 'api/hide', :id => extract_full_name(link)
end
Note:

This method will return nil if there is not a user currently signed in.

Gets a link object from its full name.

Parameters:

  • link_full_name (String)

    The full name of the link.

Returns:



50
51
52
53
# File 'lib/redditkit/client/links.rb', line 50

def link(link_full_name)
  links = objects_from_response(:get, 'api/info.json', { :id => link_full_name })
  links.first
end

Gets an array of links from a specific subreddit.

Parameters:

  • subreddit (String, RedditKit::Subreddit)

    The display name of the subreddit, or a RedditKit::Subreddit.

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :category (hot, new, rising, controversial, top, ads)

    The category from which to retrieve links.

  • :time (hour, day, week, month, year, all)

    The time from which to retrieve links. Defaults to all time.

  • :limit (1..100)

    The number of links to return.

  • :before (String)

    Only return links before this identifier.

  • :after (String)

    Only return links after this identifier.

Returns:



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/redditkit/client/links.rb', line 30

def links(subreddit, options = {})
  options = options.clone

  subreddit_name = extract_string(subreddit, :display_name) if subreddit
  category = options[:category] || :hot

  path = "%s/#{category.to_s}.json" % ('r/' + subreddit_name if subreddit_name)

  options[:t] = options[:time] if options[:time]
  options.delete :category
  options.delete :time

  objects_from_response(:get, path, options)
end

Gets links with a specific domain.

Examples:

links = RedditKit.links_with_domain “github.com”

Parameters:

  • domain (String)

    The domain for which to get links.

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :time (hour, day, week, month, year)

    The time from which to retrieve links. Defaults to all time.

  • :limit (1..100)

    The number of links to return.

  • :before (String)

    Only return links before this identifier.

  • :after (String)

    Only return links after this identifier.

Returns:



64
65
66
67
68
69
70
71
72
# File 'lib/redditkit/client/links.rb', line 64

def links_with_domain(domain, options = {})
  options = options.clone

  parameters = { :url => domain, :t => options[:time] }
  options.merge! parameters
  options.delete :t

  objects_from_response(:get, 'api/info.json', options)
end

#mark_nsfw(link) ⇒ Object

Marks a link as not safe for work.

Parameters:

  • link (String, RedditKit::Link)

    A link’s full name, or a RedditKit::Link.



107
108
109
# File 'lib/redditkit/client/links.rb', line 107

def mark_nsfw(link)
  post 'api/marknsfw', :id => extract_full_name(link)
end

#mark_sfw(link) ⇒ Object Also known as: unmark_nsfw

Marks a link as safe for work.

Parameters:



114
115
116
# File 'lib/redditkit/client/links.rb', line 114

def mark_sfw(link)
  post 'api/unmarknsfw', :id => extract_full_name(link)
end

Gets a random link.

Returns:



136
137
138
139
140
141
142
143
# File 'lib/redditkit/client/links.rb', line 136

def random_link
  response = get('/random', nil)
  headers = response[:response_headers]
  location = headers[:location]

  link_id = location[/\/tb\/(.*)/, 1]
  link "t3_#{link_id}"
end

#submit(title, subreddit, options = {}) ⇒ Object

Submits a link or self post to reddit.

Parameters:

  • title (String)

    The title of the post.

  • subreddit (String, RedditKit::Subreddit)

    A subreddit’s display name, or a RedditKit::Subreddit.

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :url (String)

    The URL for the post. Note that if this value is present, :text will be ignored.

  • :text (String)

    The text value for the post, as Markdown.

  • :captcha_identifier (String)

    An identifier for a CAPTCHA, if the current user is required to fill one out.

  • :captcha_value (String)

    The value for the CAPTCHA with the given identifier, as filled out by the user.

  • :save (Boolean)

    If true, the link will be implicitly saved after submission.



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/redditkit/client/links.rb', line 83

def submit(title, subreddit, options = {})
  subreddit_name = extract_string subreddit, :display_name
  parameters = {
    :title => title,
    :sr => subreddit_name,
    :iden => options[:captcha_identifier],
    :captcha => options[:captcha_value],
    :save => options[:save]
    }

  if options[:url]
    parameters[:url] = options[:url]
    parameters[:kind] = 'link'
  else
    parameters[:text] = options[:text]
    parameters[:kind] = 'self'
  end

  post 'api/submit', parameters
end

#unhide(link) ⇒ Object

Unhides a link.

Parameters:

  • link (String, RedditKit::Link)

    A link’s full name, or a RedditKit::Link.



129
130
131
# File 'lib/redditkit/client/links.rb', line 129

def unhide(link)
  post 'api/unhide', :id => extract_full_name(link)
end