Module: Snoo::Flair

Defined in:
lib/snoo/flair.rb

Overview

Flair related methods. These are used for interacting with flair in reddit. Things like giving flair, setting templates, and whatnot.

Author:

Instance Method Summary collapse

Instance Method Details

#clear_flair_templates(type, subreddit) ⇒ Object

Clear all the flair templates of a particular type

Parameters:

  • type (USER_FLAIR, LINK_FLAIR)

    The type of template to clear.

  • subreddit (String)

    The subreddit targeted.



13
14
15
16
# File 'lib/snoo/flair.rb', line 13

def clear_flair_templates type, subreddit
  logged_in?
  post('/api/clearflairtemplates', body: { flair_type: type, r: subreddit, uh: @modhash, api_type: 'json'})
end

#delete_flair_template(id, subreddit) ⇒ Object

Deletes a flair template by ID.

Parameters:

  • id (String)

    The flair template's ID to delete.

  • subreddit (String)

    The subreddit targeted.



32
33
34
35
# File 'lib/snoo/flair.rb', line 32

def delete_flair_template id, subreddit
  logged_in?
  post('/api/deleteflairtemplate', body: {flair_template_id: id, r: subreddit, uh: @modhash, api_type: 'json'})
end

#delete_user_flair(user, subreddit) ⇒ Object

Deletes a user's flair

Parameters:

  • user (String)

    The user who'se flair is affected

  • subreddit (String)

    The subreddit targeted.



23
24
25
26
# File 'lib/snoo/flair.rb', line 23

def delete_user_flair user, subreddit
  logged_in?
  post('/api/deleteflair', body: {name: user, r: subreddit, uh: @modhash, api_type: 'json'})
end

#flair(subreddit, opts = {}) ⇒ Object

Sets flair on a thing, currently supports links and users. Must specify either link or user, not both

Parameters:

  • subreddit (String)

    The subreddit targeted.

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

    An options hash.

Options Hash (opts):

  • :css_class (String)

    The class(es) applied to the flair. Whitespace separated

  • :text (String)

    The flair text

  • :name (String)

    The user who we are flairing. This requires a username

  • :link (String)

    The thing id of the link (if a link). Begins with t3_



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/snoo/flair.rb', line 46

def flair subreddit, opts = {}
  logged_in?
  params = {
    r: subreddit,
    uh: @modhash,
    api_type: 'json'
  }
  params.merge! opts

  post('/api/flair', body: params)
end

#flair_config(subreddit, opts = {}) ⇒ Object

Configures flair options for a subreddit. All options are required

Parameters:

  • subreddit (String)

    The subreddit targeted.

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

    An options hash

Options Hash (opts):

  • :flair_enabled (true, false) — default: true

    Flair enabled?

  • :flair_position (left, right) — default: 'right'

    Position of user flair.

  • :flair_self_assign_enabled (true, false) — default: false

    Allow users to assign their own flair from templates

  • :link_flair_position (none, left, right) — default: 'right'

    The position of link flair. Set to none to disable

  • :link_flair_self_assign_enabled (true, false) — default: false

    Allow a submitter to assign their own link flair



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/snoo/flair.rb', line 68

def flair_config subreddit, opts = {}
  logged_in?
  options = {
    flair_enabled: true,
    flair_position: 'right',
    flair_self_assign_enabled: false,
    link_flair_position: 'right',
    link_flair_self_assign_enabled: false,
    uh: @modhash,
    r: subreddit,
    api_type: 'json'
  }
  options.merge! opts

  post('/api/flairconfig', body: options)
end

#flair_csv(csv, subreddit) ⇒ HTTParty::Response

Post flair in a CSV file to reddit

Parameters:

  • csv (String)

    A string, in CSV format, of user,flair-text,css_class per line, with no more than 100 flairs, and no header line.

  • subreddit (String)

    The subreddit targeted.

Returns:

  • (HTTParty::Response)

    The request object. Note that this request object contains a json confirming the status of each line of the CSV



90
91
92
93
# File 'lib/snoo/flair.rb', line 90

def flair_csv csv, subreddit
  logged_in?
  post('/api/flaircsv.json', body: {flair_csv: csv, r: subreddit, uh: @modhash})
end

#flair_template(subreddit, opts = {}) ⇒ Object

Create or edit a flair template.

Parameters:

  • subreddit (String)

    The subreddit targeted.ate allows users to specify their own text

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

    An options hash

Options Hash (opts):

  • css_class (String)

    The list of css classes applied to this style, space separated

  • flair_type (USER_FLAIR, LINK_FLAIR) — default: 'USER_FLAIR'

    The type of flair template.

  • text (String)

    The flair template's text.

  • text_editable (true, false) — default: false

    If the user is allowed to edit their flair text

  • template_id (String)

    The flair template ID, for editing.



123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/snoo/flair.rb', line 123

def flair_template subreddit, opts = {}
  logged_in?
  params = {
    flair_type: 'USER_FLAIR',
    text_editable: false,
    uh: @modhash,
    r: subreddit,
    api_type: 'json'
  }
  params.merge! opts

  post('/api/flairtemplate', body: params)
end

#flair_toggle(enabled, subreddit) ⇒ Object

Toggle flair on and off for a subreddit

Parameters:

  • enabled (true, false)

    Enable/disable flair

  • subreddit (String)

    The subreddit targeted.



163
164
165
166
# File 'lib/snoo/flair.rb', line 163

def flair_toggle enabled, subreddit
  logged_in?
  post('/api/setflairenabled', body: {flair_enabled: enabled, uh: @modhash, r: subreddit, api_type: 'json'})
end

#get_flair_list(subreddit, opts = {}) ⇒ Object

Downloads flair from the subreddit This is limited to 1000 per request, use before/after to get "pages"

Parameters:

  • subreddit (String)

    The subreddit targeted.

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

    An options hash.

Options Hash (opts):

  • :limit (Fixnum) — default: 1000

    The amount of flairs to get. Must be between 1 and 1000

  • :before (String)

    Return entries just before this user id

  • :after (String)

    Return entries just after this user id



103
104
105
106
107
108
109
110
111
# File 'lib/snoo/flair.rb', line 103

def get_flair_list subreddit, opts = {}
  logged_in?
  query = {
    limit: 1000,
    uh: @modhash
  }
  query.merge! opts
  get("/r/#{subreddit}/api/flairlist.json", query: query)
end

#select_flair_template(template_id, subreddit, opts = {}) ⇒ Object

Select a flair template and apply it to a user or link

Parameters:

  • template_id (String)

    The template id to apply.

  • subreddit (String)

    The subreddit targeted.

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

    a customizable set of options

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

    An options hash

Options Hash (opts):

  • :link (String)

    The link id to apply to

  • :user (String)

    The username to apply flair to

  • :text (String)

    The flair text



146
147
148
149
150
151
152
153
154
155
156
# File 'lib/snoo/flair.rb', line 146

def select_flair_template template_id, subreddit, opts = {}
  logged_in?
  params = {
    flair_template_id: template_id,
    uh: @modhash,
    r: subreddit,
    api_type: 'json'
  }
  params.merge! opts
  post('/api/selectflair', body: params)
end