Class: Restfully::HTTP::Response
- Inherits:
-
Object
- Object
- Restfully::HTTP::Response
- Includes:
- Helper
- Defined in:
- lib/restfully/http/response.rb
Instance Attribute Summary collapse
-
#code ⇒ Object
readonly
Returns the value of attribute code.
-
#head ⇒ Object
readonly
Returns the value of attribute head.
-
#io ⇒ Object
readonly
Returns the value of attribute io.
Instance Method Summary collapse
- #allow?(http_method) ⇒ Boolean
- #body ⇒ Object
-
#initialize(session, code, head, body) ⇒ Response
constructor
A new instance of Response.
- #inspect ⇒ Object
-
#links ⇒ Object
TODO: we could also search for Link headers here.
- #media_type ⇒ Object
- #property(key) ⇒ Object
Methods included from Helper
#sanitize_head, #sanitize_query
Constructor Details
#initialize(session, code, head, body) ⇒ Response
Returns a new instance of Response.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/restfully/http/response.rb', line 8 def initialize(session, code, head, body) @session = session @io = StringIO.new case body when String @io << body else body.each{|chunk| @io << chunk} end @io.rewind @code = code @head = sanitize_head(head) end |
Instance Attribute Details
#code ⇒ Object (readonly)
Returns the value of attribute code.
6 7 8 |
# File 'lib/restfully/http/response.rb', line 6 def code @code end |
#head ⇒ Object (readonly)
Returns the value of attribute head.
6 7 8 |
# File 'lib/restfully/http/response.rb', line 6 def head @head end |
#io ⇒ Object (readonly)
Returns the value of attribute io.
6 7 8 |
# File 'lib/restfully/http/response.rb', line 6 def io @io end |
Instance Method Details
#allow?(http_method) ⇒ Boolean
52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/restfully/http/response.rb', line 52 def allow?(http_method) http_method = http_method.downcase.to_sym return true if http_method == :get ( media_type.respond_to?(:allow?) && media_type.allow?(http_method) ) || ( head['Allow'] && head['Allow'].split(/\s*,\s*/).map{|m| m.downcase.to_sym }.include?(http_method) ) end |
#body ⇒ Object
23 24 25 26 27 28 |
# File 'lib/restfully/http/response.rb', line 23 def body @io.rewind @body = @io.read @io.rewind @body end |
#inspect ⇒ Object
66 67 68 |
# File 'lib/restfully/http/response.rb', line 66 def inspect "#{code}, head=#{head.inspect}, body=#{body.inspect}" end |
#links ⇒ Object
TODO: we could also search for Link headers here.
44 45 46 |
# File 'lib/restfully/http/response.rb', line 44 def links media_type.links end |
#media_type ⇒ Object
34 35 36 37 38 39 40 41 |
# File 'lib/restfully/http/response.rb', line 34 def media_type @media_type ||= begin m = MediaType.find(head['Content-Type']) raise Error, "Cannot find a media-type for content-type=#{head['Content-Type'].inspect}" if m.nil? @session.logger.debug "Using media-type #{m.inspect}" m.new(io, @session) end end |
#property(key) ⇒ Object
48 49 50 |
# File 'lib/restfully/http/response.rb', line 48 def property(key) media_type.property(key) end |