class Async::HTTP::Body::Statistics

Invokes a callback once the body has finished reading.

Attributes

end_time[R]
first_chunk_time[R]
sent[R]
start_time[R]

Public Class Methods

new(start_time, body, callback) click to toggle source
Calls superclass method
# File lib/async/http/statistics.rb, line 50
def initialize(start_time, body, callback)
        super(body)
        
        @sent = 0
        
        @start_time = start_time
        @first_chunk_time = nil
        @end_time = nil
        
        @callback = callback
end

Public Instance Methods

close(error = nil) click to toggle source
Calls superclass method
# File lib/async/http/statistics.rb, line 80
def close(error = nil)
        complete_statistics(error)
        
        super
end
first_chunk_duration() click to toggle source
# File lib/async/http/statistics.rb, line 74
def first_chunk_duration
        if @first_chunk_time
                @first_chunk_time - @start_time
        end
end
inspect() click to toggle source
# File lib/async/http/statistics.rb, line 112
def inspect
        "#{super} | \#<#{self.class} #{self.to_s}>"
end
read() click to toggle source
Calls superclass method
# File lib/async/http/statistics.rb, line 86
def read
        chunk = super
        
        @first_chunk_time ||= Clock.now
        
        if chunk
                @sent += chunk.bytesize
        end
        
        return chunk
end
to_s() click to toggle source
# File lib/async/http/statistics.rb, line 98
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_duration() click to toggle source
# File lib/async/http/statistics.rb, line 68
def total_duration
        if @end_time
                @end_time - @start_time
        end
end

Private Instance Methods

complete_statistics(error = nil) click to toggle source
# File lib/async/http/statistics.rb, line 118
def complete_statistics(error = nil)
        @end_time = Clock.now
        
        @callback.call(self, error) if @callback
end
format_duration(seconds) click to toggle source
# File lib/async/http/statistics.rb, line 124
def format_duration(seconds)
        if seconds < 1.0
                return "#{(seconds * 1000.0).round(2)}ms"
        else
                return "#{seconds.round(1)}s"
        end
end