Sha256: d01f79bcb88cea96bf85fb8736dc9e8de50223f0999a9b119859438d1446a2fe

Contents?: true

Size: 1.15 KB

Versions: 2

Compression:

Stored size: 1.15 KB

Contents

require 'set'

module DeviceMap
  module DeviceData
    class Patterns
      # rubocop:disable Metrics/MethodLength
      def self.parse(openddr_builder_xml)
        builders_doc = Nokogiri::XML(openddr_builder_xml)
        openddr_builders = builders_doc.xpath('//builder')

        all_patterns = openddr_builders.flat_map do |builder_node|
          builder_node_class = builder_node[:class]
          builder = Builder.find(builder_node_class)

          builder_node.xpath('device').flat_map do |device_node|
            device_id = device_node[:id]
            keywords = device_node.xpath('list/value').map(&:content)
            builder.patterns(device_id, keywords)
          end
        end

        new(all_patterns)
      end

      def initialize(all_patterns)
        @pattern_index = {}

        all_patterns.each do |pattern|
          pattern.keywords.each do |keyword|
            @pattern_index[keyword] ||= Set.new
            @pattern_index[keyword] << pattern
          end
        end
      end

      def find(keyword)
        # TODO: Return copy of the set
        @pattern_index.fetch(keyword) do
          Set.new
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
device_map-0.1.1 lib/device_map/device_data/patterns.rb
device_map-0.1.0 lib/device_map/device_data/patterns.rb