Module: RedditKit::Client::Flair

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

Overview

Methods for interacting with flair in subreddits.

Instance Method Summary collapse

Instance Method Details

#apply_flair_template(options) ⇒ Object

Applys a flair template to a link or user.

Parameters:

  • options (Hash)

    a customizable set of options

Options Hash (options):

  • subreddit (String, RedditKit::Subreddit)

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

  • template_id (String)

    The template’s identifier.

  • link (String, RedditKit::Link)

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

  • user (String, RedditKit::User)

    A user’s username, or a RedditKit::User.



121
122
123
124
125
126
127
128
129
# File 'lib/redditkit/client/flair.rb', line 121

def apply_flair_template(options)
  subreddit_name = extract_string(options[:subreddit], :display_name)
  link_full_name = extract_full_name options[:link]
  username = extract_string options[:user], :username

  parameters = { :flair_template_id => options[:template_id], :r => subreddit_name, :name => username, :link => link_full_name }

  post('api/selectflair', parameters)
end

#clear_flair_templates(options) ⇒ Object

Clears all flair templates of a certain type.

Parameters:

  • options (Hash)

    a customizable set of options

Options Hash (options):

  • subreddit (String, RedditKit::Subreddit)

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

  • type (user, link)

    The template’s type. Defaults to user.



105
106
107
108
109
110
111
112
113
# File 'lib/redditkit/client/flair.rb', line 105

def clear_flair_templates(options)
  subreddit_name = extract_string(options[:subreddit], :display_name)
  flair_type = 'USER_FLAIR'
  flair_type = 'LINK_FLAIR' if options[:type].to_s == 'link'

  parameters = { :r => subreddit_name, :flair_type => flair_type, :text => options[:text], :css_class => options[:css_class] }

  post('api/clearflairtemplates', parameters)
end

#create_flair_template(subreddit, type, options = {}) ⇒ Object

Creates a flair template in a subreddit.

Parameters:

  • subreddit (String, RedditKit::Subreddit)

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

  • type (user, link)

    The template’s type. Defaults to user.

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

    a customizable set of options

Options Hash (options):

  • text (String)

    The text value for the template.

  • css_class (String)

    The CSS class for the template.

  • user_editable (Boolean)

    Whether the template should be editable by users.



30
31
32
33
34
35
36
37
38
# File 'lib/redditkit/client/flair.rb', line 30

def create_flair_template(subreddit, type, options = {})
  subreddit_name = extract_string(subreddit, :display_name)
  flair_type = (type.to_s == 'link') ? 'LINK_FLAIR' : 'USER_FLAIR'

  parameters = { :r => subreddit_name, :flair_type => flair_type, :text => options[:text], :css_class => options[:css_class], :api_type => :json }
  parameters[:text_editable] = 'on' if options[:user_editable]

  post('api/flairtemplate', parameters)
end

#delete_flair_template(subreddit, template_identifier) ⇒ Object

Deletes a flair template.

Parameters:

  • subreddit (String, RedditKit::Subreddit)

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

  • template_identifier (String)

    The template’s identifier.



44
45
46
47
48
49
# File 'lib/redditkit/client/flair.rb', line 44

def delete_flair_template(subreddit, template_identifier)
  subreddit_name = extract_string(subreddit, :display_name)
  parameters = { :flair_template_id => template_identifier, :r => subreddit_name }

  post('api/deleteflairtemplate', parameters)
end

#delete_user_flair(subreddit, user) ⇒ Object

Clears a user’s flair.

Parameters:

  • subreddit (String, RedditKit::Subreddit)

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

  • user (String, RedditKit::User)

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



93
94
95
96
97
98
99
# File 'lib/redditkit/client/flair.rb', line 93

def delete_user_flair(subreddit, user)
  subreddit_name = extract_string(subreddit, :display_name)
  username = extract_string(user, :username)
  parameters = { :name => username, :r => subreddit_name }

  post('api/deleteflair', parameters)
end

#flair_list(subreddit, options = {}) ⇒ Object

Lists users and their flair in a subreddit.

Parameters:

  • subreddit (String, RedditKit::Subreddit)

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

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

    a customizable set of options

Options Hash (options):

  • :limit (1..1000)

    The number of items to return.

  • :before (String)

    Only return objects before this id.

  • :after (String)

    Only return objects after this id.



15
16
17
18
19
20
21
# File 'lib/redditkit/client/flair.rb', line 15

def flair_list(subreddit, options = {})
  subreddit_name = extract_string(subreddit, :display_name)
  list = get("/r/#{subreddit_name}/api/flairlist.json", options)
  users = list[:body][:users]

  users.collect { |user| OpenStruct.new(user) }
end

#set_flair(options) ⇒ Object

Note:

Raises RedditKit::BadClassName if any CSS classes contain invalid characters, or RedditKit::TooManyClassNames if there are too many.

Sets flair on a link or user.

Parameters:

  • options (Hash)

    a customizable set of options

Options Hash (options):

  • subreddit (String, RedditKit::Subreddit)

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

  • text (String)

    The text value for the template.

  • css_class (String)

    The CSS class for the template.

  • link (String, RedditKit::Link)

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

  • user (String, RedditKit::User)

    A user’s username, or a RedditKit::User.



67
68
69
70
71
72
73
74
75
# File 'lib/redditkit/client/flair.rb', line 67

def set_flair(options)
  subreddit_name = extract_string(options[:subreddit], :display_name)
  link_full_name = extract_full_name options[:link]
  username = extract_string options[:user], :username

  parameters = { :r => subreddit_name, :text => options[:text], :css_class => options[:css_class], :name => username, :link => link_full_name }

  post('api/flair', parameters)
end

#set_flair_options(subreddit, options = {}) ⇒ Object

Sets flair options for a subreddit.

Parameters:

  • subreddit (String, RedditKit::Subreddit)

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

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

    a customizable set of options

Options Hash (options):

  • flair_enabled (Boolean)

    Whether to enable flair for the subreddit.

  • flair_position (left, right)

    The position of user flair.

  • link_flair_position (left, right)

    The position of link flair.

  • flair_self_assign_enabled (Boolean)

    Whether users may assign their own flair.

  • link_flair_self_assign_enabled (Boolean)

    Whether users may assign their own link flair.



139
140
141
142
143
144
145
146
# File 'lib/redditkit/client/flair.rb', line 139

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

  subreddit_name = extract_string(subreddit, :display_name)
  options.merge!({ :r => subreddit_name, :uh => @modhash })

  post('api/flairconfig', options)
end

#set_flair_with_csv(subreddit, csv_string) ⇒ Object

Note:

Each line in the string should be in the format ‘user,flair-text,css_class’.

Sets a subreddit’s flair using a string formatted as CSV.

Parameters:

  • subreddit (String, RedditKit::Subreddit)

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

  • csv_string (String)

    A string in CSV format.



82
83
84
85
86
87
# File 'lib/redditkit/client/flair.rb', line 82

def set_flair_with_csv(subreddit, csv_string)
  subreddit_name = extract_string(subreddit, :display_name)
  parameters = { :r => subreddit_name, :flair_csv => csv_string }

  post('api/flaircsv', parameters)
end

#toggle_flair(subreddit, flair_enabled) ⇒ Object

Toggles flair for a subreddit.

Parameters:

  • subreddit (String, RedditKit::Subreddit)

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

  • flair_enabled (Boolean)

    Whether to enable flair for the subreddit.



55
56
57
# File 'lib/redditkit/client/flair.rb', line 55

def toggle_flair(subreddit, flair_enabled)
  post('api/setflairenabled', { :r => subreddit, :flair_enabled => flair_enabled })
end