Sha256: 9197c8653f145ee3830cb0f99c2c80dcbfd171b65760b6156abfa97f6a4f7256

Contents?: true

Size: 1.53 KB

Versions: 12

Compression:

Stored size: 1.53 KB

Contents

require 'faraday'
require 'msgpack'

module Ably
  module Rest
    module Middleware
      class ParseMessagePack < Faraday::Middleware
        def on_complete(env)
          if env.response_headers['Content-Type'] == 'application/x-msgpack'
            env.body = parse(env.body) unless env.response_headers['Ably-Middleware-Parsed'] == true
            env.response_headers['Ably-Middleware-Parsed'] = true
          end
        rescue Ably::Exceptions::InvalidResponseBody => e
          debug_info = {
            method: env.method,
            url: env.url,
            base64_body: base64_body(env.body),
            response_headers: env.response_headers
          }
          raise Ably::Exceptions::InvalidResponseBody, "#{e.message}\nRequest env: #{debug_info}"
        end

        def parse(body)
          if body.length > 0
            MessagePack.unpack(body)
          else
            body
          end
        rescue MessagePack::UnknownExtTypeError => e
          raise Ably::Exceptions::InvalidResponseBody, "MessagePack::UnknownExtTypeError body could not be decoded: #{e.message}. Got Base64:\n#{base64_body(body)}"
        rescue MessagePack::MalformedFormatError => e
          raise Ably::Exceptions::InvalidResponseBody, "MessagePack::MalformedFormatError body could not be decoded: #{e.message}. Got Base64:\n#{base64_body(body)}"
        end

        def base64_body(body)
          Base64.encode64(body)
        rescue => err
          "[#{err.message}! Could not base64 encode body: '#{body}']"
        end
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 2 rubygems

Version Path
ably-rest-1.2.7 lib/submodules/ably-ruby/lib/ably/rest/middleware/parse_message_pack.rb
ably-1.2.7 lib/ably/rest/middleware/parse_message_pack.rb
ably-rest-1.2.6 lib/submodules/ably-ruby/lib/ably/rest/middleware/parse_message_pack.rb
ably-1.2.6 lib/ably/rest/middleware/parse_message_pack.rb
ably-rest-1.2.4 lib/submodules/ably-ruby/lib/ably/rest/middleware/parse_message_pack.rb
ably-1.2.4 lib/ably/rest/middleware/parse_message_pack.rb
ably-rest-1.2.3 lib/submodules/ably-ruby/lib/ably/rest/middleware/parse_message_pack.rb
ably-1.2.3 lib/ably/rest/middleware/parse_message_pack.rb
ably-rest-1.2.2 lib/submodules/ably-ruby/lib/ably/rest/middleware/parse_message_pack.rb
ably-1.2.2 lib/ably/rest/middleware/parse_message_pack.rb
ably-rest-1.2.1 lib/submodules/ably-ruby/lib/ably/rest/middleware/parse_message_pack.rb
ably-1.2.1 lib/ably/rest/middleware/parse_message_pack.rb