Sha256: 2f8ef19afb44d5ce2d9ea7cfd8708d5d4dfb69bb5d5338c1b9afa819c17bd570

Contents?: true

Size: 746 Bytes

Versions: 6

Compression:

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

6 entries across 6 versions & 1 rubygems

Version Path
twitter-5.17.0 lib/twitter/rest/response/parse_json.rb
twitter-5.16.0 lib/twitter/rest/response/parse_json.rb
twitter-5.15.0 lib/twitter/rest/response/parse_json.rb
twitter-5.14.0 lib/twitter/rest/response/parse_json.rb
twitter-5.13.0 lib/twitter/rest/response/parse_json.rb
twitter-5.12.0 lib/twitter/rest/response/parse_json.rb