Class: RoadForest::HTTP::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/roadforest/http/message.rb

Direct Known Subclasses

Request, Response

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMessage

Returns a new instance of Message.



9
10
11
12
# File 'lib/roadforest/http/message.rb', line 9

def initialize
  @body_string = ""
  @headers = {}
end

Instance Attribute Details

#headersObject

Returns the value of attribute headers.



7
8
9
# File 'lib/roadforest/http/message.rb', line 7

def headers
  @headers
end

Instance Method Details

#bodyObject



24
25
26
# File 'lib/roadforest/http/message.rb', line 24

def body
  return @body ||= StringIO.new(body_string||"")
end

#body=(value) ⇒ Object



19
20
21
22
# File 'lib/roadforest/http/message.rb', line 19

def body=(value)
  @body = value
  @body_string = nil
end

#body_stringObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/roadforest/http/message.rb', line 28

def body_string
  @body_string ||=
    begin
      case @body
      when nil
        nil
      when StringIO
        @body.string
      when IO
        @body.rewind
        @body.read
      else
        raise "Unknown class for body: #{@body.class.name}"
      end
    end
end

#body_string=(value) ⇒ Object



14
15
16
17
# File 'lib/roadforest/http/message.rb', line 14

def body_string=(value)
  @body_string = value
  @body = nil
end

#empty?Boolean

Returns:

  • (Boolean)


65
66
67
68
69
70
71
# File 'lib/roadforest/http/message.rb', line 65

def empty?
  if @body.nil?
    @body_string.nil? || @body_string.empty?
  else
    @body.respond_to?(:size) && @body.size <= 0
  end
end

#inspectObject



45
46
47
# File 'lib/roadforest/http/message.rb', line 45

def inspect
  "#<#{self.class.name}:#{'0x%0xd'%object_id}\n  #{inspection_payload.join("\n  ")}\n>"
end

#inspection_payloadObject



49
50
51
52
53
54
55
# File 'lib/roadforest/http/message.rb', line 49

def inspection_payload
  if body.respond_to? :pos
    [headers.inspect, inspection_stream]
  else
    [headers.inspect, body]
  end
end

#inspection_streamObject



57
58
59
60
61
62
63
# File 'lib/roadforest/http/message.rb', line 57

def inspection_stream
  old_pos = body.pos
  body.rewind
  body.read
ensure
  body.pos = old_pos
end