Class: Chassis::ParseJson

Inherits:
Faraday::Response::Middleware
  • Object
show all
Defined in:
lib/chassis/faraday.rb

Instance Method Summary collapse

Instance Method Details

#on_complete(env) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/chassis/faraday.rb', line 117

def on_complete(env)
  return if [204, 304].include? env.fetch(:status)

  content_type = env.fetch(:response_headers).fetch('Content-Type', nil)

  return unless content_type
  return unless content_type =~ /json/

  body = env.body

  return unless body.respond_to? :to_str

  content = body.to_str.strip

  return if content.empty?

  env[:body] = JSON.load content
end