Class: RoadForest::ContentHandling::Engine
- Inherits:
-
Object
- Object
- RoadForest::ContentHandling::Engine
- Defined in:
- lib/roadforest/content-handling/engine.rb
Defined Under Namespace
Classes: TypeHandlerList
Instance Attribute Summary collapse
-
#parsers ⇒ Object
readonly
Returns the value of attribute parsers.
-
#renderers ⇒ Object
readonly
Returns the value of attribute renderers.
Class Method Summary collapse
Instance Method Summary collapse
- #add_parser(object, type) ⇒ Object (also: #accept)
- #add_renderer(object, type) ⇒ Object (also: #provide)
- #add_type(handler, type) ⇒ Object (also: #add)
-
#choose_media_type(provided, header) ⇒ Object
Given the ‘Accept’ header and provided types, chooses an appropriate media type.
- #choose_parser(header) ⇒ Object
- #choose_renderer(header) ⇒ Object
- #each_parser(&block) ⇒ Object
- #each_renderer(&block) ⇒ Object
- #fetch(symbol) ⇒ Object
-
#initialize ⇒ Engine
constructor
A new instance of Engine.
Constructor Details
#initialize ⇒ Engine
Returns a new instance of Engine.
61 62 63 64 65 |
# File 'lib/roadforest/content-handling/engine.rb', line 61 def initialize @renderers = TypeHandlerList.new("provide") @parsers = TypeHandlerList.new("accept") @type_mapping = {} end |
Instance Attribute Details
#parsers ⇒ Object (readonly)
Returns the value of attribute parsers.
66 67 68 |
# File 'lib/roadforest/content-handling/engine.rb', line 66 def parsers @parsers end |
#renderers ⇒ Object (readonly)
Returns the value of attribute renderers.
66 67 68 |
# File 'lib/roadforest/content-handling/engine.rb', line 66 def renderers @renderers end |
Class Method Details
.default ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/roadforest/content-handling/engine.rb', line 49 def self.default require 'roadforest/content-handling/type-handlers/jsonld' require 'roadforest/content-handling/type-handlers/rdfa' self.new.tap do |engine| engine.add RoadForest::MediaType::Handlers::RDFa.new, "text/html;q=1;rdfa=1" engine.add RoadForest::MediaType::Handlers::RDFa.new, "application/xhtml+xml;q=1;rdfa=1" engine.add RoadForest::MediaType::Handlers::JSONLD.new, "application/ld+json" engine.add RoadForest::MediaType::Handlers::RDFa.new, "text/html;q=0.5" engine.add RoadForest::MediaType::Handlers::RDFa.new, "application/xhtml+xml;q=0.5" end end |
Instance Method Details
#add_parser(object, type) ⇒ Object Also known as: accept
75 76 77 78 79 |
# File 'lib/roadforest/content-handling/engine.rb', line 75 def add_parser(object, type) type = MediaType.parse(type) wrapper = RoadForest::MediaType::Handlers::Wrap::Parse.new(type, object) parsers.add(wrapper) end |
#add_renderer(object, type) ⇒ Object Also known as: provide
82 83 84 85 86 |
# File 'lib/roadforest/content-handling/engine.rb', line 82 def add_renderer(object, type) type = MediaType.parse(type) wrapper = RoadForest::MediaType::Handlers::Wrap::Render.new(type, object) renderers.add(wrapper) end |
#add_type(handler, type) ⇒ Object Also known as: add
68 69 70 71 72 |
# File 'lib/roadforest/content-handling/engine.rb', line 68 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.
115 116 117 118 119 |
# File 'lib/roadforest/content-handling/engine.rb', line 115 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
103 104 105 106 |
# File 'lib/roadforest/content-handling/engine.rb', line 103 def choose_parser(header) content_type = choose_media_type(parsers.types, header) return parsers.handler_for(content_type) end |
#choose_renderer(header) ⇒ Object
93 94 95 96 |
# File 'lib/roadforest/content-handling/engine.rb', line 93 def choose_renderer(header) content_type = choose_media_type(renderers.types, header) return renderers.handler_for(content_type) end |
#each_parser(&block) ⇒ Object
108 109 110 111 |
# File 'lib/roadforest/content-handling/engine.rb', line 108 def each_parser(&block) parsers.handlers.enum_for(:each_pair) unless block_given? parsers.handlers.each_pair(&block) end |
#each_renderer(&block) ⇒ Object
98 99 100 101 |
# File 'lib/roadforest/content-handling/engine.rb', line 98 def each_renderer(&block) renderers.handlers.enum_for(:each_pair) unless block_given? renderers.handlers.each_pair(&block) end |
#fetch(symbol) ⇒ Object
89 90 91 |
# File 'lib/roadforest/content-handling/engine.rb', line 89 def fetch(symbol) @renderers.fetch(symbol){ @parsers.fetch(symbol) } end |