Sha256: ab10026453c0f6b165526c91f9b9b7e6a83a870f2189bf9593b075ea481982f7

Contents?: true

Size: 664 Bytes

Versions: 5

Compression:

Stored size: 664 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

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
twitter-5.5.1 lib/twitter/rest/response/parse_json.rb
twitter-5.5.0 lib/twitter/rest/response/parse_json.rb
twitter-5.4.1 lib/twitter/rest/response/parse_json.rb
twitter-5.4.0 lib/twitter/rest/response/parse_json.rb
twitter-5.3.1 lib/twitter/rest/response/parse_json.rb