Sha256: 36602080813d5c547b9bdb99fe0caca2b9ffde70eca16b59339e9b561e29c789

Contents?: true

Size: 904 Bytes

Versions: 1

Compression:

Stored size: 904 Bytes

Contents

require 'vedeu/parsing/compositor'
require 'vedeu/parsing/hash_parser'
require 'vedeu/parsing/json_parser'
require 'vedeu/parsing/menu_parser'

module Vedeu
  ParseError = Class.new(StandardError)

  class Parser
    def self.parse(output = {})
      return nil if output.nil? || output.empty?

      new(output).parse
    end

    def initialize(output = {})
      @output = output
    end

    def parse
      Compositor.enqueue(parsed_output)
    end

    private

    attr_reader :output

    def parsed_output
      @parsed ||= parser.parse(output)
    end

    def parser
      return JSONParser if json?
      return HashParser if hash?
      return MenuParser if menu?

      fail ParseError, 'Cannot process output from command.'
    end

    def menu?
      output.is_a?(Array)
    end

    def hash?
      output.is_a?(Hash)
    end

    def json?
      output.is_a?(String)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
vedeu-0.0.41 lib/vedeu/parsing/parser.rb