Class: Terminal::ResponseExt::ParseJson

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

Constant Summary collapse

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

Instance Method Summary collapse

Instance Method Details

#on_complete(response) ⇒ Object



20
21
22
# File 'lib/terminal/response/parse_json.rb', line 20

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

#parse(body) ⇒ Object



11
12
13
14
15
16
17
18
# File 'lib/terminal/response/parse_json.rb', line 11

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

#unparsable_status_codesObject



24
25
26
# File 'lib/terminal/response/parse_json.rb', line 24

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