Class: ClientBlacklist

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/kill_switch/client_blacklist.rb

Defined Under Namespace

Classes: BlacklistFilter

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.agent_blacklisted?(user_agent_string) ⇒ Boolean

Returns:

  • (Boolean)

Raises:

  • (RuntimeError)


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/kill_switch/client_blacklist.rb', line 28

def self.agent_blacklisted?(user_agent_string)
  agent = UserAgent.new(user_agent_string)
  raise RuntimeError, "User Agent Invalid" unless agent.valid?

  base_rules = rules.fetch("blacklist", {}).fetch(agent.app_version, {}).fetch(agent.device_platform, nil)
  # no rules found for version and device
  return false if base_rules.nil?

  if device_os_version_rules = base_rules[agent.device_os_version]
    if device_os_version_rules.empty?
      # No further criteria to match on. Found final match on device_os_version
      return true
    end
    if device_os_version_rules[agent.device_model]
      # Found a match on the nested device model
      return true
    end
  end

  if base_rules[agent.device_model]
    # 3 rules, excluding devise_os_version
    return true
  end

  if base_rules.any? # this means we have nested rules, not just the version and platform
    return false # we have nested rules, nothing matched - good to go
  end

  return true # If we made it this far, we matched on the base_rules only
end

.clear_rulesObject



16
17
18
# File 'lib/kill_switch/client_blacklist.rb', line 16

def self.clear_rules
  instance.instance_variable_set(:@rules, {})
end

.configure {|instance| ... } ⇒ Object

Yields:

  • (instance)

Raises:

  • (ArgumentError)


7
8
9
10
# File 'lib/kill_switch/client_blacklist.rb', line 7

def self.configure(&block)
  raise ArgumentError, "Block not provided" unless block_given?
  yield instance
end

.rulesObject



12
13
14
# File 'lib/kill_switch/client_blacklist.rb', line 12

def self.rules
  instance.rules
end

.user_agent_valid?(user_agent_string) ⇒ Boolean

Returns:

  • (Boolean)


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

def self.user_agent_valid?(user_agent_string)
  UserAgent.new(user_agent_string).valid?
end

Instance Method Details

#blacklist(app_version, device_platform, device_os_version = nil, device_model = nil) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/kill_switch/client_blacklist.rb', line 59

def blacklist(app_version, device_platform, device_os_version = nil, device_model = nil)
  rules["blacklist"] ||= {}
  rules["blacklist"][app_version] ||= {}
  rules["blacklist"][app_version][device_platform] ||= {}

  if device_os_version
    rules["blacklist"][app_version][device_platform][device_os_version] ||= {}
  end
  if device_model
    if device_os_version
      # all 4 rules included - nest under device_os_version
      rules["blacklist"][app_version][device_platform][device_os_version][device_model] ||= {}
    else
      # device_os_version excluded - rule is to exclude a particular device_platform
      rules["blacklist"][app_version][device_platform][device_model] ||= {}
    end
  end
end

#rulesObject



20
21
22
# File 'lib/kill_switch/client_blacklist.rb', line 20

def rules
  @rules ||= {}
end