Class: Rbkb::Http::Body

Inherits:
String 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=

Methods inherited from String

#^, #b64, #bgrep, #blit, #camelize, #camelize_meth, #class_name, #const_lookup, #crc32, #cstring, #d64, #dat_to_num, #decamelize, #dehexdump, #entropy, #hex_to_num, #hexdump, #hexify, #ishex?, #lalign, #pipe_magick, #ralign, #randomize, #randomize!, #rotate_bytes, #starts_with?, #strings, #to_stringio, #unhexify, #urldec, #urlenc, #xor

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)


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

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

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

#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.



68
69
70
71
# File 'lib/rbkb/http/body.rb', line 68

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.



76
77
78
79
# File 'lib/rbkb/http/body.rb', line 76

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