Class: EventStore::HTTP::NetHTTP::Substitute

Inherits:
Object
  • Object
show all
Defined in:
lib/event_store/http/net_http/substitute.rb,
lib/event_store/http/net_http/substitute/telemetry.rb

Defined Under Namespace

Modules: Telemetry

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#activeObject

Returns the value of attribute active.



7
8
9
# File 'lib/event_store/http/net_http/substitute.rb', line 7

def active
  @active
end

#errorObject

Returns the value of attribute error.



8
9
10
# File 'lib/event_store/http/net_http/substitute.rb', line 8

def error
  @error
end

Class Method Details

.buildObject



16
17
18
19
20
21
22
23
24
25
# File 'lib/event_store/http/net_http/substitute.rb', line 16

def self.build
  telemetry_sink = Telemetry::Sink.new

  instance = new telemetry_sink

  telemetry = ::Telemetry.configure instance
  telemetry.register telemetry_sink

  instance
end

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


10
11
12
# File 'lib/event_store/http/net_http/substitute.rb', line 10

def active?
  @active.nil? ? false : @active
end

#build_response(status_code, reason_phrase: nil, body: nil, headers: nil) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/event_store/http/net_http/substitute.rb', line 69

def build_response(status_code, reason_phrase: nil, body: nil, headers: nil)
  reason_phrase ||= 'None'
  headers ||= {}
  body ||= ''

  response_cls = Net::HTTPResponse::CODE_TO_OBJ.fetch status_code.to_s do
    case status_code
    when (200...300) then Net::HTTPSuccess
    when (300...400) then Net::HTTPRedirection
    when (400...500) then Net::HTTPClientError
    when (500...600) then Net::HTTPServerError
    end
  end

  response = response_cls.new '1.1', status_code.to_s, reason_phrase

  headers.each do |name, value|
    response[name] = value
  end

  response.instance_exec do
    @read = true
    @body = body
  end

  response
end

#finishObject



105
106
107
# File 'lib/event_store/http/net_http/substitute.rb', line 105

def finish
  self.active = false
end

#next_responseObject



55
56
57
58
59
60
61
62
63
# File 'lib/event_store/http/net_http/substitute.rb', line 55

def next_response
  return build_response 404 if responses.empty?

  response = responses.shift

  responses << response if responses.empty?

  response
end

#request(request, _ = nil, &block) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/event_store/http/net_http/substitute.rb', line 27

def request(request, _=nil, &block)
  telemetry.record :requested, Telemetry::Requested.new(request)

  request_headers.each do |name, value|
    request[name] = value
  end

  raise error if error

  response = next_response

  telemetry.record :responded, Telemetry::Responded.new(response, request)

  response
end

#request_headersObject



97
98
99
# File 'lib/event_store/http/net_http/substitute.rb', line 97

def request_headers
  @request_headers ||= {}
end

#responsesObject



65
66
67
# File 'lib/event_store/http/net_http/substitute.rb', line 65

def responses
  @responses ||= []
end

#set_error(error) ⇒ Object



43
44
45
# File 'lib/event_store/http/net_http/substitute.rb', line 43

def set_error(error)
  self.error = error
end

#set_response(*arguments) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/event_store/http/net_http/substitute.rb', line 47

def set_response(*arguments)
  response = build_response *arguments

  responses << response

  response
end

#startObject



101
102
103
# File 'lib/event_store/http/net_http/substitute.rb', line 101

def start
  self.active = true
end