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)

    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.



109
110
111
# File 'lib/redditkit/client/links.rb', line 109

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:



48
49
50
51
# File 'lib/redditkit/client/links.rb', line 48

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)

    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
# File 'lib/redditkit/client/links.rb', line 30

def links(subreddit, options = {})
  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:



62
63
64
65
66
67
68
# File 'lib/redditkit/client/links.rb', line 62

def links_with_domain(domain, options = {})
  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.



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

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:



101
102
103
# File 'lib/redditkit/client/links.rb', line 101

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

Gets a random link.

Returns:



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

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.



78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/redditkit/client/links.rb', line 78

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] }
  
  if options[:url]
    parameters[:url] = options[:url]
  else
    parameters[:text] = options[:text]
  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.



116
117
118
# File 'lib/redditkit/client/links.rb', line 116

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