Sha256: 5e805e217ea48d40af669c048753940a34abbbd1e4845924d40ac4b1589e1c01

Contents?: true

Size: 821 Bytes

Versions: 6

Compression:

Stored size: 821 Bytes

Contents

require 'faraday'
require 'json'

module Uploadcare
  module Connections
    module Response
      class ParseJson < Faraday::Response::Middleware
        WHITESPACE_REGEX = /\A^\s*$\z/

        ERROR_CODES = [400, 401, 403, 404, 406, 408, 422, 429, 500, 502, 503, 504]

        def parse(body)
          case body
          when WHITESPACE_REGEX, nil
            nil
          else
            JSON.parse(body)
          end
        end

        def on_complete(response)
          response[:body] = parse(response[:body]) if respond_to?(:parse) && !ERROR_CODES.include?(response[:status])
        end

        def unparsable_status_codes
          [204, 301, 302, 304]
        end
      end
    end
  end
end

Faraday::Response.register_middleware :uploadcare_parse_json => Uploadcare::Connections::Response::ParseJson

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
uploadcare-ruby-2.1.2 lib/uploadcare/rest/middlewares/parse_json_middleware.rb
uploadcare-ruby-1.2.2 lib/uploadcare/rest/middlewares/parse_json_middleware.rb
uploadcare-ruby-1.2.1 lib/uploadcare/rest/middlewares/parse_json_middleware.rb
uploadcare-ruby-2.1.1 lib/uploadcare/rest/middlewares/parse_json_middleware.rb
uploadcare-ruby-2.0.0 lib/uploadcare/rest/middlewares/parse_json_middleware.rb
uploadcare-ruby-1.1.0 lib/uploadcare/rest/middlewares/parse_json_middleware.rb