Sha256: 026d76ca7218e5b2a0b7274b6cb2a17a4958329166804a55fc56a4008bbc8f24

Contents?: true

Size: 678 Bytes

Versions: 3

Compression:

Stored size: 678 Bytes

Contents

require 'faraday'
require 'multi_json'

require 'tango/error'

module Tango
  module Response
    class ParseJson < Faraday::Response::Middleware

      def parse(body)
        case body
        when /\A^\s*$\z/, nil
          nil
        else
          json = MultiJson.load(body, :symbolize_keys => true)
          unless json.is_a?(Hash) && json[:response].is_a?(Hash) && json[:responseType] == 'SUCCESS'
            raise ::Tango::Error::ServerError.from_response(json)
          end

          json[:response]
        end
      end

      def on_complete(env)
        if respond_to?(:parse)
          env[:body] = parse(env[:body])
        end
      end

    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
tango-client-1.0.2 lib/tango/response/parse_json.rb
tango-client-1.0.1 lib/tango/response/parse_json.rb
tango-client-1.0.0 lib/tango/response/parse_json.rb