Class: Waylon::SkillRegistry

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/waylon/skill_registry.rb

Overview

A place to track all skills known to this instance of Waylon

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#routesObject (readonly)

Returns the value of attribute routes.



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

def routes
  @routes
end

Class Method Details

.find_by_name(name) ⇒ Object



19
20
21
22
23
24
25
26
# File 'lib/waylon/skill_registry.rb', line 19

def self.find_by_name(name)
  [
    *instance.routes,
    Routes::PermissionDenied.new,
    Routes::BlackHole.new,
    Routes::Default.new
  ].find { |r| r.name == name.to_s }
end

.register(name, class_name, condition) ⇒ Route

A wrapper around the singleton #register method

Parameters:

  • name (String)

    The name of the skill in the registry

  • class_name (Class)

    The class to associate with the name

  • condition (Condition)

    Criteria for whether or not the given subclass applies

Returns:

  • (Route)

    The created Route instance



15
16
17
# File 'lib/waylon/skill_registry.rb', line 15

def self.register(name, class_name, condition)
  instance.register(name, class_name, condition)
end

.route(message) ⇒ Object



28
29
30
# File 'lib/waylon/skill_registry.rb', line 28

def self.route(message)
  instance.route(message)
end

Instance Method Details

#default_route(message) ⇒ Waylon::Route

Provides the default route based on the received message.

Parameters:

Returns:



35
36
37
# File 'lib/waylon/skill_registry.rb', line 35

def default_route(message)
  message.to_bot? ? Routes::Default.new : Routes::BlackHole.new
end

#help(user) ⇒ Hash

Gathers a Hash of help data for all routes a user is permitted to access

Parameters:

  • user (User)

    The user asking for help

Returns:

  • (Hash)


42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/waylon/skill_registry.rb', line 42

def help(user)
  data = {}
  @routes.select { |r| r.permits?(user) && r.mention_only? }.each do |permitted|
    data[permitted.destination.component_namespace] ||= []
    data[permitted.destination.component_namespace] << if permitted.help
                                                         { name: permitted.name, help: permitted.help }
                                                       else
                                                         { name: permitted.name }
                                                       end
  end

  data.reject { |_k, v| v.empty? }
end

#log(message, level = :info) ⇒ Object

Simple pass-through for logging through the Waylon Logger

Parameters:

  • message (String)

    The message to log

  • level (String, Symbol) (defaults to: :info)

    The log level for this message



59
60
61
# File 'lib/waylon/skill_registry.rb', line 59

def log(message, level = :info)
  ::Waylon::Logger.log(message, level)
end

#register(name, skill_class, condition) ⇒ Route

Add the provided Skill subclass to the registry under ‘name`

Parameters:

  • name (String)

    The name of the component in the registry

  • skill_class (Class)

    The class to associate with the name

  • condition (Condition)

    Criteria for whether or not the given subclass applies

Returns:

  • (Route)

    The created Route instance

Raises:



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

def register(name, skill_class, condition)
  raise Exceptions::ValidationError, "Must be a kind of Skill" unless skill_class.ancestors.include?(Skill)

  @routes ||= []
  @routes << Route.new(name: name.to_s, destination: skill_class, condition:)
end

#route(message) ⇒ Hash

Given a message, find a suitable skill Route for it (sorted by priority, highest first) rubocop:disable Metrics/AbcSize rubocop:disable Metrics/CyclomaticComplexity rubocop:disable Metrics/MethodLength rubocop:disable Metrics/PerceivedComplexity

Parameters:

Returns:

  • (Hash)


82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/waylon/skill_registry.rb', line 82

def route(message)
  route = nil
  message_text = message.body.strip
  @routes ||= []
  @routes.sort_by(&:priority).reverse.each do |this_route|
    if this_route.permits?(message.author) &&
       this_route.matches?(message_text) &&
       (this_route.properly_mentions?(message) || message.private?)
      route = this_route
    elsif this_route.permits?(message.author) &&
          this_route.matches?(message_text) &&
          !this_route.properly_mentions?(message)
      # Black hole these because they're not direct mentions
      route = Routes::BlackHole.new
    elsif this_route.matches?(message_text)
      route = Routes::PermissionDenied.new
    end
    if route
      log("Using route '#{route.name}' based on '#{message_text}' from '#{message.author.email}'", :debug)
      break
    end
  end
  route
end