Class: Restfully::HTTP::Response

Inherits:
Object
  • Object
show all
Includes:
Helper
Defined in:
lib/restfully/http/response.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#codeObject (readonly)

Returns the value of attribute code.



6
7
8
# File 'lib/restfully/http/response.rb', line 6

def code
  @code
end

#headObject (readonly)

Returns the value of attribute head.



6
7
8
# File 'lib/restfully/http/response.rb', line 6

def head
  @head
end

#ioObject (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

Returns:

  • (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

#bodyObject



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

#inspectObject



66
67
68
# File 'lib/restfully/http/response.rb', line 66

def inspect
  "#{code}, head=#{head.inspect}, body=#{body.inspect}"
end

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_typeObject



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