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-0.8.14 lib/ably/rest/middleware/parse_json.rb
ably-rest-0.8.13 lib/submodules/ably-ruby/lib/ably/rest/middleware/parse_json.rb
ably-0.8.13 lib/ably/rest/middleware/parse_json.rb
ably-0.8.12 lib/ably/rest/middleware/parse_json.rb
ably-0.8.11 lib/ably/rest/middleware/parse_json.rb
ably-0.8.10 lib/ably/rest/middleware/parse_json.rb
ably-rest-0.8.9 lib/submodules/ably-ruby/lib/ably/rest/middleware/parse_json.rb
ably-0.8.9 lib/ably/rest/middleware/parse_json.rb
ably-0.8.8 lib/ably/rest/middleware/parse_json.rb
ably-0.8.7 lib/ably/rest/middleware/parse_json.rb
ably-0.8.6 lib/ably/rest/middleware/parse_json.rb
ably-rest-0.8.6 lib/submodules/ably-ruby/lib/ably/rest/middleware/parse_json.rb
ably-rest-0.8.5 lib/submodules/ably-ruby/lib/ably/rest/middleware/parse_json.rb
ably-0.8.5 lib/ably/rest/middleware/parse_json.rb
ably-0.8.4 lib/ably/rest/middleware/parse_json.rb
ably-rest-0.8.3 lib/submodules/ably-ruby/lib/ably/rest/middleware/parse_json.rb
ably-0.8.3 lib/ably/rest/middleware/parse_json.rb
ably-rest-0.8.2 lib/submodules/ably-ruby/lib/ably/rest/middleware/parse_json.rb
ably-0.8.2 lib/ably/rest/middleware/parse_json.rb
ably-0.8.1 lib/ably/rest/middleware/parse_json.rb