Sha256: 4fde224eb44b79b1e8d847d42416d7b331b1ed125a3c5b636a10a5f6fc634e46

Contents?: true

Size: 836 Bytes

Versions: 3

Compression:

Stored size: 836 Bytes

Contents

require_relative '../models/composition'
require_relative 'parsing/hash_parser'
require_relative 'parsing/json_parser'

module Vedeu
  class ParseError < StandardError; end

  class Parser
    def self.parse(output = {})
      new(output).parse
    end

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

    def parse
      return nil if empty?

      Composition.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?

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

    def hash?
      output.is_a?(Hash)
    end

    def json?
      output.is_a?(String)
    end

    def empty?
      output.nil? || output.empty?
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
vedeu-0.0.33 lib/vedeu/support/parser.rb
vedeu-0.0.32 lib/vedeu/support/parser.rb
vedeu-0.0.31 lib/vedeu/support/parser.rb