Class: Bidi2pdf::Bidi::NetworkEvent
- Inherits:
-
Object
- Object
- Bidi2pdf::Bidi::NetworkEvent
- Defined in:
- lib/bidi2pdf/bidi/network_event.rb
Constant Summary collapse
- STATE_MAP =
{ "network.responseStarted" => "started", "network.responseCompleted" => "completed", "network.fetchError" => "error" }.freeze
Instance Attribute Summary collapse
-
#bytes_received ⇒ Object
readonly
Returns the value of attribute bytes_received.
-
#end_timestamp ⇒ Object
readonly
Returns the value of attribute end_timestamp.
-
#http_method ⇒ Object
readonly
Returns the value of attribute http_method.
-
#http_status_code ⇒ Object
readonly
Returns the value of attribute http_status_code.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#start_timestamp ⇒ Object
readonly
Returns the value of attribute start_timestamp.
-
#state ⇒ Object
readonly
Returns the value of attribute state.
-
#timing ⇒ Object
readonly
Returns the value of attribute timing.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Instance Method Summary collapse
- #dup ⇒ Object
- #duration_seconds ⇒ Object
- #format_timestamp(timestamp) ⇒ Object
- #in_progress? ⇒ Boolean
-
#initialize(id:, url:, timestamp:, timing:, state:, http_status_code: nil, http_method: nil) ⇒ NetworkEvent
constructor
A new instance of NetworkEvent.
- #map_state(state) ⇒ Object
- #to_s ⇒ Object
- #update_state(new_state, timestamp: nil, timing: nil, http_status_code: nil, bytes_received: nil) ⇒ Object
Constructor Details
#initialize(id:, url:, timestamp:, timing:, state:, http_status_code: nil, http_method: nil) ⇒ NetworkEvent
Returns a new instance of NetworkEvent.
15 16 17 18 19 20 21 22 23 |
# File 'lib/bidi2pdf/bidi/network_event.rb', line 15 def initialize(id:, url:, timestamp:, timing:, state:, http_status_code: nil, http_method: nil) @id = id @url = url @start_timestamp = @timing = timing @state = map_state(state) @http_status_code = http_status_code @http_method = http_method end |
Instance Attribute Details
#bytes_received ⇒ Object (readonly)
Returns the value of attribute bytes_received.
6 7 8 |
# File 'lib/bidi2pdf/bidi/network_event.rb', line 6 def bytes_received @bytes_received end |
#end_timestamp ⇒ Object (readonly)
Returns the value of attribute end_timestamp.
6 7 8 |
# File 'lib/bidi2pdf/bidi/network_event.rb', line 6 def @end_timestamp end |
#http_method ⇒ Object (readonly)
Returns the value of attribute http_method.
6 7 8 |
# File 'lib/bidi2pdf/bidi/network_event.rb', line 6 def http_method @http_method end |
#http_status_code ⇒ Object (readonly)
Returns the value of attribute http_status_code.
6 7 8 |
# File 'lib/bidi2pdf/bidi/network_event.rb', line 6 def http_status_code @http_status_code end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
6 7 8 |
# File 'lib/bidi2pdf/bidi/network_event.rb', line 6 def id @id end |
#start_timestamp ⇒ Object (readonly)
Returns the value of attribute start_timestamp.
6 7 8 |
# File 'lib/bidi2pdf/bidi/network_event.rb', line 6 def @start_timestamp end |
#state ⇒ Object (readonly)
Returns the value of attribute state.
6 7 8 |
# File 'lib/bidi2pdf/bidi/network_event.rb', line 6 def state @state end |
#timing ⇒ Object (readonly)
Returns the value of attribute timing.
6 7 8 |
# File 'lib/bidi2pdf/bidi/network_event.rb', line 6 def timing @timing end |
#url ⇒ Object (readonly)
Returns the value of attribute url.
6 7 8 |
# File 'lib/bidi2pdf/bidi/network_event.rb', line 6 def url @url end |
Instance Method Details
#dup ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/bidi2pdf/bidi/network_event.rb', line 71 def dup self.class.new( id: @id, url: @url, timestamp: @start_timestamp, timing: @timing&.dup, state: @state, http_status_code: @http_status_code, http_method: @http_method ).tap do |duped| duped.instance_variable_set(:@end_timestamp, @end_timestamp) duped.instance_variable_set(:@bytes_received, @bytes_received) end end |
#duration_seconds ⇒ Object
43 44 45 46 47 |
# File 'lib/bidi2pdf/bidi/network_event.rb', line 43 def duration_seconds return nil unless @start_timestamp && @end_timestamp ((@end_timestamp - @start_timestamp) / 1000.0).round(3) end |
#format_timestamp(timestamp) ⇒ Object
37 38 39 40 41 |
# File 'lib/bidi2pdf/bidi/network_event.rb', line 37 def () return "N/A" unless Time.at( / 1000.0).utc.strftime("%Y-%m-%d %H:%M:%S.%L UTC") end |
#in_progress? ⇒ Boolean
49 |
# File 'lib/bidi2pdf/bidi/network_event.rb', line 49 def in_progress? = state == "started" |
#map_state(state) ⇒ Object
33 34 35 |
# File 'lib/bidi2pdf/bidi/network_event.rb', line 33 def map_state(state) STATE_MAP.fetch(state, state) end |
#to_s ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/bidi2pdf/bidi/network_event.rb', line 51 def to_s took_str = duration_seconds ? "#{duration_seconds.round(2)} sec" : "in progress" http_status = @http_status_code ? "HTTP #{@http_status_code}" : "HTTP (N/A)" start_str = (@start_timestamp) || "N/A" end_str = (@end_timestamp) || "N/A" method_str = @http_method || "N/A" bytes_str = @bytes_received ? "#{@bytes_received} bytes" : "0 bytes" "#<NetworkEvent " \ "id=#{@id.inspect}, " \ "method=#{method_str.inspect}, " \ "url=#{@url.inspect}, " \ "state=#{@state.inspect}, " \ "#{http_status}, " \ "bytes_received=#{bytes_str}, " \ "start=#{start_str}, " \ "end=#{end_str}, " \ "duration=#{took_str}>" end |
#update_state(new_state, timestamp: nil, timing: nil, http_status_code: nil, bytes_received: nil) ⇒ Object
25 26 27 28 29 30 31 |
# File 'lib/bidi2pdf/bidi/network_event.rb', line 25 def update_state(new_state, timestamp: nil, timing: nil, http_status_code: nil, bytes_received: nil) @state = map_state(new_state) @end_timestamp = if @timing = timing if timing @http_status_code = http_status_code if http_status_code @bytes_received = bytes_received if bytes_received end |