Class: RoadForest::ContentHandling::MediaTypeList
- Inherits:
-
Object
- Object
- RoadForest::ContentHandling::MediaTypeList
- Includes:
- Enumerable
- Defined in:
- lib/roadforest/content-handling/media-type.rb
Class Method Summary collapse
-
.build(list) ⇒ Object
Given an acceptance list, create a PriorityList from them.
Instance Method Summary collapse
- #accept_header ⇒ Object (also: #to_s)
-
#add(type) ⇒ Object
Adds an acceptable item with the given priority to the list.
-
#add_header_val(type_string) ⇒ Object
Given a raw acceptable value from an acceptance header, parse and add it to the list.
-
#best_match_from(other) ⇒ Object
Given another MediaTypeList, find the media type that is the best match between them - generally, the idea is to match an Accept header with a local list of provided types.
- #by_precedence ⇒ Object
-
#each {|q, v| ... } ⇒ Object
Iterates over the list in priority order, that is, taking into account the order in which items were added as well as their priorities.
-
#initialize ⇒ MediaTypeList
constructor
Creates a PriorityList.
- #matches?(other) ⇒ Boolean
Constructor Details
#initialize ⇒ MediaTypeList
Creates a PriorityList.
168 169 170 |
# File 'lib/roadforest/content-handling/media-type.rb', line 168 def initialize @list = [] end |
Class Method Details
.build(list) ⇒ Object
Given an acceptance list, create a PriorityList from them.
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/roadforest/content-handling/media-type.rb', line 148 def self.build(list) return list if self === list case list when Array when String list = list.split(/\s*,\s*/) else raise "Cannot build a MediaTypeList from #{list.inspect}" end new.tap do |plist| list.each {|item| plist.add_header_val(item) } end end |
Instance Method Details
#accept_header ⇒ Object Also known as: to_s
172 173 174 |
# File 'lib/roadforest/content-handling/media-type.rb', line 172 def accept_header @list.map(&:accept_header).join(", ") end |
#add(type) ⇒ Object
Adds an acceptable item with the given priority to the list.
207 208 209 210 |
# File 'lib/roadforest/content-handling/media-type.rb', line 207 def add(type) @list << type self end |
#add_header_val(type_string) ⇒ Object
Given a raw acceptable value from an acceptance header, parse and add it to the list.
216 217 218 219 220 |
# File 'lib/roadforest/content-handling/media-type.rb', line 216 def add_header_val(type_string) add(MediaType.parse(type_string)) rescue ArgumentError raise "Invalid media type" end |
#best_match_from(other) ⇒ Object
Given another MediaTypeList, find the media type that is the best match between them - generally, the idea is to match an Accept header with a local list of provided types
180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/roadforest/content-handling/media-type.rb', line 180 def best_match_from(other) other.max_by do |their_type| best_type = self.by_precedence.find do |our_type| their_type =~ our_type end if best_type.nil? 0 else best_type.quality * their_type.quality end end end |
#by_precedence ⇒ Object
198 199 200 201 202 |
# File 'lib/roadforest/content-handling/media-type.rb', line 198 def by_precedence self.sort do |left, right| right.precedence_index <=> left.precedence_index end.enum_for(:each) end |
#each {|q, v| ... } ⇒ Object
Iterates over the list in priority order, that is, taking into account the order in which items were added as well as their priorities.
228 229 230 231 232 233 |
# File 'lib/roadforest/content-handling/media-type.rb', line 228 def each return enum_for(:each) unless block_given? @list.each do |item| yield item end end |
#matches?(other) ⇒ Boolean
193 194 195 196 |
# File 'lib/roadforest/content-handling/media-type.rb', line 193 def matches?(other) type = best_match_from(other) include?(type) && other.include?(type) end |