Sha256: 083c9c4303a746eff43e8c14958856732d4f671d555d17ef4cd76b090e79dc78

Contents?: true

Size: 1.68 KB

Versions: 1

Compression:

Stored size: 1.68 KB

Contents

# frozen_string_literal: true

module Nmap
  class XML
    module CPE
      #
      # Represents a [Common Platform Enumeration (CPE)][CPE] URL.
      #
      # [CPE]: http://nmap.org/book/output-formats-cpe.html
      #
      # @since 1.0.0
      #
      class URL < Struct.new(:part,:vendor,:product,:version,:update,:edition,
                             :language)

        # CPE part codes
        PARTS = {
          '/a' => :application,
          '/h' => :hardware,
          '/o' => :os
        }

        #
        # Parses a CPE URL.
        #
        # @param [String] url
        #   The raw URL.
        #
        # @return [URL]
        #   The parsed URL.
        #
        def self.parse(url)
          scheme,
            part,
            vendor,
            product,
            version,
            update,
            edition,
            language = url.split(':',8)

          unless scheme == 'cpe'
            raise(ArgumentError,"CPE URLs must begin with 'cpe:'")
          end

          vendor   = vendor.to_sym
          product  = product.to_sym
          language = language.to_sym if language

          return new(
            PARTS[part],
            vendor,
            product,
            version,
            update,
            edition,
            language
          )
        end

        #
        # Converts the CPE URL back into a String.
        #
        # @return [String]
        #   The raw CPE URL.
        #
        def to_s
          'cpe:' + [
            PARTS.invert[part],
            vendor,
            product,
            version,
            update,
            edition,
            language
          ].compact.join(':')
        end

      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ruby-nmap-1.0.3 lib/nmap/xml/cpe/url.rb