Module: NeonRAW::Objects::Subreddit::Utilities

Included in:
NeonRAW::Objects::Subreddit
Defined in:
lib/NeonRAW/objects/subreddit/utilities.rb

Overview

Utilities for subreddits.

Instance Method Summary collapse

Instance Method Details

#info(params = {}) ⇒ NeonRAW::Objects::Comment/Submission/Subreddit

Get info on a link/comment/subreddit.

Parameters:

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

    The parameters.

Options Hash (params):

  • :id (String)

    The fullname of the thing.

  • :url (String)

    The URL of the thing.

Returns:



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/NeonRAW/objects/subreddit/utilities.rb', line 13

def info(params = {})
  path = "/r/#{display_name}/api/info"
  data = @client.request_data(path, :get, params)
  case data[:data][:children][0][:kind]
  when 't1'
    Comment.new(@client, data[:data][:children][0][:data])
  when 't3'
    Submission.new(@client, data[:data][:children][0][:data])
  when 't5'
    Subreddit.new(@client, data[:data][:children][0][:data])
  end
end

Gets recommended subreddits for the subreddit.

Parameters:

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

    Optional parameters.

Options Hash (opts):

  • :omit (String)

    A comma-separated list of subreddits to omit from the results.

Returns:

  • (Array<String>)

    Returns a list of the recommended subreddits.



51
52
53
54
55
56
# File 'lib/NeonRAW/objects/subreddit/utilities.rb', line 51

def recommended_subreddits(opts = {})
  params = { omit: opts[:omit], srnames: display_name }
  path = "/api/recommend/sr/#{display_name}"
  data = @client.request_data(path, :get, params)
  data.map { |subreddit| subreddit[:sr_name] }
end

#submit(title, params = {}) ⇒ NeonRAW::Objects::Submission

Submit a thread to the subreddit.

Parameters:

  • title (String)

    The title of the submission (300 characters maximum).

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

    The parameters.

Options Hash (params):

  • :text (String)

    The text of the submission (selfpost).

  • :url (String)

    The URL of the submission (link post).

Returns:



34
35
36
37
38
39
40
41
42
43
# File 'lib/NeonRAW/objects/subreddit/utilities.rb', line 34

def submit(title, params = {})
  params[:kind] = 'self' if params[:text]
  params[:kind] = 'link' if params[:url]
  params[:sr] = display_name
  params[:title] = title
  response = @client.request_data('/api/submit', :post, params)
  # Seriously though, fucking convoluted data structures.
  submission_id = response[:jquery].last[3].first.split('/')[6]
  info(id: 't3_' + submission_id)
end

#subscribe!Object

Toggle your subscription to the subreddit.



61
62
63
64
65
66
67
68
69
# File 'lib/NeonRAW/objects/subreddit/utilities.rb', line 61

%w(subscribe unsubscribe).each do |type|
  define_method :"#{type}!" do
    params = { sr: name }
    params[:action] = 'sub' if type == 'subscribe'
    params[:action] = 'unsub' if type == 'unsubscribe'
    @client.request_data('/api/subscribe', :post, params)
    refresh!
  end
end

#unsubscribe!Object

Toggle your subscription to the subreddit.



61
62
63
64
65
66
67
68
69
# File 'lib/NeonRAW/objects/subreddit/utilities.rb', line 61

%w(subscribe unsubscribe).each do |type|
  define_method :"#{type}!" do
    params = { sr: name }
    params[:action] = 'sub' if type == 'subscribe'
    params[:action] = 'unsub' if type == 'unsubscribe'
    @client.request_data('/api/subscribe', :post, params)
    refresh!
  end
end