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
35
36
37
38
# File 'lib/maze/request_list.rb', line 27

def add(request)
  clone = request.clone
  # UUID and other metadata primarily used for commands, but no harm to set on everything
  clone[:uuid] = SecureRandom.uuid
  unless Maze.scenario.nil?
    clone[:cucumber_scenario_name] = Maze.scenario.name
    clone[:cucumber_scenario_location] = Maze.scenario.location
  end
  clone[:run_uuid] = Maze.run_uuid
  @requests.append clone
  @count += 1
end

#allObject

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



73
74
75
# File 'lib/maze/request_list.rb', line 73

def all
  @requests.clone.freeze
end

#clearObject

Clears the list completely



78
79
80
81
82
# File 'lib/maze/request_list.rb', line 78

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

#currentObject

The current request



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

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

#endObject

Moves to the end of the list



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

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

#get(index) ⇒ Object

The request at the given index



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

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

#nextObject

Moves to the next request, if there is one



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

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.



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

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



110
111
112
113
114
115
116
# File 'lib/maze/request_list.rb', line 110

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_request_path!Object

Sorts the remaining elements of the list by the request path, if present in all of those elements



99
100
101
102
103
104
105
106
107
# File 'lib/maze/request_list.rb', line 99

def sort_by_request_path!
  list = remaining

  return if list.any? { |r| r[:request].path.nil? }

  # Sort the list by request path and overwrite in the main list
  list.sort_by! { |r| r[:request].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



85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/maze/request_list.rb', line 85

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