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

Inherits:
Wrapper 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

Methods inherited from Wrapper

#close, #empty?

Methods inherited from Readable

#close, #each, #empty?, #join

Constructor Details

#initialize(start_time, body, callback) ⇒ Statistics

Returns a new instance of Statistics.



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/async/http/statistics.rb', line 44

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

Instance Attribute Details

#bytesizeObject (readonly)

Returns the value of attribute bytesize.



60
61
62
# File 'lib/async/http/statistics.rb', line 60

def bytesize
  @bytesize
end

#end_timeObject (readonly)

Returns the value of attribute end_time.



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

def end_time
  @end_time
end

#first_chunk_timeObject (readonly)

Returns the value of attribute first_chunk_time.



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

def first_chunk_time
  @first_chunk_time
end

#start_timeObject (readonly)

Returns the value of attribute start_time.



56
57
58
# File 'lib/async/http/statistics.rb', line 56

def start_time
  @start_time
end

Instance Method Details

#first_chunk_durationObject



68
69
70
71
72
# File 'lib/async/http/statistics.rb', line 68

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

#inspectObject



108
109
110
# File 'lib/async/http/statistics.rb', line 108

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

#readObject



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

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

#stop(error) ⇒ Object



74
75
76
77
78
# File 'lib/async/http/statistics.rb', line 74

def stop(error)
	complete_statistics(error)
	
	super
end

#to_sObject



94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/async/http/statistics.rb', line 94

def to_s
	parts = ["sent #{@bytesize} 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



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

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