Sha256: f3657a7a2b70ff314ad1cde68d341b046b3b08ec4718887f1c9cb4c56ed6552c
Contents?: true
Size: 1.09 KB
Versions: 1
Compression:
Stored size: 1.09 KB
Contents
require 'faraday' module Saddle::Middleware module Response # Public: Parse response bodies as JSON. class ParseJson < Faraday::Middleware CONTENT_TYPE = 'Content-Type'.freeze MIME_TYPE = 'application/json'.freeze dependency do require 'json' unless defined?(::JSON) end def call(env) if parse_response?(env) # Make sure we're working with a valid body that's not a String if env.body and !env.body.respond_to?(:to_str) env[:request_headers][CONTENT_TYPE] ||= MIME_TYPE env.body = ::JSON.dump env.body end end @app.call env end def parse_response?(env) has_body?(env) and (response_type(env) == MIME_TYPE) end def has_body?(env) body = env[:body] and !(body.respond_to?(:to_str) and body.empty?) end def response_type(env) return nil unless env[:response_headers] type = env[:response_headers][CONTENT_TYPE].to_s type = type.split(';', 2).first if type.index(';') type end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
saddle-0.0.6 | lib/saddle/middleware/response/parse_json.rb |