Module: Rack::App::Payload::Parser::Builder::Formats

Extended by:
Formats
Included in:
Formats
Defined in:
lib/rack/app/payload/parser/builder/formats.rb

Constant Summary collapse

JSON_CONTENT_TYPES =
[
    'application/json',
    'application/x-javascript',
    'text/javascript',
    'text/x-javascript',
    'text/x-json'
].freeze
JSON_PARSER =
proc do |io|
  begin
    ::JSON.load(io)
  rescue ::JSON::ParserError => ex
    rr = Rack::Response.new
    rr.status = 400
    rr.write(ex.message)
    throw(:rack_response, rr)
  end
end
JSON_STREAM_CONTENT_TYPES =
[
    'application/jsonstream',
    'application/stream+json',
    'application/x-json-stream'
].freeze
JSON_STREAM_PARSER =
proc do |io|
  Rack::App::RequestStream.new(io, JSON_PARSER)
end
FORM_CONTENT_TYPES =

CSV_CONTENT_TYPE = [

"text/comma-separated-values",
"application/csv",
"text/csv",

]

CSV_PARSER = proc do |io|

CSV.parse(io.read)

end

def csv(builder)

require "csv"
CSV_CONTENT_TYPE.each do |content_type|
  builder.on(content_type, &CSV_PARSER)
end

rescue LoadError end

[
    'application/x-www-form-urlencoded',
# 'multipart/form-data'
].freeze
FORM_SEP_CHAR =
'&'.freeze
RACK_QUERY_PARSER =
if Rack::Utils.respond_to?(:default_query_parser)
  lambda do |form|
    ::Rack::Utils.default_query_parser.parse_nested_query(form, FORM_SEP_CHAR)
  end
else
  lambda do |form|
    ::Rack::Utils.parse_nested_query(form, FORM_SEP_CHAR)
  end
end
NULL_END_CHAR =
/#{"\u0000"}$/
FORM_PARSER =
proc do |io|
  form_vars = io.read
  # Fix for Safari Ajax postings that always append \0
  form_vars.slice!(NULL_END_CHAR)
  RACK_QUERY_PARSER.call(form_vars)
end

Instance Method Summary collapse

Instance Method Details

#accept(builder, *form_names) ⇒ Object



99
100
101
102
103
104
105
106
107
108
# File 'lib/rack/app/payload/parser/builder/formats.rb', line 99

def accept(builder, *form_names)
  last_name = nil
  form_names.map(&:to_sym).each do |form_name|
    last_name = form_name
    unless respond_to?(form_name)
      raise(NotImplementedError, "unknown formatter: #{last_name}")
    end
    __send__ form_name, builder
  end
end

#json(builder) ⇒ Object



23
24
25
26
27
28
# File 'lib/rack/app/payload/parser/builder/formats.rb', line 23

def json(builder)
  require 'json'
  JSON_CONTENT_TYPES.each do |content_type|
    builder.on(content_type, &JSON_PARSER)
  end
end

#json_stream(builder) ⇒ Object



40
41
42
43
44
# File 'lib/rack/app/payload/parser/builder/formats.rb', line 40

def json_stream(builder)
  JSON_STREAM_CONTENT_TYPES.each do |content_type|
    builder.on(content_type, &JSON_STREAM_PARSER)
  end
end

#www_form_urlencoded(builder) ⇒ Object Also known as: form, urlencoded



90
91
92
93
94
# File 'lib/rack/app/payload/parser/builder/formats.rb', line 90

def www_form_urlencoded(builder)
  FORM_CONTENT_TYPES.each do |content_type|
    builder.on(content_type, &FORM_PARSER)
  end
end