Sha256: 0830d39199e293a6bb655df8d4f495827f361f344ded9a8fc669b2ef707e7db7

Contents?: true

Size: 1.17 KB

Versions: 4

Compression:

Stored size: 1.17 KB

Contents

require 'lita/handlers/enhance/node_index'

module Lita
  module Handlers
    class Enhance
      class MacAddressEnhancer < Enhancer
        # TODO: enhance unknown MAC address with OUI names http://standards.ieee.org/develop/regauth/oui/oui.txt

        MAC_ADDRESS_REGEX = /([0-9a-f]{2}:){5}[0-9a-f]{2}/i

        def initialize(redis)
          super
          @nodes_by_mac_address = NodeIndex.new(redis, 'nodes_by_mac_address')
        end

        def index(mac_address, node)
          @nodes_by_mac_address[mac_address.downcase] = node
        end

        def enhance!(string, level)
          substitutions = []
          string.scan(MAC_ADDRESS_REGEX) do
            match = Regexp.last_match
            mac_address = match.to_s
            range = (match.begin(0)...match.end(0))

            node = @nodes_by_mac_address[mac_address.downcase]
            if node
              new_text = render(node, level)
              substitutions << Substitution.new(range, new_text)
            end
          end
          substitutions
        end

        def to_s
          "#{self.class.name}: #{@nodes_by_mac_address.size} MAC addresses indexed"
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
lita-enhance-0.9.3 lib/lita/handlers/enhance/enhancers/mac_address_enhancer.rb
lita-enhance-0.9.2 lib/lita/handlers/enhance/enhancers/mac_address_enhancer.rb
lita-enhance-0.9.1 lib/lita/handlers/enhance/enhancers/mac_address_enhancer.rb
lita-enhance-0.9.0 lib/lita/handlers/enhance/enhancers/mac_address_enhancer.rb