Sha256: 76d09091903580a1599bb27434a2a2385d4ceec0773a22ed1f8186c1ae523e4b

Contents?: true

Size: 740 Bytes

Versions: 2

Compression:

Stored size: 740 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 twitter_parse_json: Twitter::REST::Response::ParseJson

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
twitter-6.1.0 lib/twitter/rest/response/parse_json.rb
twitter-6.0.0 lib/twitter/rest/response/parse_json.rb