class Restfulness::Headers::Accept

The Accept header handler provides an array of Media Types that the client is willing to accept.

Based on a simplified RFC2616 implementation, each media type is stored and ordered.

Restfulness does not currently deal with the special 'q' paremeter defined in the standard as quality is not something APIs normally need to handle.

Aside from media type detection, a useful feature of the accept header is to provide the desired version of content to provide in the response. This class offers a helper method that will attempt to determine the version.

Given the HTTP header:

Accept: application/com.example.api+json;version=1

The resource instace has access to the version via:

request.accept.version == "1"

Attributes

media_types[RW]

The -ordered- array of media types provided in the headers

Public Class Methods

new(str = "") click to toggle source
# File lib/restfulness/headers/accept.rb, line 30
def initialize(str = "")
  self.media_types = []
  parse(str) unless str.empty?
end

Public Instance Methods

json?() click to toggle source
# File lib/restfulness/headers/accept.rb, line 49
def json?
  media_types.each do |mt|
    return true if mt.json?
  end 
  false
end
parse(str) click to toggle source
# File lib/restfulness/headers/accept.rb, line 35
def parse(str)
  types = str.split(',').map{|t| t.strip}
 
  # Attempt to crudely determine order based on length, and store
  types.sort{|a,b| b.length <=> a.length}.each do |t|
    media_types << MediaType.new(t)
  end
end
text?() click to toggle source
# File lib/restfulness/headers/accept.rb, line 63
def text?
  media_types.each do |mt|
    return true if mt.text?
  end 
  false
end
version() click to toggle source

Request the version, always assumes that the first media type is the most relevant

# File lib/restfulness/headers/accept.rb, line 45
def version
  media_types.first.version
end
xml?() click to toggle source
# File lib/restfulness/headers/accept.rb, line 56
def xml?
  media_types.each do |mt|
    return true if mt.xml?
  end 
  false
end