Class: WireBridge::Client

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

Overview

─── CLIENT ─────────────────────────────────────────────────────────────────

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(service_name:, base_url:, bridge_url: "http://localhost:7331", service_id: nil, version: "1.0.0", api_key: nil, heartbeat_interval: 30, logger: Logger.new($stdout)) ⇒ Client

Returns a new instance of Client.



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/wirebridge.rb', line 77

def initialize(
  service_name:,
  base_url:,
  bridge_url: "http://localhost:7331",
  service_id: nil,
  version: "1.0.0",
  api_key: nil,
  heartbeat_interval: 30,
  logger: Logger.new($stdout)
)
  @config = {
    service_name: service_name,
    base_url: base_url,
    bridge_url: bridge_url,
    service_id: service_id || "svc-#{SecureRandom.hex(4)}",
    version: version,
    api_key: api_key || ENV["WIREBRIDGE_ANTHROPIC_KEY"] || ENV["ANTHROPIC_API_KEY"],
    heartbeat_interval: heartbeat_interval
  }
  @capabilities = []
  @registered = false
  @logger = logger
  @heartbeat_thread = nil
end

Instance Attribute Details

#capabilitiesObject (readonly)

Returns the value of attribute capabilities.



75
76
77
# File 'lib/wirebridge.rb', line 75

def capabilities
  @capabilities
end

#configObject (readonly)

Returns the value of attribute config.



75
76
77
# File 'lib/wirebridge.rb', line 75

def config
  @config
end

Instance Method Details

#capability(name:, handler:, output:, method: "GET", input: {}, tags: [], description: nil) ⇒ Object

Register a capability — chainable.

Parameters:

  • name (String)

    Human-readable capability name

  • handler (String)

    Route path, e.g. “/api/payments”

  • method (String) (defaults to: "GET")

    HTTP method (default: “GET”)

  • output (Hash)

    Schema describing the response shape

  • input (Hash) (defaults to: {})

    Schema describing accepted parameters

  • tags (Array<String>) (defaults to: [])

    Semantic tags for matching

  • description (String) (defaults to: nil)

    Optional description



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/wirebridge.rb', line 111

def capability(name:, handler:, output:, method: "GET", input: {}, tags: [], description: nil)
  id = "#{@config[:service_id]}.#{handler.gsub(/[^a-z0-9]/, '-')}"
  @capabilities << {
    id: id,
    name: name,
    handler: handler,
    method: method.upcase,
    output: output,
    input: input,
    tags: tags,
    description: description || "",
    stack: "ruby"
  }
  self
end

#register(api_key: nil) ⇒ Object

Push the manifest to WireBridge and start heartbeat.



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/wirebridge.rb', line 128

def register(api_key: nil)
  key = api_key || @config[:api_key]
  manifest = build_manifest

  payload = { manifest: manifest }
  payload[:apiKey] = key if key

  uri = URI("#{@config[:bridge_url]}/registry/backend")
  http = Net::HTTP.new(uri.host, uri.port)
  http.open_timeout = 5
  http.read_timeout = 10

  request = Net::HTTP::Post.new(uri.path, "Content-Type" => "application/json")
  request.body = payload.to_json

  response = http.request(request)

  if response.code.to_i >= 400
    @logger.error("[WireBridge] Registration failed: #{response.code} #{response.body}")
    return false
  end

  @registered = true
  @logger.info("[WireBridge] ✓ Registered #{@capabilities.size} capabilities for '#{@config[:service_name]}'")
  start_heartbeat
  true
rescue => e
  @logger.error("[WireBridge] Registration error: #{e.message}")
  false
end

#stopObject

Stop the heartbeat thread.



160
161
162
# File 'lib/wirebridge.rb', line 160

def stop
  @heartbeat_thread&.kill
end