Class: Takagi::ClientBase

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

Overview

Base class for Takagi clients, providing common functionality for both UDP (CoAP) and TCP (CoAP over TCP) clients.

This class defines the common interface and lifecycle management that all Takagi clients should follow.

Direct Known Subclasses

Client, TcpClient, UdpClient

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(server_uri, timeout: 5) ⇒ ClientBase

Initializes the base client

Parameters:

  • server_uri (String)

    URL of the Takagi server

  • timeout (Integer) (defaults to: 5)

    Maximum time to wait for a response



18
19
20
21
22
23
# File 'lib/takagi/client_base.rb', line 18

def initialize(server_uri, timeout: 5)
  @server_uri = URI(server_uri)
  @timeout = timeout
  @callbacks = {}
  @closed = false
end

Instance Attribute Details

#callbacksObject (readonly)

Returns the value of attribute callbacks.



13
14
15
# File 'lib/takagi/client_base.rb', line 13

def callbacks
  @callbacks
end

#server_uriObject (readonly)

Returns the value of attribute server_uri.



13
14
15
# File 'lib/takagi/client_base.rb', line 13

def server_uri
  @server_uri
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



13
14
15
# File 'lib/takagi/client_base.rb', line 13

def timeout
  @timeout
end

Class Method Details

.open(server_uri, timeout: 5, **options) {|client| ... } ⇒ Object

Creates a new client and yields it to the block, ensuring it’s closed afterward. This is the recommended way to use clients to prevent resource leaks.

Examples:

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

Parameters:

  • server_uri (String)

    URL of the Takagi server

  • timeout (Integer) (defaults to: 5)

    Maximum time to wait for a response

  • options (Hash)

    Additional options passed to the subclass constructor

Yields:

  • (client)

    Gives the client to the block

Returns:

  • (Object)

    The return value of the block



146
147
148
149
150
151
# File 'lib/takagi/client_base.rb', line 146

def self.open(server_uri, timeout: 5, **options, &block)
  client = new(server_uri, timeout: timeout, **options)
  block.call(client)
ensure
  client&.close
end

Instance Method Details

#closeObject

Closes the client and releases any resources. This should be called when the client is no longer needed to prevent resource leaks in long-running processes.

Subclasses should override this method to perform specific cleanup and then call super.



120
121
122
123
124
125
# File 'lib/takagi/client_base.rb', line 120

def close
  return if @closed

  cleanup_resources
  @closed = true
end

#closed?Boolean

Check if the client has been closed

Returns:

  • (Boolean)

    true if the client is closed



129
130
131
# File 'lib/takagi/client_base.rb', line 129

def closed?
  @closed
end

#delete(path, options: {}, type: nil, &block) ⇒ Object

Sends a DELETE request

Parameters:

  • path (String)

    Resource path

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

    CoAP options

  • type (Integer, nil) (defaults to: nil)

    CoAP message type

  • callback (Proc)

    (optional) Callback function for processing the response



66
67
68
# File 'lib/takagi/client_base.rb', line 66

def delete(path, options: {}, type: nil, &block)
  request(:delete, path, nil, options: options, type: type, &block)
end

#get(path, options: {}, type: nil, &block) ⇒ Object

Sends a GET request

Parameters:

  • path (String)

    Resource path

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

    CoAP options (e.g. { 17 => 50 } for Accept=application/json)

  • type (Integer, nil) (defaults to: nil)

    CoAP message type (CON/NON/ACK/RST). Defaults to CON.

  • callback (Proc)

    (optional) Callback function for processing the response



37
38
39
# File 'lib/takagi/client_base.rb', line 37

def get(path, options: {}, type: nil, &block)
  request(:get, path, nil, options: options, type: type, &block)
end

#get_json(path, options: {}, &block) ⇒ Object

Sends a GET request and automatically parses JSON response (convenience method)



99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/takagi/client_base.rb', line 99

