Class: Takagi::ClientBase
- Inherits:
-
Object
- Object
- Takagi::ClientBase
- 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.
Instance Attribute Summary collapse
-
#callbacks ⇒ Object
readonly
Returns the value of attribute callbacks.
-
#server_uri ⇒ Object
readonly
Returns the value of attribute server_uri.
-
#timeout ⇒ Object
readonly
Returns the value of attribute timeout.
Class Method Summary collapse
-
.open(server_uri, timeout: 5, **options) {|client| ... } ⇒ Object
Creates a new client and yields it to the block, ensuring it’s closed afterward.
Instance Method Summary collapse
-
#close ⇒ Object
Closes the client and releases any resources.
-
#closed? ⇒ Boolean
Check if the client has been closed.
-
#delete(path, options: {}, type: nil, &block) ⇒ Object
Sends a DELETE request.
-
#get(path, options: {}, type: nil, &block) ⇒ Object
Sends a GET request.
-
#get_json(path, options: {}, &block) ⇒ Object
Sends a GET request and automatically parses JSON response (convenience method).
-
#initialize(server_uri, timeout: 5) ⇒ ClientBase
constructor
Initializes the base client.
-
#observe(path, options: {}, &block) ⇒ Object
Sends an OBSERVE request (RFC 7641).
-
#on(event, &callback) ⇒ Object
Registers a callback for a given event.
-
#post(path, payload = nil, options: {}, type: nil, &block) ⇒ Object
Sends a POST request.
-
#post_json(path, data, options: {}, &block) ⇒ Object
Sends a POST request with JSON payload (convenience method).
-
#put(path, payload = nil, options: {}, type: nil, &block) ⇒ Object
Sends a PUT request.
-
#put_json(path, data, options: {}, &block) ⇒ Object
Sends a PUT request with JSON payload (convenience method).
Constructor Details
#initialize(server_uri, timeout: 5) ⇒ ClientBase
Initializes the base client
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
#callbacks ⇒ Object (readonly)
Returns the value of attribute callbacks.
13 14 15 |
# File 'lib/takagi/client_base.rb', line 13 def callbacks @callbacks end |
#server_uri ⇒ Object (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 |
#timeout ⇒ Object (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.
146 147 148 149 150 151 |
# File 'lib/takagi/client_base.rb', line 146 def self.open(server_uri, timeout: 5, **, &block) client = new(server_uri, timeout: timeout, **) block.call(client) ensure client&.close end |
Instance Method Details
#close ⇒ Object
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
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
66 67 68 |
# File 'lib/takagi/client_base.rb', line 66 def delete(path, options: {}, type: nil, &block) request(:delete, path, nil, options: , type: type, &block) end |
#get(path, options: {}, type: nil, &block) ⇒ Object
Sends a GET request
37 38 39 |
# File 'lib/takagi/client_base.rb', line 37 def get(path, options: {}, type: nil, &block) request(:get, path, nil, 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() 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:`.
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() request(:get, path, nil, options: merged, &block) end |
#on(event, &callback) ⇒ Object
Registers a callback for a given 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
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: , type: type, &block) end |
#post_json(path, data, options: {}, &block) ⇒ Object
Sends a POST request with JSON payload (convenience method)
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() request(:post, path, JSON.generate(data), options: merged, &block) end |
#put(path, payload = nil, options: {}, type: nil, &block) ⇒ Object
Sends a PUT request
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: , 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() request(:put, path, JSON.generate(data), options: merged, &block) end |