Class: RoadForest::ContentHandling::Engine

Inherits:
Object
  • Object
show all
Defined in:
lib/roadforest/content-handling/engine.rb

Defined Under Namespace

Classes: TypeHandlerList

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeEngine

Returns a new instance of Engine.



49
50
51
52
53
# File 'lib/roadforest/content-handling/engine.rb', line 49

def initialize
  @renderers = TypeHandlerList.new("provide")
  @parsers = TypeHandlerList.new("accept")
  @type_mapping = {}
end

Instance Attribute Details

#parsersObject (readonly)

Returns the value of attribute parsers.



54
55
56
# File 'lib/roadforest/content-handling/engine.rb', line 54

def parsers
  @parsers
end

#renderersObject (readonly)

Returns the value of attribute renderers.



54
55
56
# File 'lib/roadforest/content-handling/engine.rb', line 54

def renderers
  @renderers
end

Instance Method Details

#add_parser(object, type) ⇒ Object Also known as: accept



63
64
65
66
67
# 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

#add_renderer(object, type) ⇒ Object Also known as: provide



70
71
72
73
74
# 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

#add_type(handler, type) ⇒ Object Also known as: add



56
57
58
59
60
# 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

#choose_media_type(provided, header) ⇒ Object

Given the ‘Accept’ header and provided types, chooses an appropriate media type.



103
104
105
106
107
# 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) ⇒ Object



91
92
93
94
# 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) ⇒ Object



81
82
83
84
# 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) ⇒ Object



96
97
98
99
# 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) ⇒ Object



86
87
88
89
# 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) ⇒ Object



77
78
79
# File 'lib/roadforest/content-handling/engine.rb', line 77

def fetch(symbol)
  @renderers.fetch(symbol){ @parsers.fetch(symbol) }
end