class RoadForest::ContentHandling::Engine
Attributes
parsers[R]
renderers[R]
Public Class Methods
new()
click to toggle source
# File lib/roadforest/content-handling/engine.rb, line 49 def initialize @renderers = TypeHandlerList.new("provide") @parsers = TypeHandlerList.new("accept") @type_mapping = {} end
Public Instance Methods
add_parser(object, type)
click to toggle source
# File lib/roadforest/content-handling/engine.rb, line 63 def add_parser(object, type) type = MediaType.parse(type) wrapper = RoadForest::ContentHandling::Wrap::Parse.new(type, object) parsers.add(wrapper) end
Also aliased as: accept
add_renderer(object, type)
click to toggle source
# File lib/roadforest/content-handling/engine.rb, line 70 def add_renderer(object, type) type = MediaType.parse(type) wrapper = RoadForest::ContentHandling::Wrap::Render.new(type, object) renderers.add(wrapper) end
Also aliased as: provide
add_type(handler, type)
click to toggle source
# File lib/roadforest/content-handling/engine.rb, line 56 def add_type(handler, type) type = MediaType.parse(type) add_parser(handler, type) add_renderer(handler, type) end
Also aliased as: add
choose_media_type(provided, header)
click to toggle source
Given the ‘Accept’ header and provided types, chooses an appropriate media type.
# File lib/roadforest/content-handling/engine.rb, line 103 def choose_media_type(provided, header) return "*/*" if header.nil? requested = MediaTypeList.build(header.split(/\s*,\s*/)) requested.best_match_from(provided) end
choose_parser(header)
click to toggle source
# File lib/roadforest/content-handling/engine.rb, line 91 def choose_parser(header) content_type = choose_media_type(parsers.types, header) return parsers.handler_for(content_type) end
choose_renderer(header)
click to toggle source
# File lib/roadforest/content-handling/engine.rb, line 81 def choose_renderer(header) content_type = choose_media_type(renderers.types, header) return renderers.handler_for(content_type) end
each_parser(&block)
click to toggle source
# File lib/roadforest/content-handling/engine.rb, line 96 def each_parser(&block) parsers.handlers.enum_for(:each_pair) unless block_given? parsers.handlers.each_pair(&block) end
each_renderer(&block)
click to toggle source
# File lib/roadforest/content-handling/engine.rb, line 86 def each_renderer(&block) renderers.handlers.enum_for(:each_pair) unless block_given? renderers.handlers.each_pair(&block) end
fetch(symbol)
click to toggle source
# File lib/roadforest/content-handling/engine.rb, line 77 def fetch(symbol) @renderers.fetch(symbol){ @parsers.fetch(symbol) } end