Class: Maze::RequestList

Inherits:
Object
  • Object
show all
Defined in:
lib/maze/request_list.rb

Overview

An abstraction for storing a list of requests (e.g. Errors, Sessions), keeping track of the “current” request (i.e. the one being inspected).

Instance Method Summary collapse

Constructor Details

#initializeRequestList

Returns a new instance of RequestList.



8
9
10
11
12
# File 'lib/maze/request_list.rb', line 8

def initialize
  @requests = []
  @current = 0
  @count = 0
end

Instance Method Details

#add(request) ⇒ Object

Add a request to the list

Parameters:

  • request

    The new request, from which a clone is made



27
28
29
30
31
32
33
34
# File 'lib/maze/request_list.rb', line 27

def add(request)
  clone = request.clone
  # UUID primarily used for commands, but no harm to set on everything
  clone[:uuid] = SecureRandom.uuid
  clone[:run_uuid] = Maze.run_uuid
  @requests.append clone
  @count += 1
end

#allObject

A frozen clone of all requests held, including those already processed



63
64
65
# File 'lib/maze/request_list.rb', line 63

def all
  @requests.clone.freeze
end

#clearObject

Clears the list completely



68
69
70
71
72
# File 'lib/maze/request_list.rb', line 68

def clear
  @requests.clear
  @current = 0
  @count = 0
end

#currentObject

The current request



37
38
39
# File 'lib/maze/request_list.rb', line 37

def current
  @requests[@current] if @requests.size > @current
end

#get(index) ⇒ Object

The request at the given index



42
43
44
# File 'lib/maze/request_list.rb', line 42

def get(index)
  @requests[index] if @requests.size > index
end

#nextObject

Moves to the next request, if there is one



55
56
57
58
59
60
# File 'lib/maze/request_list.rb', line 55

def next
  return if @current >= @requests.size

  @current += 1
  @count -= 1
end

#remainingObject

Peek at requests yet to be processed - i.e. from current onwards. All requests are left visible in the list. Returns an empty array if there are no requests outstanding.



48
49
50
51
52
# File 'lib/maze/request_list.rb', line 48

def remaining
  return [] if current.nil?

  @requests[@current..@requests.size]
end

#size_allObject

The total number of requests received, including those already processed



20
21
22
# File 'lib/maze/request_list.rb', line 20

def size_all
  @requests.size
end

#size_remainingObject

The number of unprocessed/remaining requests in the list (not the total number actually held)



15
16
17
# File 'lib/maze/request_list.rb', line 15

def size_remaining
  @count
end

#sort_by!(key_path) ⇒ Object

Sorts the remaining elements of the list by the field given, if present in all of those elements



89
90
91
92
93
94
95
# File 'lib/maze/request_list.rb', line 89

def sort_by!(key_path)
  list = remaining

  # Sort the list and overwrite in the main list
  list.sort_by! { |r| Maze::Helper.read_key_path(r[:body], key_path) }
  list.each_with_index { |r, i| @requests[@current + i] = r }
end

#sort_by_sent_at!(count) ⇒ Object

Sorts the first ‘count` elements of the list by the Bugsnag-Sent-At header, if present in all of those elements



75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/maze/request_list.rb', line 75

def sort_by_sent_at!(count)
  return unless count > 1

  header = 'Bugsnag-Sent-At'
  sub_list = @requests[@current...@current + count]

  return if sub_list.any? { |r| r[:request][header].nil? }

  # Sort sublist by Bugsnag-Sent-At and overwrite in the main list
  sub_list.sort_by! { |r| DateTime.parse(r[:request][header]) }
  sub_list.each_with_index { |r, i| @requests[@current + i] = r }
end