class NATS::Service

Overview

The Service represents a NATS service that was created by the Services API. It exists to hang your endpoints on:

inventory = nats.services.add "inventory",
  version: "0.1.0",
  description: "Manage inventory"
inventory.add_endpoint "inventory.products.get", subject: "inventory.products.get.*" do |request|
  _, _, _, id
  nats.reply request, ProductQuery.new.find(id).to_json
end

Defined in:

services.cr

Instance Method Summary

Instance Method Detail

def add_endpoint(name : String, *, subject : String = name, queue_group : String = "q", &block : Message -> ) : Endpoint #
service.add_endpoint "endpoint-name", subject: "endpoint.subject" do |request|
  nats.reply request, "reply goes here"
end

[View source]
def add_group(name : String, &) #

Add a Group and yield it to the block.

service.add_group "products" do |inventory|
  inventory.add_endpoint "create" do |request|
    nats.reply request, "reply goes here"
  end
end

[View source]
def add_group(name : String) #

Add a Group of endpoints.


[View source]
def description : String #

[View source]
def endpoints : Array(Endpoint) #

[View source]
def id : String #

[View source]
def metadata : Hash(String, String) #

[View source]
def name : String #

[View source]
def on_error(&error_handler : ErrorHandler) #

Execute the given block for all exceptions raised in this service's endpoints. This is useful for returning a common error response.

service.on_error do |exception, message|
  nats.reply message, "", headers: NATS::Headers{
    "Nats-Service-Error-Code" => "500",
    "Nats-Service-Error"      => "Unexpected error: #{exception.message}",
  }
end

[View source]
def subscriptions : Array(Subscription) #

[View source]
def version : String #

[View source]