Class: HTTP::Response::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/http/response/parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeParser

Returns a new instance of Parser.



8
9
10
11
# File 'lib/http/response/parser.rb', line 8

def initialize
  @parser = HTTP::Parser.new(self)
  reset
end

Instance Attribute Details

#headersObject (readonly)

Returns the value of attribute headers.



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

def headers
  @headers
end

Instance Method Details

#add(data) ⇒ Object Also known as: <<



13
14
15
# File 'lib/http/response/parser.rb', line 13

def add(data)
  @parser << data
end

#finished?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/http/response/parser.rb', line 72

def finished?
  @finished
end

#headers?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/http/response/parser.rb', line 18

def headers?
  !!@headers
end

#http_versionObject



22
23
24
# File 'lib/http/response/parser.rb', line 22

def http_version
  @parser.http_version.join(".")
end

#on_body(chunk) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/http/response/parser.rb', line 38

def on_body(chunk)
  if @chunk
    @chunk << chunk
  else
    @chunk = chunk
  end
end

#on_headers_complete(headers) ⇒ Object

HTTP::Parser callbacks



34
35
36
# File 'lib/http/response/parser.rb', line 34

def on_headers_complete(headers)
  @headers = headers
end

#on_message_completeObject



60
61
62
# File 'lib/http/response/parser.rb', line 60

def on_message_complete
  @finished = true
end

#read(size) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/http/response/parser.rb', line 46

def read(size)
  return if @chunk.nil?

  if @chunk.bytesize <= size
    chunk  = @chunk
    @chunk = nil
  else
    chunk = @chunk.byteslice(0, size)
    @chunk[0, size] = ""
  end

  chunk
end

#resetObject



64
65
66
67
68
69
70
# File 'lib/http/response/parser.rb', line 64

def reset
  @parser.reset!

  @finished = false
  @headers  = nil
  @chunk    = nil
end

#status_codeObject



26
27
28
# File 'lib/http/response/parser.rb', line 26

def status_code
  @parser.status_code
end