Class: VanilliClient::Stub

Inherits:
Object
  • Object
show all
Defined in:
lib/vanilli/client.rb

Overview

Represents a single stub as will be registered with the vanilli server.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(criteria:, priority:) ⇒ Stub

Returns a new instance of Stub.



51
52
53
54
55
# File 'lib/vanilli/client.rb', line 51

def initialize(criteria:, priority:)
  @criteria = criteria
  @priority = priority
  @response = {}
end

Instance Attribute Details

#expect=(value) ⇒ Object (writeonly)

Sets the attribute expect

Parameters:

  • value

    the value to set the attribute expect to.



49
50
51
# File 'lib/vanilli/client.rb', line 49

def expect=(value)
  @expect = value
end

Instance Method Details

#capture(capture_id) ⇒ Object

The body of the matching request(s) will be logged by vanilli under the specified capture id.



82
83
84
85
# File 'lib/vanilli/client.rb', line 82

def capture(capture_id)
  @capture_id = capture_id
  self
end

#ensure_times_existsObject



97
98
99
# File 'lib/vanilli/client.rb', line 97

def ensure_times_exists
  @expect = 1 unless @expect
end

#respond_with(status, body: nil, content_type: nil, headers: nil, times: 1) ⇒ Object

Construct the response for the stub



58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/vanilli/client.rb', line 58

def respond_with(status, body: nil, content_type: nil, headers: nil, times: 1)
  fail 'Status code is missing.' if status.nil?
  fail_body_with_no_contenttype(body, content_type)

  @response = stringify_non_json_content(
    @response.merge!(
      strip_nils(status: status, contentType: content_type, body: body, headers: headers)
    )
  )

  @times = times unless times == :any

  self
end

#to_jsonObject

Converts the stub to JSON for sending to vanilli.



88
89
90
91
92
93
94
95
# File 'lib/vanilli/client.rb', line 88

def to_json
  strip_nils(criteria: strip_nils(@criteria),
             priority: @priority,
             response: strip_nils(@response),
             times: @times,
             captureId: @capture_id,
             expect: @expect).to_json
end

#wait(milliseconds:) ⇒ Object

Vanilli will wait the specified number of milliseconds before responding with the stub response.



75
76
77
78
# File 'lib/vanilli/client.rb', line 75

def wait(milliseconds:)
  @response[:wait] = milliseconds
  self
end