Class: WADL::ResponseFormat

Inherits:
HasDocs show all
Includes:
RepresentationContainer
Defined in:
lib/wadl/response_format.rb

Constant Summary

Constant Summary

Constants inherited from CheapSchema

CheapSchema::ATTRIBUTES

Instance Attribute Summary

Attributes inherited from CheapSchema

#attributes, #href, #index_key, #parent

Instance Method Summary collapse

Methods included from RepresentationContainer

#find_form, #find_representation_by_media_type

Methods inherited from HasDocs

#define_singleton

Methods inherited from CheapSchema

as_collection, as_member, contents_are_mixed_data, #dereference, #dereference_with_context, dereferencing_attr_accessor, dereferencing_instance_accessor, #each_attribute, #each_collection, #each_member, from_element, has_attributes, has_many, has_one, has_required, in_document, inherit, inherited, init, #initialize, #matches?, may_be_reference, may_be_reference?, #paths, #to_s

Constructor Details

This class inherits a constructor from WADL::CheapSchema

Instance Method Details

#build(http_response) ⇒ Object

Builds a service response object out of an HTTPResponse object.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/wadl/response_format.rb', line 41

def build(http_response)
  # Figure out which fault or representation to use.

  status = http_response.status[0]

  unless response_format = faults.find { |f| f.dereference.status == status }
    # Try to match the response to a response format using a media
    # type.
    response_media_type = http_response.content_type
    response_format = representations.find { |f|
      t = f.dereference.mediaType and response_media_type.index(t) == 0
    }

    # If an exact media type match fails, use the mime-types gem to
    # match the response to a response format using the underlying
    # subtype. This will match "application/xml" with "text/xml".
    response_format ||= begin
      mime_type = MIME::Types[response_media_type]
      raw_sub_type = mime_type[0].raw_sub_type if mime_type && !mime_type.empty?

      representations.find { |f|
        if t = f.dereference.mediaType
          response_mime_type = MIME::Types[t]
          response_raw_sub_type = response_mime_type[0].raw_sub_type if response_mime_type && !response_mime_type.empty?
          response_raw_sub_type == raw_sub_type
        end
      }
    end

    # If all else fails, try to find a response that specifies no
    # media type. TODO: check if this would be valid WADL.
    response_format ||= representations.find { |f| !f.dereference.mediaType }
  end

  body = http_response.read

  if response_format && response_format.mediaType =~ /xml/
    begin
      body = REXML::Document.new(body)

      # Find the appropriate element of the document
      if response_format.element
        # TODO: don't strip the damn namespace. I'm not very good at
        # namespaces and I don't see how to deal with them here.
        element = response_format.element.sub(/.*:/, '')
        body = REXML::XPath.first(body, "//#{element}")
      end
    rescue REXML::ParseException
    end

    body.extend(XMLRepresentation)
    body.representation_of(response_format)
  end

  klass = response_format.is_a?(FaultFormat) ? response_format.subclass : Response
  obj = klass.new(http_response.status, http_response.headers, body, response_format)

  obj.is_a?(Exception) ? raise(obj) : obj
end