Class: Podio::Middleware::JsonResponse

Inherits:
Faraday::Response::Middleware
  • Object
show all
Defined in:
lib/podio/middleware/json_response.rb

Instance Method Summary collapse

Instance Method Details

#is_json?(env) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/podio/middleware/json_response.rb', line 20

def is_json?(env)
  env[:response_headers]['content-type'] =~ /application\/json/
end

#on_complete(env) ⇒ Object



9
10
11
12
13
# File 'lib/podio/middleware/json_response.rb', line 9

def on_complete(env)
  if env[:body].is_a?(String) && is_json?(env) && should_unmarshal?(env) && env[:status] != 500
    env[:body] = parse(env[:body])
  end
end

#parse(body) ⇒ Object



24
25
26
27
28
29
# File 'lib/podio/middleware/json_response.rb', line 24

def parse(body)
  return nil if body !~ /\S/ # json gem doesn't like decoding blank strings
  MultiJson.decode(body)
rescue Object => err
  raise Faraday::Error::ParsingError.new(err)
end

#should_unmarshal?(env) ⇒ Boolean

Returns:

  • (Boolean)


15
16
17
18
# File 'lib/podio/middleware/json_response.rb', line 15

def should_unmarshal?(env)
  # don't unmarshal data from the API with a content-disposition header.
  not env[:response_headers]['content-disposition'] =~ /filename=/
end