Sha256: d5084031341c7e6fbc34d94eee004b5d59768be8512e58f8888479981ec72dc6

Contents?: true

Size: 1.62 KB

Versions: 78

Compression:

Stored size: 1.62 KB

Contents

# -*- coding: utf-8 -*-
#
# Copyright (C) 2012-2013  Kouhei Sutou <kou@clear-code.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

require "json"
require "msgpack"

require "grntest/error"

module Grntest
  class ResponseParser
    class << self
      def parse(content, type)
        parser = new(type)
        parser.parse(content)
      end
    end

    def initialize(type)
      @type = type
    end

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

    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
  end
end

Version data entries

78 entries across 78 versions & 1 rubygems

Version Path
grntest-1.8.0 lib/grntest/response-parser.rb
grntest-1.7.9 lib/grntest/response-parser.rb
grntest-1.7.8 lib/grntest/response-parser.rb
grntest-1.7.7 lib/grntest/response-parser.rb
grntest-1.7.6 lib/grntest/response-parser.rb
grntest-1.7.5 lib/grntest/response-parser.rb
grntest-1.7.4 lib/grntest/response-parser.rb
grntest-1.7.3 lib/grntest/response-parser.rb
grntest-1.7.2 lib/grntest/response-parser.rb
grntest-1.7.1 lib/grntest/response-parser.rb
grntest-1.7.0 lib/grntest/response-parser.rb
grntest-1.6.9 lib/grntest/response-parser.rb
grntest-1.6.8 lib/grntest/response-parser.rb
grntest-1.6.7 lib/grntest/response-parser.rb
grntest-1.6.6 lib/grntest/response-parser.rb
grntest-1.6.5 lib/grntest/response-parser.rb
grntest-1.6.4 lib/grntest/response-parser.rb
grntest-1.6.3 lib/grntest/response-parser.rb
grntest-1.6.2 lib/grntest/response-parser.rb
grntest-1.6.1 lib/grntest/response-parser.rb