Sha256: 8603ca92e1298f0ac052ae9f8eab780ea464dedd68f5f8ff3261f9fb922310b0

Contents?: true

Size: 1.05 KB

Versions: 39

Compression:

Stored size: 1.05 KB

Contents

# encoding: utf-8

require 'faraday'

module Github
  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 # Github

Version data entries

39 entries across 39 versions & 1 rubygems

Version Path
github_api-0.11.3 lib/github_api/request/jsonize.rb
github_api-0.11.2 lib/github_api/request/jsonize.rb
github_api-0.11.1 lib/github_api/request/jsonize.rb
github_api-0.11.0 lib/github_api/request/jsonize.rb
github_api-0.10.2 lib/github_api/request/jsonize.rb
github_api-0.10.1 lib/github_api/request/jsonize.rb
github_api-0.10.0 lib/github_api/request/jsonize.rb
github_api-0.9.7 lib/github_api/request/jsonize.rb
github_api-0.9.6 lib/github_api/request/jsonize.rb
github_api-0.9.5 lib/github_api/request/jsonize.rb
github_api-0.9.4 lib/github_api/request/jsonize.rb
github_api-0.9.3 lib/github_api/request/jsonize.rb
github_api-0.9.2 lib/github_api/request/jsonize.rb
github_api-0.9.1 lib/github_api/request/jsonize.rb
github_api-0.9.0 lib/github_api/request/jsonize.rb
github_api-0.8.11 lib/github_api/request/jsonize.rb
github_api-0.8.10 lib/github_api/request/jsonize.rb
github_api-0.8.9 lib/github_api/request/jsonize.rb
github_api-0.8.8 lib/github_api/request/jsonize.rb
github_api-0.8.7 lib/github_api/request/jsonize.rb