Class: Takagi::Client

Inherits:
ClientBase show all
Extended by:
Forwardable
Defined in:
lib/takagi/client.rb,
lib/takagi/client/response.rb

Overview

Unified Takagi Client for communicating with Takagi servers over CoAP.

Supports multiple protocols (UDP, TCP) with automatic protocol detection based on URI scheme or explicit protocol parameter.

Examples:

Block-based with protocol auto-detection (recommended)

Takagi::Client.new('coap://localhost:5683') do |client|
  client.get('/temperature')
end

Block-based with explicit protocol

Takagi::Client.new('localhost:5683', protocol: :tcp) do |client|
  client.get('/temperature')
end

Manual lifecycle management

client = Takagi::Client.new('coap://localhost:5683')
begin
  client.get('/temperature')
ensure
  client.close
end

Defined Under Namespace

Classes: Response

Instance Attribute Summary

Attributes inherited from ClientBase

#callbacks, #server_uri, #timeout

Instance Method Summary collapse

Methods inherited from ClientBase

#close, #closed?, #delete, #get, #get_json, #observe, #on, open, #post, #post_json, #put, #put_json

Constructor Details

#initialize(server_uri, timeout: 5, protocol: nil, use_retransmission: true) {|client| ... } ⇒ Client, Object

Creates a new client and optionally yields it to a block.

Examples:

Protocol auto-detection from URI

client = Takagi::Client.new('coap://localhost:5683')      # Uses UDP
client = Takagi::Client.new('coap+tcp://localhost:5683')  # Uses TCP

Explicit protocol specification

client = Takagi::Client.new('localhost:5683', protocol: :tcp)
client = Takagi::Client.new('localhost:5683', protocol: :udp)

With block (auto-close)

Takagi::Client.new('coap://localhost:5683') do |client|
  client.get('/resource')
end

Parameters:

  • server_uri (String)

    URL of the Takagi server (e.g., ‘coap://localhost:5683’, ‘localhost:5683’)

  • timeout (Integer) (defaults to: 5)

    Maximum time to wait for a response

  • protocol (Symbol, nil) (defaults to: nil)

    Protocol to use (:udp, :tcp, or nil for auto-detection from URI)

  • use_retransmission (Boolean) (defaults to: true)

    Enable RFC 7252 §4.2 compliant retransmission for UDP (default: true)

Yields:

  • (client)

    Optionally yields the client to a block and auto-closes afterward



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/takagi/client.rb', line 60

def initialize(server_uri, timeout: 5, protocol: nil, use_retransmission: true)
  # Detect protocol from URI if not explicitly specified
  @protocol = protocol || detect_protocol(server_uri)

  # Delegate to the appropriate client implementation
  @impl = create_client_impl(server_uri, timeout, use_retransmission)

  # If a block is given, yield and auto-close
  return unless block_given?

  begin
    yield(self)
  ensure
    close
  end
end