views:

28

answers:

1
class UbacParser
    def initialize(str)
        @str= str
        @valid= true
        base_parse
    end

    private
    def base_parse
        @protocol_code = Integer(@str[0..2]) rescue nil
        begin
        @data = @str[@str.index('<')[email protected]('>')-1] 
        str_mod = @str[@str.index('>##')+1..-1] 

        arr_mod=str_mod[2..-3].split(',')
        @hash_mod=Hash.new
        arr_mod.each_index { |i| @hash_mod[arr_mod[i].split('=')[0]]=arr_mod[i].split('=')[1] }
        rescue
            puts "error in data_parse"
            @valid=false
        end
    end

    public
    def valid?

        return @valid;
    end

    def [](key)
        unless @valid: return 
        end
        @hash_mod[key.upcase]
    end

end

How do i write a rspec validator / test case for this Ruby Class code?

Please help

A: 

I suggest you have a look at the rspec website and restart from scratch by writing the specs first. It would be a good learning exercise and help you write testable code.

David