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

Attributes inherited from Wrapper

#body

Instance Method Summary collapse

Methods inherited from Wrapper

#empty?, #finish, #length

Methods inherited from Readable

#each, #empty?, #finish, #join, #length

Constructor Details

#initialize(start_time, body, callback) ⇒ Statistics

Returns a new instance of Statistics.



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/async/http/statistics.rb', line 48

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.



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

def end_time
  @end_time
end

#first_chunk_timeObject (readonly)

Returns the value of attribute first_chunk_time.



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

def first_chunk_time
  @first_chunk_time
end

#sentObject (readonly)

Returns the value of attribute sent.



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

def sent
  @sent
end

#start_timeObject (readonly)

Returns the value of attribute start_time.



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

def start_time
  @start_time
end

Instance Method Details

#close(error = nil) ⇒ Object



78
79
80
81
82
# File 'lib/async/http/statistics.rb', line 78

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

#first_chunk_durationObject



72
73
74
75
76
# File 'lib/async/http/statistics.rb', line 72

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

#inspectObject



110
111
112
# File 'lib/async/http/statistics.rb', line 110

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

#readObject



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

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

#to_sObject



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

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



66
67
68
69
70
# File 'lib/async/http/statistics.rb', line 66

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