Class: Grape::Middleware::Formatter

Inherits:
Base
  • Object
show all
Includes:
Formats
Defined in:
lib/grape/middleware/formatter.rb

Instance Attribute Summary

Attributes inherited from Base

#app, #env, #options

Instance Method Summary collapse

Methods inherited from Base

#call, #call!, #initialize, #request, #response

Constructor Details

This class inherits a constructor from Grape::Middleware::Base

Instance Method Details

#afterObject



70
71
72
73
74
75
76
77
78
# File 'lib/grape/middleware/formatter.rb', line 70

def after
  status, headers, bodies = *@app_response
  formatter = formatter_for env['api.format']
  bodymap = bodies.collect do |body|
    formatter.call(body)
  end
  headers['Content-Type'] = content_types[env['api.format']] unless headers['Content-Type']
  Rack::Response.new(bodymap, status, headers).to_a
end

#beforeObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/grape/middleware/formatter.rb', line 21

def before
  fmt = format_from_extension || options[:format] || format_from_header || options[:default_format]
  if content_types.key?(fmt)
    if !env['rack.input'].nil? and (body = env['rack.input'].read).strip.length != 0
      parser = parser_for fmt
      unless parser.nil?
        begin
          body = parser.call(body)
          env['rack.request.form_hash'] = !env['rack.request.form_hash'].nil? ? env['rack.request.form_hash'].merge(body) : body
          env['rack.request.form_input'] = env['rack.input']
        rescue
          # It's possible that it's just regular POST content -- just back off
        end
      end
      env['rack.input'].rewind
    end
    env['api.format'] = fmt
  else
    throw :error, :status => 406, :message => 'The requested format is not supported.'
  end
end

#default_optionsObject



8
9
10
11
12
13
14
15
# File 'lib/grape/middleware/formatter.rb', line 8

def default_options
  {
    :default_format => :txt,
    :formatters => {},
    :content_types => {},
    :parsers => {}
  }
end

#format_from_extensionObject



43
44
45
46
47
48
49
50
51
# File 'lib/grape/middleware/formatter.rb', line 43

def format_from_extension
  parts = request.path.split('.')
  extension = parts.last.to_sym

  if parts.size > 1 && content_types.key?(extension)
    return extension
  end
  nil
end

#format_from_headerObject



53
54
55
56
57
58
59
60
# File 'lib/grape/middleware/formatter.rb', line 53

def format_from_header
  mime_array.each do |t|
    if mime_types.key?(t)
      return mime_types[t]
    end
  end
  nil
end

#headersObject



17
18
19
# File 'lib/grape/middleware/formatter.rb', line 17

def headers
  env.dup.inject({}){|h,(k,v)| h[k.to_s.downcase[5..-1]] = v if k.to_s.downcase.start_with?('http_'); h}
end

#mime_arrayObject



62
63
64
65
66
67
68
# File 'lib/grape/middleware/formatter.rb', line 62

def mime_array
  accept = headers['accept'] or return []

  accept.gsub(/\b/,'').scan(%r((\w+/[\w+.-]+)(?:(?:;[^,]*?)?;\s*q=([\d.]+))?)).sort_by { |_, q| -q.to_f }.map {|mime, _|
    mime.sub(%r(vnd\.[^+]+\+), '')
  }
end