Sha256: a8adfff0c5cffd1e69bdaaa340dca6457e3c0e12f7b8ac83606d1e1f3fb48609

Contents?: true

Size: 731 Bytes

Versions: 73

Compression:

Stored size: 731 Bytes

Contents

require 'faraday'
require 'json'

module Ably
  module Rest
    module Middleware
      class ParseJson < Faraday::Response::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

73 entries across 73 versions & 2 rubygems

Version Path
ably-rest-1.2.0 lib/submodules/ably-ruby/lib/ably/rest/middleware/parse_json.rb
ably-1.2.0 lib/ably/rest/middleware/parse_json.rb
ably-rest-1.1.8 lib/submodules/ably-ruby/lib/ably/rest/middleware/parse_json.rb
ably-1.1.8 lib/ably/rest/middleware/parse_json.rb
ably-rest-1.1.7 lib/submodules/ably-ruby/lib/ably/rest/middleware/parse_json.rb
ably-1.1.7 lib/ably/rest/middleware/parse_json.rb
ably-rest-1.1.6 lib/submodules/ably-ruby/lib/ably/rest/middleware/parse_json.rb
ably-1.1.6 lib/ably/rest/middleware/parse_json.rb
ably-rest-1.1.5 lib/submodules/ably-ruby/lib/ably/rest/middleware/parse_json.rb
ably-1.1.5 lib/ably/rest/middleware/parse_json.rb
ably-rest-1.1.4 lib/submodules/ably-ruby/lib/ably/rest/middleware/parse_json.rb
ably-1.1.4 lib/ably/rest/middleware/parse_json.rb
ably-rest-1.1.4.rc lib/submodules/ably-ruby/lib/ably/rest/middleware/parse_json.rb
ably-1.1.4.rc lib/ably/rest/middleware/parse_json.rb
ably-rest-1.1.3 lib/submodules/ably-ruby/lib/ably/rest/middleware/parse_json.rb
ably-1.1.3 lib/ably/rest/middleware/parse_json.rb
ably-rest-1.1.2 lib/submodules/ably-ruby/lib/ably/rest/middleware/parse_json.rb
ably-1.1.2 lib/ably/rest/middleware/parse_json.rb
ably-rest-1.1.2.rc1 lib/submodules/ably-ruby/lib/ably/rest/middleware/parse_json.rb
ably-1.1.1 lib/ably/rest/middleware/parse_json.rb