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.

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.



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.



118
119
120
# File 'lib/redditkit/client/links.rb', line 118

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.



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.

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.



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”


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.



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.



103
104
105
# File 'lib/redditkit/client/links.rb', line 103

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.



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

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

Gets a random link.



132
133
134
135
136
137
138
139
# File 'lib/redditkit/client/links.rb', line 132

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.

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.



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/redditkit/client/links.rb', line 79

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.



125
126
127
# File 'lib/redditkit/client/links.rb', line 125

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