File: responses_sequence.rb

package info (click to toggle)
ruby-webmock 3.25.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,172 kB
  • sloc: ruby: 12,829; makefile: 6
file content (42 lines) | stat: -rw-r--r-- 706 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# frozen_string_literal: true

module WebMock

  class ResponsesSequence

    attr_accessor :times_to_repeat

    def initialize(responses)
      @times_to_repeat = 1
      @responses = responses
      @current_position = 0
    end

    def end?
      @times_to_repeat == 0
    end

    def next_response
      if @times_to_repeat > 0
        response = @responses[@current_position]
        increase_position
        response
      else
        @responses.last
      end
    end

    private

    def increase_position
      if @current_position == (@responses.length - 1)
        @current_position = 0
        @times_to_repeat -= 1
      else
        @current_position += 1
      end
    end

  end

end