Sha256: 51c9ad5eff5bf5c1c5e6e397d67125934f65cb1703c5079245b59bb23f41c82e

Contents?: true

Size: 753 Bytes

Versions: 1

Compression:

Stored size: 753 Bytes

Contents

require 'faraday'
require 'json'

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

        def parse(body)
          case body
          when WHITESPACE_REGEX, nil
            nil
          else
            JSON.parse(body, :symbolize_names => true)
          end
        end

        def on_complete(env)
          if respond_to?(:parse)
            env[:body] = parse(env[:body]) unless unparsable_status_codes.include?(env[:status])
          end
        end

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

Faraday::Response.register_middleware :parse_json => Twitter::REST::Response::ParseJson

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
twitter-5.6.0 lib/twitter/rest/response/parse_json.rb