Class: DungeonMaster::Decider

Inherits:
Object
  • Object
show all
Defined in:
lib/dungeon_master.rb

Instance Method Summary collapse

Constructor Details

#initialize(redis = $redis) ⇒ Decider

Returns a new instance of Decider.



8
9
10
# File 'lib/dungeon_master.rb', line 8

def initialize(redis = $redis)
  @redis = redis
end

Instance Method Details

#activate(browser, min_version, group = "main") ⇒ Object



12
13
14
15
# File 'lib/dungeon_master.rb', line 12

def activate(browser, min_version, group = "main")
  @redis.sadd("dungeon_master/groups", group)
  @redis.sadd("dungeon_master/active/#{group}", "#{browser}/#{min_version}")
end

#active?(user_agent, group = "main") ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
20
21
22
# File 'lib/dungeon_master.rb', line 17

def active?(user_agent, group = "main")
  current_browser = UserAgent.parse(user_agent)
  supported_browsers(group).any? do |browser|
    current_browser >= browser && !current_browser.mobile?
  end
end

#groupsObject



24
25
26
# File 'lib/dungeon_master.rb', line 24

def groups
  @redis.smembers("dungeon_master/groups")
end

#supported_browsers(group = "main") ⇒ Object



28
29
30
31
32
33
# File 'lib/dungeon_master.rb', line 28

def supported_browsers(group = "main")
  @redis.smembers("dungeon_master/active/#{group}").map do |string|
    browser_version = string.split("/")
    Browser.new(browser_version.first, browser_version.last)
  end
end