Class: NATS::Service::Request

Inherits:
Msg
  • Object
show all
Defined in:
lib/nats/service/endpoint.rb

Instance Attribute Summary collapse

Attributes inherited from Msg

#data, #header, #nc, #reply, #sub, #subject

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Msg

#respond, #respond_msg

Methods included from JetStream::Msg::AckMethods

#ack, #ack_sync, #in_progress, #metadata, #nak, #term

Constructor Details

#initialize(opts = {}) ⇒ Request

Returns a new instance of Request.



21
22
23
24
25
# File 'lib/nats/service/endpoint.rb', line 21

def initialize(opts = {})
  super
  @endpoint = opts[:endpoint]
  @error = nil
end

Instance Attribute Details

#endpointObject (readonly)

Returns the value of attribute endpoint.



19
20
21
# File 'lib/nats/service/endpoint.rb', line 19

def endpoint
  @endpoint
end

#errorObject (readonly)

Returns the value of attribute error.



19
20
21
# File 'lib/nats/service/endpoint.rb', line 19

def error
  @error
end

Class Method Details

.from_msg(svc, msg) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/nats/service/endpoint.rb', line 50

def from_msg(svc, msg)
  request = Request.new(endpoint: svc)
  request.subject = msg.subject
  request.reply = msg.reply
  request.data = msg.data
  request.header = msg.header
  request.nc = msg.nc
  request.sub = msg.sub

  request
end

Instance Method Details

#inspectObject



43
44
45
46
47
# File 'lib/nats/service/endpoint.rb', line 43

def inspect
  dot = "..." if @data.length > 10
  dat = "#{data.slice(0, 10)}#{dot}"
  "#<Service::Request(subject: \"#{@subject}\", reply: \"#{@reply}\", data: #{dat.inspect})>"
end

#respond_with_error(error) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/nats/service/endpoint.rb', line 27

def respond_with_error(error)
  @error = NATS::Service::ErrorWrapper.new(error)

  message = dup
  message.subject = reply
  message.reply = ""
  message.data = @error.data

  message.header = {
    "Nats-Service-Error" => @error.message,
    "Nats-Service-Error-Code" => @error.code
  }

  respond_msg(message)
end