Class: Waylon::Condition Abstract

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

Overview

This class is abstract.

Abstract route condition superclass

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mechanism, action, allowed_groups, help = nil, mention_only = true) ⇒ Condition

rubocop:disable Style/OptionalBooleanParameter

Parameters:

  • mechanism

    The meat of the condition used to decide if this condition applies

  • action (Symbol)

    The method to call if the condition matches

  • allowed_groups (Array<Symbol>)

    The group names allowed to use this action

  • help (String) (defaults to: nil)

    Optional help text to describe usage for this action

  • mention_only (Boolean) (defaults to: true)

    Only applies to messages that directly mention (or IM) this bot



15
16
17
18
19
20
21
# File 'lib/waylon/condition.rb', line 15

def initialize(mechanism, action, allowed_groups, help = nil, mention_only = true)
  @mechanism = mechanism
  @action = action
  @allowed_groups = allowed_groups
  @help = help
  @mention_only = mention_only
end

Instance Attribute Details

#actionObject (readonly)

Returns the value of attribute action.



7
8
9
# File 'lib/waylon/condition.rb', line 7

def action
  @action
end

#helpObject (readonly)

Returns the value of attribute help.



7
8
9
# File 'lib/waylon/condition.rb', line 7

def help
  @help
end

#mechanismObject (readonly)

Returns the value of attribute mechanism.



7
8
9
# File 'lib/waylon/condition.rb', line 7

def mechanism
  @mechanism
end

Instance Method Details

#matches?(_input) ⇒ Boolean

Placeholder for determining if this condition applies to the given input

Parameters:

Returns:

  • (Boolean)


27
28
29
# File 'lib/waylon/condition.rb', line 27

def matches?(_input)
  false
end

#mention_only?Boolean

Is this condition only valid for Messages that directly mention the bot?

Returns:

  • (Boolean)


33
34
35
# File 'lib/waylon/condition.rb', line 33

def mention_only?
  @mention_only
end

#named_tokens(_input) ⇒ Hash<String,Object>

Placeholder for optionally providing named tokens

Parameters:

  • _input (String)

    The message content

Returns:

  • (Hash<String,Object>)


40
41
42
# File 'lib/waylon/condition.rb', line 40

def named_tokens(_input)
  {}
end

#permits?(user) ⇒ Boolean

Checks if a user is allowed based on this condition

Parameters:

Returns:

  • (Boolean)


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/waylon/condition.rb', line 46

def permits?(user)
  return true if allows?(:everyone)

  Logger.log("Checking permissions for #{user.email}", :debug)
  group_class = user.class.sense.group_class
  # Check for global admins
  return true if Config.instance.admins.include?(user.email)
  # Check for managed admins
  return true if group_class.new("admins").include?(user)

  permitted = false

  [*@allowed_groups].each do |group|
    permitted = true if group_class.new(group).include?(user)
    break if permitted
  end
  permitted
end

#properly_mentions?(message) ⇒ Boolean

Determines of a message complies with the #mention_only? setting for this condition

Parameters:

Returns:

  • (Boolean)


68
69
70
71
72
73
# File 'lib/waylon/condition.rb', line 68

def properly_mentions?(message)
  return true unless mention_only?

  message.to_bot?
  # (mention_only? && message.to_bot?) || (!mention_only? && !message.to_bot?)
end

#tokens(_input) ⇒ Array<String>

Tokens is used to provide details about the message input to the action

Parameters:

  • _input (String)

    The message content as text

Returns:

  • (Array<String>)

    The tokens extracted from the input message



78
79
80
# File 'lib/waylon/condition.rb', line 78

def tokens(_input)
  []
end