Module: RedditKit::Client::Multireddits

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

Overview

Methods for interacting with multireddits.

Instance Method Summary collapse

Instance Method Details

#add_subreddit_to_multireddit(multireddit, subreddit) ⇒ Object

Add a subreddit to a multireddit owned by the current user.

Parameters:



157
158
159
160
161
162
163
164
165
166
# File 'lib/redditkit/client/multireddits.rb', line 157

def add_subreddit_to_multireddit(multireddit, subreddit)
  multireddit_name = extract_string multireddit, :name
  subreddit_name = extract_string subreddit, :name

  multireddit_path = path_for_multireddit @username, multireddit_name
  path = "api/multi#{multireddit_path}/r/#{subreddit_name}"
  model = { :name => subreddit_name }

  put path, :model => model.to_json
end

#copy_multireddit(multireddit, copied_name) ⇒ Object #copy_multireddit(user, multireddit_name, copied_name) ⇒ Object

Copies a multireddit.

Overloads:

  • #copy_multireddit(multireddit, copied_name) ⇒ Object

    Parameters:

    • multireddit (String, RedditKit::Multireddit)

      The name of the multireddit, or a RedditKit::Multireddit.

    • copied_name (String)

      The copied name for the multireddit.

  • #copy_multireddit(user, multireddit_name, copied_name) ⇒ Object

    Parameters:

    • user (String, RedditKit::User)

      The name of the user who owns the multireddit.

    • multireddit_name (String)

      The name of the multireddit.

    • copied_name (String)

      The copied name for the multireddit.



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/redditkit/client/multireddits.rb', line 108

def copy_multireddit(*args)
  copied_name = args.pop
  multireddit_name = extract_string(args.pop, :name)
  user = args.first

  if user.nil?
    user = @username
  else
    user = extract_string(user, :username)
  end

  target_multireddit_path = path_for_multireddit user, multireddit_name
  destination_multireddit_path = path_for_multireddit user, copied_name
  parameters = { :from => target_multireddit_path, :to => destination_multireddit_path }

  post('api/multi/copy', parameters)
end

#create_multireddit(multireddit, subreddits = [], visibility = 'private') ⇒ Object

Creates a new multireddit.

Parameters:

  • multireddit (String, RedditKit::Multireddit)

    The name of the multireddit, or a RedditKit::Multireddit.

  • subreddits (Array) (defaults to: [])

    An array of subreddit names or RedditKit::Subreddit objects.

  • visibility (public, private) (defaults to: 'private')

    An array of subreddit names to be added to the multireddit.



86
87
88
# File 'lib/redditkit/client/multireddits.rb', line 86

def create_multireddit(multireddit, subreddits = [], visibility = 'private')
  create_or_update_multireddit(:post, multireddit, subreddits, visibility)
end

#delete_multireddit(multireddit) ⇒ Object

Delete a multireddit.

Parameters:



143
144
145
146
147
148
149
150
151
# File 'lib/redditkit/client/multireddits.rb', line 143

def delete_multireddit(multireddit)
  multireddit_name = extract_string(multireddit, :name)

  multireddit_path = path_for_multireddit @username, multireddit_name
  path = "api/multi#{multireddit_path}"
  parameters = { :multireddit => path_for_multireddit(@username, multireddit_name) }

  delete_path(path, parameters)
end

#multireddit(path) ⇒ RedditKit::Multireddit #multireddit(username, multireddit_name) ⇒ RedditKit::Multireddit

Fetch a single multireddit.

Overloads:

  • #multireddit(path) ⇒ RedditKit::Multireddit

    Parameters:

    • path (String)

      The multireddit’s path.

  • #multireddit(username, multireddit_name) ⇒ RedditKit::Multireddit

    Parameters:

    • username (String)

      The username of the user who owns the multireddit.

    • multireddit_name (String)

      The multireddit’s name.

Returns:



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/redditkit/client/multireddits.rb', line 26

def multireddit(*args)
  path = 'api/multi'

  if args.length == 1
    path << args.first
  else
    path << path_for_multireddit(args.first, args.last << '.json')
  end

  object_from_response(:get, path, nil)
