Class: Togglehq::Notify::UserPreferences

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
lib/togglehq/notify/user_preferences.rb

Instance Method Summary collapse

Instance Method Details

#categoriesObject

Gets the current preferences for this user.



8
9
10
# File 'lib/togglehq/notify/user_preferences.rb', line 8

def categories
  @categories ||= reload!
end

#disable_preference!(category_key, preference_key) ⇒ Object

Disables a preference for this user.

Parameters:

  • cateogry_key (String)

    the key of the preference category

  • preference_key (String)

    the key of the preference within the category

Raises:

  • (RuntimeError)

    raised if either the category or preference keys are invalid



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/togglehq/notify/user_preferences.rb', line 38

def disable_preference!(category_key, preference_key)
  response = Togglehq::Request.new("/preferences/disable",
                                   {"user" => {"identifier" => self.identifier}, "category" => category_key, "preference" => preference_key}).patch!
  if response.status == 200
    if @categories
      category = @categories.find {|g| g["key"] == category_key}
      preference = category["preferences"].find {|s| s["key"] == preference_key} if category
      preference["enabled"] = false if preference
    end
    return true
  elsif response.status == 404
    json = JSON.parse(response.body)
    raise json["message"]
  else
    raise "unexpected error disabling preference"
  end
end

#enable_preference!(category_key, preference_key) ⇒ Object

Enables a preference for this user.

Parameters:

  • category_key (String)

    the key of the preference category

  • preference_key (String)

    the key of the preference within the category

Raises:

  • (RuntimeError)

    raised if either the category or preference keys are invalid



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/togglehq/notify/user_preferences.rb', line 16

def enable_preference!(category_key, preference_key)
  response = Togglehq::Request.new("/preferences/enable",
                                   {"user" => {"identifier" => self.identifier}, "category" => category_key, "preference" => preference_key}).patch!
  if response.status == 200
    if @categories
      category = @categories.find {|g| g["key"] == category_key}
      preference = category["preferences"].find {|s| s["key"] == preference_key} if category
      preference["enabled"] = true if preference
    end
    return true
  elsif response.status == 404
    json = JSON.parse(response.body)
    raise json["message"]
  else
    raise "unexpected error enabling preference"
  end
end

#reload!Object

Reloads this UserPreferences from the ToggleHQ API



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/togglehq/notify/user_preferences.rb', line 57

def reload!
  response = Togglehq::Request.new("/preferences", {"user" => {"identifier" => self.identifier}}).get!
  if response.status == 200
    @categories = JSON.parse(response.body)
    return @categories
  elsif response.status == 404
    raise "user not found"
  else
    raise "Unexpected error getting user preferences"
  end
end