Class: Grntest::ResponseParser

Inherits:
Object
  • Object
show all
Defined in:
lib/grntest/response-parser.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type) ⇒ ResponseParser

Returns a new instance of ResponseParser.



32
33
34
# File 'lib/grntest/response-parser.rb', line 32

def initialize(type)
  @type = type
end

Class Method Details

.parse(content, type) ⇒ Object



26
27
28
29
# File 'lib/grntest/response-parser.rb', line 26

def parse(content, type)
  parser = new(type)
  parser.parse(content)
end

Instance Method Details

#parse(content) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/grntest/response-parser.rb', line 36

def parse(content)
  case @type
  when "json", "msgpack"
    parse_result(content.chomp)
  else
    content
  end
end

#parse_result(result) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/grntest/response-parser.rb', line 45

def parse_result(result)
  case @type
  when "json"
    begin
      JSON.parse(result)
    rescue JSON::ParserError
      raise ParseError.new(@type, result, $!.message)
    end
  when "msgpack"
    begin
      MessagePack.unpack(result.chomp)
    rescue MessagePack::UnpackError, NoMemoryError
      raise ParseError.new(@type, result, $!.message)
    end
  else
    raise ParseError.new(@type, result, "unknown type")
  end
end