Sha256: 39c83f4f1cc4f862e22d32368f5819889591a0692397cc3d6f1e580050dd59ef

Contents?: true

Size: 1.06 KB

Versions: 16

Compression:

Stored size: 1.06 KB

Contents

# encoding: utf-8

require 'faraday'

module BitBucket
  class Request::Jsonize < Faraday::Middleware

    CONTENT_TYPE = 'Content-Type'.freeze
    MIME_TYPE    = 'application/json'.freeze

    dependency 'multi_json'

    def call(env)
      if request_with_body?(env)
        env[:request_headers][CONTENT_TYPE] ||= MIME_TYPE
        env[:body] = encode_body env unless env[:body].respond_to?(:to_str)
      end
      @app.call env
    end

    def encode_body(env)
      if MultiJson.respond_to?(:dump)
        MultiJson.dump env[:body]
      else
        MultiJson.encode env[:body]
      end
    end

    def request_with_body?(env)
      type = request_type(env)
      has_body?(env) and (type.empty? or type == MIME_TYPE)
    end

    # Don't encode bodies in string form
    def has_body?(env)
      body = env[:body] and !(body.respond_to?(:to_str) and body.empty?)
    end

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

  end # Request::Jsonize
end # BitBucket

Version data entries

16 entries across 16 versions & 4 rubygems

Version Path
zz_bitbucket_rest_api-0.1.11 lib/bitbucket_rest_api/request/jsonize.rb
zz_bitbucket_rest_api-0.1.10 lib/bitbucket_rest_api/request/jsonize.rb
zz_bitbucket_rest_api-0.1.9 lib/bitbucket_rest_api/request/jsonize.rb
zz_bitbucket_rest_api-0.1.8 lib/bitbucket_rest_api/request/jsonize.rb
bitbucket_rest_api2-0.9.5 lib/bitbucket_rest_api/request/jsonize.rb
bitbucket_rest_api2-0.9.1 lib/bitbucket_rest_api/request/jsonize.rb
bitbucket_rest_api2-0.2.2 lib/bitbucket_rest_api/request/jsonize.rb
bitbucket_rest_api2-0.2.1 lib/bitbucket_rest_api/request/jsonize.rb
bitbucket_rest_api2-0.2.0 lib/bitbucket_rest_api/request/jsonize.rb
bitbucket_rest_api-0.1.7 lib/bitbucket_rest_api/request/jsonize.rb
reenhanced_bitbucket_api-0.1.6 lib/bitbucket_rest_api/request/jsonize.rb
bitbucket_rest_api-0.1.5 lib/bitbucket_rest_api/request/jsonize.rb
bitbucket_rest_api-0.1.4 lib/bitbucket_rest_api/request/jsonize.rb
bitbucket_rest_api-0.1.2 lib/bitbucket_rest_api/request/jsonize.rb
bitbucket_rest_api-0.1.1 lib/bitbucket_rest_api/request/jsonize.rb
bitbucket_rest_api-0.1.0 lib/bitbucket_rest_api/request/jsonize.rb