end

#multireddit_description(multireddit) ⇒ Object #multireddit_description(user, multireddit_name) ⇒ Object

Fetches the description for a multireddit.

Overloads:

  • #multireddit_description(multireddit) ⇒ Object

    Parameters:

  • #multireddit_description(user, multireddit_name) ⇒ Object

    Parameters:

    • user (String, RedditKit::User)

      The name of the user who owns the multireddit.

    • multireddit_name (String)

      The name of the multireddit.

Raises:

  • (RedditKit::NotAuthenticated)

    if there is not a user signed in.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/redditkit/client/multireddits.rb', line 46

def multireddit_description(*args)
  raise RedditKit::NotAuthenticated unless signed_in?

  multireddit = args.pop
  user = args.first
  multireddit_path = nil

  if user.nil?
    multireddit_path = multireddit.path
  else
    username = extract_string(user, :username)
    multireddit_path = path_for_multireddit username, multireddit
  end

  object_from_response(:get, "api/multi#{multireddit_path}/description", nil)
end

#my_multiredditsArray<RedditKit::Multireddit>

Fetch the current user’s multireddits.

Returns:



14
15
16
# File 'lib/redditkit/client/multireddits.rb', line 14

def my_multireddits
  objects_from_response(:get, 'api/multi/mine.json', nil)
end

#remove_subreddit_from_multireddit(multireddit, subreddit) ⇒ Object

Removes a subreddit from a multireddit owned by the current user.

Parameters:



172
173
174
175
176
177
178
179
180
# File 'lib/redditkit/client/multireddits.rb', line 172

def remove_subreddit_from_multireddit(multireddit, subreddit)
  multireddit_name = extract_string multireddit, :name
  subreddit_name = extract_string subreddit, :name

  multireddit_path = path_for_multireddit @username, multireddit_name
  path = "api/multi#{multireddit_path}/r/#{subreddit_name}"

  delete_path(path, nil)
end

#rename_multireddit(from, to) ⇒ Object

Rename a multireddit owned by the current user.

Parameters:

  • from (String)

    The multireddit’s current name.

  • to (String)

    The new name for the multireddit.



130
131
132
133
134
135
136
137
138
# File 'lib/redditkit/client/multireddits.rb', line 130

def rename_multireddit(from, to)
  old_multireddit_path = path_for_multireddit @username, from
  new_multireddit_path = path_for_multireddit @username, to

  parameters = { :from => old_multireddit_path, :to => new_multireddit_path }
  response = post('api/multi/rename', parameters)

  RedditKit::Multireddit.new response[:body]
end

#set_multireddit_description(multireddit, description) ⇒ RedditKit::MultiredditDescription

Sets the description for a multireddit.

Parameters:

  • multireddit (String, RedditKit::Multireddit)

    The name of the multireddit, or a RedditKit::Multireddit.

  • description (String)

    The new description for the subreddit.

Returns:



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/redditkit/client/multireddits.rb', line 68

def set_multireddit_description(multireddit, description)
  multireddit_name = extract_string(multireddit, :name)
  multireddit_path = path_for_multireddit username, multireddit_name

  model = { :body_md => description }
  parameters = { :multipath => multireddit_path, :model => model.to_json }
  path = "api/multi#{multireddit_path}/description"

  response = put path, parameters

  RedditKit::MultiredditDescription.new response[:body]
end

#update_multireddit(multireddit, subreddits = [], visibility = 'private') ⇒ Object

Updates an existing multireddit.

Parameters:

  • multireddit (String, RedditKit::Multireddit)

    The name of the multireddit, or a RedditKit::Multireddit.

  • subreddits (Array) (defaults to: [])

    An array of subreddit names or RedditKit::Subreddit objects.

  • visibility (public, private) (defaults to: 'private')

    An array of subreddit names to be added to the multireddit.



95
96
97
# File 'lib/redditkit/client/multireddits.rb', line 95

def update_multireddit(multireddit, subreddits = [], visibility = 'private')
  create_or_update_multireddit :put, multireddit, subreddits, visibility
end