Class: Twitter::REST::Response::ParseJson

Inherits:
Faraday::Response::Middleware
  • Object
show all
Defined in:
lib/twitter/rest/response/parse_json.rb

Direct Known Subclasses

ParseErrorJson

Constant Summary

WHITESPACE_REGEX =
/\A^\s*$\z/

Instance Method Summary collapse

Instance Method Details

#on_complete(response) ⇒ Object



19
20
21
# File 'lib/twitter/rest/response/parse_json.rb', line 19

def on_complete(response)
  response.body = parse(response.body) if respond_to?(:parse) && !unparsable_status_codes.include?(response.status)
end

#parse(body) ⇒ Object



10
11
12
13
14
15
16
17
# File 'lib/twitter/rest/response/parse_json.rb', line 10

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

#unparsable_status_codesObject



23
24
25
# File 'lib/twitter/rest/response/parse_json.rb', line 23

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