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



82
83
84
85
86
87
88
89
90
# File 'lib/grape/middleware/formatter.rb', line 82

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
42
43
44
45
46
47
48
49
50
51
52
53
# 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
          fmt = mime_types[request.media_type] if request.media_type
          if content_type_for(fmt)
            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
          else
            throw :error, :status => 406, :message => 'The requested content-type is not supported.'
          end
        ensure
          env['rack.input'].rewind
        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



55
56
57
58
59
60
61
62
63
# File 'lib/grape/middleware/formatter.rb', line 55

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



65
66
67
68
69
70
71
72
# File 'lib/grape/middleware/formatter.rb', line 65

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



74
75
76
77
78
79
80
# File 'lib/grape/middleware/formatter.rb', line 74

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