Sha256: 06c7394a70945f5d896ad3facf2834ddb6d1de419d64cffc145c414779464533

Contents?: true

Size: 939 Bytes

Versions: 2

Compression:

Stored size: 939 Bytes

Contents

require 'cgi'

module RubyAMI
  class AGIResultParser
    attr_reader :code, :result, :data

    FORMAT          = /^(\d{3}) result=(-?\d*) ?(\(?.*\)?)?$/.freeze
    DATA_KV_FORMAT  = /([\w\d]+)=([\w\d]*)/.freeze
    DATA_CLEANER    = /(^\()|(\)$)/.freeze

    def initialize(result_string)
      @result_string = result_string.dup
      raise ArgumentError, "The result string did not match the required format." unless match
      parse
    end

    def data_hash
      return unless data_kv_match
      {data_kv_match[1] => data_kv_match[2]}
    end

    private

    def unescape
      CGI.unescape @result_string
    end

    def match
      @match ||= unescape.chomp.match(FORMAT)
    end

    def parse
      @code = match[1].to_i
      @result = match[2].to_i
      @data = match[3] ? match[3].gsub(DATA_CLEANER, '').freeze : nil
    end

    def data_kv_match
      @data_kv_match ||= data.match(DATA_KV_FORMAT)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ruby_ami-1.2.1 lib/ruby_ami/agi_result_parser.rb
ruby_ami-1.2.0 lib/ruby_ami/agi_result_parser.rb