Class: Rack::App::Payload::Parser::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/app/payload/parser/builder.rb

Instance Method Summary collapse

Constructor Details

#initializeBuilder

Returns a new instance of Builder.



5
6
7
# File 'lib/rack/app/payload/parser/builder.rb', line 5

def initialize
  @content_type__parsers = Hash.new(Rack::App::Payload::Parser::DEFAULT_PARSER)
end

Instance Method Details

#accept(*formats) ⇒ Object



18
19
20
# File 'lib/rack/app/payload/parser/builder.rb', line 18

def accept(*formats)
  Rack::App::Payload::Parser::Builder::Formats.accept(self, *formats)
end

#merge!(parser_builder) ⇒ Object



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

def merge!(parser_builder)
  raise unless parser_builder.is_a?(self.class)
  @content_type__parsers.merge!(parser_builder.instance_variable_get(:@content_type__parsers))
  self
end

#on(content_type, &parser) ⇒ Object



13
14
15
16
# File 'lib/rack/app/payload/parser/builder.rb', line 13

def on(content_type, &parser)
  @content_type__parsers[content_type]= parser
  self
end

#on_unknown_media_types(&parser) ⇒ Object



22
23
24
# File 'lib/rack/app/payload/parser/builder.rb', line 22

def on_unknown_media_types(&parser)
  @content_type__parsers = Hash.new(parser).merge(@content_type__parsers)
end

#reject_unsupported_media_typesObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/rack/app/payload/parser/builder.rb', line 26

def reject_unsupported_media_types
  reject = proc do |io|
    rr = Rack::Response.new
    rr.status = 415
    rr.write("Unsupported Media Type")
    rr.write("Accepted content-types:")
    @content_type__parsers.each do |content_type, _|
      rr.write(content_type.to_s)
    end
    throw(:rack_response, rr)
  end
  @content_type__parsers = Hash.new(reject).merge(@content_type__parsers)
  nil
end

#to_parserObject



9
10
11
# File 'lib/rack/app/payload/parser/builder.rb', line 9

def to_parser
  Rack::App::Payload::Parser.new(@content_type__parsers.dup)
end