Sha256: cd4cd3771020c004ba3bf2aa61a8b3e75cd0029be395d1aca403f9c4384c238d

Contents?: true

Size: 721 Bytes

Versions: 12

Compression:

Stored size: 721 Bytes

Contents

require 'faraday'
require 'json'

module Ably
  module Rest
    module Middleware
      class ParseJson < Faraday::Middleware
        def on_complete(env)
          if env.response_headers['Content-Type'] == 'application/json'
            env.body = parse(env.body) unless env.response_headers['Ably-Middleware-Parsed'] == true
            env.response_headers['Ably-Middleware-Parsed'] = true
          end
        end

        def parse(body)
          if body.length > 0
            JSON.parse(body)
          else
            body
          end
        rescue JSON::ParserError => e
          raise Ably::Exceptions::InvalidResponseBody, "Expected JSON response: #{e.message}"
        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_json.rb
ably-1.2.7 lib/ably/rest/middleware/parse_json.rb
ably-rest-1.2.6 lib/submodules/ably-ruby/lib/ably/rest/middleware/parse_json.rb
ably-1.2.6 lib/ably/rest/middleware/parse_json.rb
ably-rest-1.2.4 lib/submodules/ably-ruby/lib/ably/rest/middleware/parse_json.rb
ably-1.2.4 lib/ably/rest/middleware/parse_json.rb
ably-rest-1.2.3 lib/submodules/ably-ruby/lib/ably/rest/middleware/parse_json.rb
ably-1.2.3 lib/ably/rest/middleware/parse_json.rb
ably-rest-1.2.2 lib/submodules/ably-ruby/lib/ably/rest/middleware/parse_json.rb
ably-1.2.2 lib/ably/rest/middleware/parse_json.rb
ably-rest-1.2.1 lib/submodules/ably-ruby/lib/ably/rest/middleware/parse_json.rb
ably-1.2.1 lib/ably/rest/middleware/parse_json.rb