Class: Async::HTTP::Body::Statistics

Inherits:
Protocol::HTTP::Body::Wrapper
  • Object
show all
Defined in:
lib/async/http/statistics.rb

Overview

Invokes a callback once the body has finished reading.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(start_time, body, callback) ⇒ Statistics

Returns a new instance of Statistics.



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/async/http/statistics.rb', line 33

def initialize(start_time, body, callback)
	super(body)
	
	@sent = 0
	
	@start_time = start_time
	@first_chunk_time = nil
	@end_time = nil
	
	@callback = callback
end

Instance Attribute Details

#end_timeObject (readonly)

Returns the value of attribute end_time.



47
48
49
# File 'lib/async/http/statistics.rb', line 47

def end_time
  @end_time
end

#first_chunk_timeObject (readonly)

Returns the value of attribute first_chunk_time.



46
47
48
# File 'lib/async/http/statistics.rb', line 46

def first_chunk_time
  @first_chunk_time
end

#sentObject (readonly)

Returns the value of attribute sent.



49
50
51
# File 'lib/async/http/statistics.rb', line 49

def sent
  @sent
end

#start_timeObject (readonly)

Returns the value of attribute start_time.



45
46
47
# File 'lib/async/http/statistics.rb', line 45

def start_time
  @start_time
end

Instance Method Details

#close(error = nil) ⇒ Object



63
64
65
66
67
# File 'lib/async/http/statistics.rb', line 63

def close(error = nil)
	complete_statistics(error)
	
	super
end

#first_chunk_durationObject



57
58
59
60
61
# File 'lib/async/http/statistics.rb', line 57

def first_chunk_duration
	if @first_chunk_time
		@first_chunk_time - @start_time
	end
end

#inspectObject



95
96
97
# File 'lib/async/http/statistics.rb', line 95

def inspect
	"#{super} | \#<#{self.class} #{self.to_s}>"
end

#readObject



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/async/http/statistics.rb', line 69

def read
	chunk = super
	
	@first_chunk_time ||= Clock.now
	
	if chunk
		@sent += chunk.bytesize
	end
	
	return chunk
end

#to_sObject



81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/async/http/statistics.rb', line 81

def to_s
	parts = ["sent #{@sent} bytes"]
	
	if duration = self.total_duration
		parts << "took #{format_duration(duration)} in total"
	end
	
	if duration = self.first_chunk_duration
		parts << "took #{format_duration(duration)} until first chunk"
	end
	
	return parts.join('; ')
end

#total_durationObject



51
52
53
54
55
# File 'lib/async/http/statistics.rb', line 51

def total_duration
	if @end_time
		@end_time - @start_time
	end
end