Class: Bidi2pdf::Bidi::NetworkEvent

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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 = timestamp
  @timing = timing
  @state = map_state(state)
  @http_status_code = http_status_code
  @http_method = http_method
end

Instance Attribute Details

#bytes_receivedObject (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_timestampObject (readonly)

Returns the value of attribute end_timestamp.



6
7
8
# File 'lib/bidi2pdf/bidi/network_event.rb', line 6

def end_timestamp
  @end_timestamp
end

#http_methodObject (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_codeObject (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

#idObject (readonly)

Returns the value of attribute id.



6
7
8
# File 'lib/bidi2pdf/bidi/network_event.rb', line 6

def id
  @id
end

#start_timestampObject (readonly)

Returns the value of attribute start_timestamp.



6
7
8
# File 'lib/bidi2pdf/bidi/network_event.rb', line 6

def start_timestamp
  @start_timestamp
end

#stateObject (readonly)

Returns the value of attribute state.



6
7
8
# File 'lib/bidi2pdf/bidi/network_event.rb', line 6

def state
  @state
end

#timingObject (readonly)

Returns the value of attribute timing.



6
7
8
# File 'lib/bidi2pdf/bidi/network_event.rb', line 6

def timing
  @timing
end

#urlObject (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

#dupObject



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_secondsObject



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 format_timestamp(timestamp)
  return "N/A" unless timestamp

  Time.at(timestamp / 1000.0).utc.strftime("%Y-%m-%d %H:%M:%S.%L UTC")
end

#in_progress?Boolean

Returns:

  • (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_sObject



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 = format_timestamp(@start_timestamp) || "N/A"
  end_str = format_timestamp(@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 = timestamp if timestamp
  @timing = timing if timing
  @http_status_code = http_status_code if http_status_code
  @bytes_received = bytes_received if bytes_received
end