Sha256: 2e5ac4517fe060b66b5dc39fba7802221b9c8018a818b583743d7951faa4eb54

Contents?: true

Size: 738 Bytes

Versions: 6

Compression:

Stored size: 738 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(response)
          response.body = parse(response.body) if respond_to?(:parse) && !unparsable_status_codes.include?(response.status)
        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

6 entries across 6 versions & 1 rubygems

Version Path
twitter-5.11.0 lib/twitter/rest/response/parse_json.rb
twitter-5.10.0 lib/twitter/rest/response/parse_json.rb
twitter-5.9.0 lib/twitter/rest/response/parse_json.rb
twitter-5.8.0 lib/twitter/rest/response/parse_json.rb
twitter-5.7.1 lib/twitter/rest/response/parse_json.rb
twitter-5.7.0 lib/twitter/rest/response/parse_json.rb