Class: Takagi::ClientBase
- Inherits:
-
Object
- Object
- Takagi::ClientBase
- Defined in:
- lib/takagi/client_base.rb,
sig/takagi/client_base.rbs
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
-
#cleanup_resources ⇒ nil
Subclasses can override this to perform specific cleanup Called by #close before marking the client as closed.
-
#close ⇒ nil, untyped
Closes the client and releases any resources.
-
#closed? ⇒ Boolean
Check if the client has been closed.
-
#delete(path, options: {}, type: nil) {|arg0| ... } ⇒ Object
Sends a DELETE request.
-
#deliver_response(response_data, &callback) ⇒ void
Delivers a response using the callback or registered callback Wraps raw response data in a Response object for convenience.
-
#get(path, options: {}, type: nil) {|arg0| ... } ⇒ Object
Sends a GET request.
-
#get_json(path, options: {}) {|data| ... } ⇒ Hash, ...
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) {|arg0| ... } ⇒ Object
Registers a callback for a given event.
-
#parse_json_response(response_data) ⇒ Hash, ...
Helper to parse JSON response from raw data.
-
#post(path, payload = nil, options: {}, type: nil) {|arg0| ... } ⇒ Object
Sends a POST request.
-
#post_json(path, data, options: {}) {|arg0| ... } ⇒ Object
Sends a POST request with JSON payload (convenience method).
-
#put(path, payload = nil, options: {}, type: nil) {|arg0| ... } ⇒ Object
Sends a PUT request.
-
#put_json(path, data, options: {}) {|arg0| ... } ⇒ Object
Sends a PUT request with JSON payload (convenience method).
-
#request(_method, _path, _payload = nil, options: {}, type: nil) {|arg0| ... } ⇒ Object
Subclasses must implement this method to perform the actual request.
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
#cleanup_resources ⇒ nil
Subclasses can override this to perform specific cleanup Called by #close before marking the client as closed
168 169 170 |
# File 'lib/takagi/client_base.rb', line 168 def cleanup_resources # Default: no-op end |
#close ⇒ nil, untyped
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) {|arg0| ... } ⇒ 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 |
#deliver_response(response_data, &callback) ⇒ void
This method returns an undefined value.
Delivers a response using the callback or registered callback Wraps raw response data in a Response object for convenience
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/takagi/client_base.rb', line 176 def deliver_response(response_data, &callback) # Wrap response in Response object for better DX require_relative 'client/response' response = Client::Response.new(response_data) return callback.call(response) if callback return @callbacks[:response].call(response) if @callbacks[:response] # Default: print response details if response.success? puts "[#{response.code_name}] #{response.payload}" else puts "[ERROR #{response.code_name}] #{response.payload}" end end |
#get(path, options: {}, type: nil) {|arg0| ... } ⇒ 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: {}) {|data| ... } ⇒ Hash, ...
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) {|arg0| ... } ⇒ 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 |
#parse_json_response(response_data) ⇒ Hash, ...
Helper to parse JSON response from raw data
195 196 197 198 199 200 201 202 |
# File 'lib/takagi/client_base.rb', line 195 def parse_json_response(response_data) inbound = Takagi::Message::Inbound.new(response_data) return nil unless inbound.payload JSON.parse(inbound.payload) rescue JSON::ParserError nil end |
#post(path, payload = nil, options: {}, type: nil) {|arg0| ... } ⇒ 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: {}) {|arg0| ... } ⇒ 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) {|arg0| ... } ⇒ 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: {}) {|arg0| ... } ⇒ 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 |
#request(_method, _path, _payload = nil, options: {}, type: nil) {|arg0| ... } ⇒ Object
Subclasses must implement this method to perform the actual request
162 163 164 |
# File 'lib/takagi/client_base.rb', line 162 def request(_method, _path, _payload = nil, options: {}, type: nil, &_callback) raise NotImplementedError, "#{self.class} must implement #request" end |