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.



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

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



26
27
28
29
# File 'lib/maze/request_list.rb', line 26

def add(request)
  @requests.append request.clone
  @count += 1
end

#allObject

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



53
54
55
# File 'lib/maze/request_list.rb', line 53

def all
  @requests.clone.freeze
end

#clearObject

Clears the list completely



58
59
60
61
62
# File 'lib/maze/request_list.rb', line 58

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

#currentObject

The current request



32
33
34
# File 'lib/maze/request_list.rb', line 32

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

#nextObject

Moves to the next request, if there is one



45
46
47
48
49
50
# File 'lib/maze/request_list.rb', line 45

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.



38
39
40
41
42
# File 'lib/maze/request_list.rb', line 38

def remaining
  return [] if current.nil?

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

#size_allObject

The total number of requests received, including those already processed



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

def size_all
  @requests.size
end

#size_remainingObject

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



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

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



79
80
81
82
83
84
85
# File 'lib/maze/request_list.rb', line 79

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



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/maze/request_list.rb', line 65

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