class Github::Request::Jsonize

Constants

CONTENT_TYPE
MIME_TYPE

Public Instance Methods

call(env) click to toggle source
# File lib/github_api/request/jsonize.rb, line 12
def call(env)
  if request_with_body?(env)
    env[:request_headers][CONTENT_TYPE] ||= MIME_TYPE
    env[:body] = encode_body env[:body] unless env[:body].respond_to?(:to_str)
  elsif safe_to_modify?(env)
    # Ensure valid body for put and post requests
    if [:put, :patch, :post].include?(env[:method])
      env[:body] = encode_body({})
    end
  end
  @app.call env
end
encode_body(value) click to toggle source
# File lib/github_api/request/jsonize.rb, line 25
def encode_body(value)
  if MultiJson.respond_to?(:dump)
    MultiJson.dump(value)
  else
    MultiJson.encode(value)
  end
end
has_body?(env) click to toggle source

Don't encode bodies in string form

# File lib/github_api/request/jsonize.rb, line 44
def has_body?(env)
  body = env[:body] and !(body.respond_to?(:to_str) and body.empty?)
end
request_type(env) click to toggle source
# File lib/github_api/request/jsonize.rb, line 48
def request_type(env)
  type = env[:request_headers][CONTENT_TYPE].to_s
  type = type.split(';', 2).first if type.index(';')
  type
end
request_with_body?(env) click to toggle source
# File lib/github_api/request/jsonize.rb, line 33
def request_with_body?(env)
  type = request_type(env)
  has_body?(env) and safe_to_modify?(env)
end
safe_to_modify?(env) click to toggle source
# File lib/github_api/request/jsonize.rb, line 38
def safe_to_modify?(env)
  type = request_type(env)
  type.empty? or type == MIME_TYPE
end