Class: Saddle::Middleware::ParseJson

Inherits:
FaradayMiddleware::ResponseMiddleware
  • Object
show all
Defined in:
lib/saddle/middleware/parse_json.rb

Overview

Public: Parse response bodies as JSON.

Constant Summary collapse

MIME_TYPE =
'application/json'.freeze

Instance Method Summary collapse

Instance Method Details

#has_body?(env) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/saddle/middleware/parse_json.rb', line 24

def has_body?(env)
  body = env[:body] and !(body.respond_to?(:to_str) and body.empty?)
end

#parse_response?(env) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
22
# File 'lib/saddle/middleware/parse_json.rb', line 19

def parse_response?(env)
  type = response_type(env)
  super and has_body?(env) and (type.empty? or type == MIME_TYPE)
end

#response_type(env) ⇒ Object



28
29
30
31
32
# File 'lib/saddle/middleware/parse_json.rb', line 28

def response_type(env)
  type = env[:response_headers][CONTENT_TYPE].to_s
  type = type.split(';', 2).first if type.index(';')
  type
end