Module: Redd::Client::Authenticated::Flair

Included in:
Redd::Client::Authenticated
Defined in:
lib/redd/client/authenticated/flair.rb

Instance Method Summary collapse

Instance Method Details

#get_flair(subreddit, user) ⇒ Object



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

def get_flair(subreddit, user)
  username = extract_attribute(user, :name)
  options = {name: username}

  flair = get_flair_list(subreddit, options).first
  flair if flair[:user].casecmp(username.downcase) == 0
end

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



5
6
7
8
9
10
11
12
# File 'lib/redd/client/authenticated/flair.rb', line 5

def get_flair_list(subreddit, params = {})
  name = extract_attribute(subreddit, :display_name)

  path = "/api/flairlist.json"
  path = path.prepend("/r/#{name}")

  get(path, params)[:users]
end

#set_flair(subreddit, user_or_link, text = "", css_class = "") ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/redd/client/authenticated/flair.rb', line 23

def set_flair(subreddit, user_or_link, text = "", css_class = "")
  name = extract_attribute(subreddit, :display_name)

  path = "/api/flair"
  path = path.prepend("/r/#{name}")
  params = {api_type: "json", text: text, css_class: css_class}

  case user_or_link
  when Redd::Object::User
    params[:name] = extract_attribute(user_or_link, :name)
  when Redd::Object::Submission
    params[:link] = extract_attribute(user_or_link, :display_name)
  else
    fail "You should provide a User or Submission object."
  end

  post path, params
end