def get_json(path, options: {}, &block)
  cf = Takagi::CoAP::Registries::ContentFormat::JSON
  merged = { Takagi::CoAP::Registries::Option::ACCEPT => cf }.merge(options)
  if block_given?
    get(path, options: merged) do |response|
      data = response.is_a?(String) ? parse_json_response(response) : response.json
      block.call(data)
    end
  else
    result = nil
    get(path, options: merged) { |response| result = response.is_a?(String) ? parse_json_response(response) : response.json }
    result
  end
end

#observe(path, options: {}, &block) ⇒ Object

Sends an OBSERVE request (RFC 7641). The Observe option is added automatically; additional CoAP options can be supplied via ‘options:`.

Parameters:

  • path (String)

    Resource path

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

    CoAP options (Observe is injected automatically)

  • callback (Proc)

    (optional) Callback function for processing notifications



75
76
77
78
# File 'lib/takagi/client_base.rb', line 75

def observe(path, options: {}, &block)
  merged = { Takagi::CoAP::Registries::Option::OBSERVE => 0 }.merge(options)
  request(:get, path, nil, options: merged, &block)
end

#on(event, &callback) ⇒ Object

Registers a callback for a given event

Parameters:

  • event (Symbol)

    Event name (e.g., :response)

  • callback (Proc)

    Callback function to handle the event



28
29
30
# File 'lib/takagi/client_base.rb', line 28

def on(event, &callback)
  @callbacks[event] = callback
end

#post(path, payload = nil, options: {}, type: nil, &block) ⇒ Object

Sends a POST request

Parameters:

  • path (String)

    Resource path

  • payload (String, Hash) (defaults to: nil)

    Data to send (Hash is JSON-encoded automatically)

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

    CoAP options (e.g. { 12 => 50 } for Content-Format=application/json)

  • type (Integer, nil) (defaults to: nil)

    CoAP message type

  • callback (Proc)

    (optional) Callback function for processing the response



47
48
49
# File 'lib/takagi/client_base.rb', line 47

def post(path, payload = nil, options: {}, type: nil, &block)
  request(:post, path, payload, options: options, type: type, &block)
end

#post_json(path, data, options: {}, &block) ⇒ Object

Sends a POST request with JSON payload (convenience method)

Parameters:

  • path (String)

    Resource path

  • data (Hash, Array)

    Data to encode as JSON

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

    CoAP options

  • callback (Proc)

    (optional) Callback function for processing the response



85
86
87
88
89
# File 'lib/takagi/client_base.rb', line 85

def post_json(path, data, options: {}, &block)
  cf = Takagi::CoAP::Registries::ContentFormat::JSON
  merged = { Takagi::CoAP::Registries::Option::CONTENT_FORMAT => cf }.merge(options)
  request(:post, path, JSON.generate(data), options: merged, &block)
end

#put(path, payload = nil, options: {}, type: nil, &block) ⇒ Object

Sends a PUT request

Parameters:

  • path (String)

    Resource path

  • payload (String, Hash) (defaults to: nil)

    Data to send

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

    CoAP options

  • type (Integer, nil) (defaults to: nil)

    CoAP message type

  • callback (Proc)

    (optional) Callback function for processing the response



57
58
59
# File 'lib/takagi/client_base.rb', line 57

def put(path, payload = nil, options: {}, type: nil, &block)
  request(:put, path, payload, options: options, type: type, &block)
end

#put_json(path, data, options: {}, &block) ⇒ Object

Sends a PUT request with JSON payload (convenience method)



92
93
94
95
96
# File 'lib/takagi/client_base.rb', line 92

def put_json(path, data, options: {}, &block)
  cf = Takagi::CoAP::Registries::ContentFormat::JSON
  merged = { Takagi::CoAP::Registries::Option::CONTENT_FORMAT => cf }.merge(options)
  request(:put, path, JSON.generate(data), options: merged, &block)
end