Class: Rbkb::Http::Body

Inherits:
String
  • Object
show all
Includes:
CommonInterface
Defined in:
lib/rbkb/http/body.rb

Direct Known Subclasses

BoundBody, ChunkedBody

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from CommonInterface

#_common_init, #opts, #opts=

Constructor Details

#initialize(str = nil, opts = nil) {|_self| ... } ⇒ Body

Returns a new instance of Body.

Yields:

  • (_self)

Yield Parameters:



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/rbkb/http/body.rb', line 13

def initialize(str=nil, opts=nil)
  self.opts = opts
  if Body === str
    self.replace(str)
    @opts = str.opts.merge(@opts)
  elsif String === str 
    super(str)
  else
    super()
  end

  yield(self) if block_given?
end

Instance Attribute Details

#baseObject

Returns the value of attribute base.



40
41
42
# File 'lib/rbkb/http/body.rb', line 40

def base
  @base
end

#expect_lengthObject (readonly)

Returns the value of attribute expect_length.



11
12
13
# File 'lib/rbkb/http/body.rb', line 11

def expect_length
  @expect_length
end

Class Method Details

.parse(str) ⇒ Object



7
8
9
# File 'lib/rbkb/http/body.rb', line 7

def self.parse(str)
  new().capture(str)
end

Instance Method Details

#capture(str) {|str| ... } ⇒ Object

The capture method is used when parsing HTTP requests/responses. This can and probably should be overridden in derived classes.

Yields:

  • (str)


29
30
31
32
# File 'lib/rbkb/http/body.rb', line 29

def capture(str)
  yield(str) if block_given?
  self.data=(str)
end

#capture_complete?Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/rbkb/http/body.rb', line 87

def capture_complete?
  not @expect_length
end

#dataObject



50
51
52
# File 'lib/rbkb/http/body.rb', line 50

def data
  self
end

#data=(str) ⇒ Object

Sets internal raw string data without any HTTP decoration.



55
56
57
# File 'lib/rbkb/http/body.rb', line 55

def data=(str)
  self.replace(str.to_s)
end

#get_content_lengthObject Also known as: content_length

Returns the content length from the HTTP base object if there is one and content-length is available.



61
62
63
# File 'lib/rbkb/http/body.rb', line 61

def get_content_length
  @base.content_length if @base
end

#get_content_typeObject Also known as: content_type



66
67
68
# File 'lib/rbkb/http/body.rb', line 66

def get_content_type
  @base.content_type if @base
end

#reset_captureObject

This method will non-destructively reset the capture state on this object. It is non-destructive in that it will not affect existing captured data if present.



74
75
76
77
# File 'lib/rbkb/http/body.rb', line 74

def reset_capture
  @expect_length = nil
  @base.reset_capture() if @base and @base.capture_complete?
end

#reset_capture!Object

This method will destructively reset the capture state on this object. This method is destructive in that it will clear any previously captured data.



82
83
84
85
# File 'lib/rbkb/http/body.rb', line 82

def reset_capture!
  reset_capture()
  self.data=""
end

#to_rawObject

The to_raw method is used when writing HTTP requests/responses. This can and probably should be overridden in derived classes.



36
37
38
# File 'lib/rbkb/http/body.rb', line 36

def to_raw
  (block_given?) ? yield(self.data) : self.data
end