Faraday::Middleware
Request middleware that encodes the body as JSON.
Processes only requests with matching Content-type or those without a type. If a request doesn't have a type but has a body, it sets the Content-type to JSON MIME-type.
Doesn't try to encode bodies that already are in string form.
# File lib/faraday_middleware/request/encode_json.rb, line 19 def call(env) match_content_type(env) do |data| env[:body] = encode data end @app.call env end
# File lib/faraday_middleware/request/encode_json.rb, line 26 def encode(data) ::JSON.dump data end
# File lib/faraday_middleware/request/encode_json.rb, line 42 def has_body?(env) body = env[:body] and !(body.respond_to?(:to_str) and body.empty?) end
# File lib/faraday_middleware/request/encode_json.rb, line 30 def match_content_type(env) if process_request?(env) env[:request_headers][CONTENT_TYPE] ||= MIME_TYPE yield env[:body] unless env[:body].respond_to?(:to_str) end end
Generated with the Darkfish Rdoc Generator 2.