Sha256: f1bc8167fb20629a435f2fc7d3831f36d31c613203fbcffb964725453afd941a

Contents?: true

Size: 914 Bytes

Versions: 14

Compression:

Stored size: 914 Bytes

Contents

require "faraday"
require "multi_json"

class ApiClient::Connection::Middlewares::Request::Json < Faraday::Middleware
  CONTENT_TYPE = "Content-Type".freeze

  class << self
    attr_accessor :mime_type
  end
  self.mime_type = "application/json".freeze

  def call(env)
    match_content_type(env) do |data|
      params = Faraday::Utils::ParamsHash[data]
      env[:body] = MultiJson.dump(params)
    end
    @app.call env
  end

  def match_content_type(env)
    if process_request?(env)
      env[:request_headers][CONTENT_TYPE] ||= self.class.mime_type
      yield env[:body] unless env[:body].respond_to?(:to_str)
    end
  end

  def process_request?(env)
    type = request_type(env)
    env[:body] and (type.empty? or type == self.class.mime_type)
  end

  def request_type(env)
    type = env[:request_headers][CONTENT_TYPE].to_s
    type = type.split(";", 2).first if type.index(";")
    type
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
api_client-0.5.11 lib/api_client/connection/middlewares/request/json.rb
api_client-0.5.9 lib/api_client/connection/middlewares/request/json.rb
api_client-0.5.7 lib/api_client/connection/middlewares/request/json.rb
api_client-0.5.6 lib/api_client/connection/middlewares/request/json.rb
api_client-0.5.5 lib/api_client/connection/middlewares/request/json.rb
api_client-0.5.4 lib/api_client/connection/middlewares/request/json.rb
api_client-0.5.3 lib/api_client/connection/middlewares/request/json.rb
api_client-0.5.2 lib/api_client/connection/middlewares/request/json.rb
api_client-0.5.1 lib/api_client/connection/middlewares/request/json.rb
api_client-0.5.0 lib/api_client/connection/middlewares/request/json.rb
api_client-0.4.3 lib/api_client/connection/middlewares/request/json.rb
api_client-0.4.2 lib/api_client/connection/middlewares/request/json.rb
api_client-0.4.1 lib/api_client/connection/middlewares/request/json.rb
api_client-0.4.0 lib/api_client/connection/middlewares/request/json.rb