Class: TLQClient
- Inherits:
-
Object
- Object
- TLQClient
- Defined in:
- lib/tlq_client.rb,
lib/tlq_client/version.rb
Overview
A client library for communicating with a TLQ (Tiny Little Queue) server.
TLQClient provides a simple interface for message queue operations including adding, retrieving, deleting, and retrying messages.
Constant Summary collapse
- VERSION =
The current version of the TLQClient gem.
"0.2.1"
Instance Attribute Summary collapse
-
#base_uri ⇒ String
readonly
The base URI for API requests.
-
#host ⇒ String
readonly
The hostname of the TLQ server.
-
#port ⇒ Integer
readonly
The port number of the TLQ server.
Instance Method Summary collapse
-
#add_message(body) ⇒ Hash
Adds a message to the queue.
-
#delete_messages(ids) ⇒ Boolean
Deletes messages from the queue permanently.
-
#get_messages(count = 1) ⇒ Array<Hash>
Retrieves messages from the queue.
-
#health_check ⇒ Boolean
Checks if the TLQ server is reachable and responding.
-
#initialize(host: 'localhost', port: 1337) ⇒ TLQClient
constructor
Creates a new TLQClient instance.
-
#purge_queue ⇒ Boolean
Purges all messages from the queue.
-
#retry_messages(ids) ⇒ Boolean
Returns messages to the queue for reprocessing.
Constructor Details
#initialize(host: 'localhost', port: 1337) ⇒ TLQClient
Creates a new TLQClient instance.
47 48 49 50 51 |
# File 'lib/tlq_client.rb', line 47 def initialize(host: 'localhost', port: 1337) @host = host @port = port @base_uri = "http://#{@host}:#{@port}" end |
Instance Attribute Details
#base_uri ⇒ String (readonly)
Returns the base URI for API requests.
35 36 37 |
# File 'lib/tlq_client.rb', line 35 def base_uri @base_uri end |
#host ⇒ String (readonly)
Returns the hostname of the TLQ server.
29 30 31 |
# File 'lib/tlq_client.rb', line 29 def host @host end |
#port ⇒ Integer (readonly)
Returns the port number of the TLQ server.
32 33 34 |
# File 'lib/tlq_client.rb', line 32 def port @port end |
Instance Method Details
#add_message(body) ⇒ Hash
Adds a message to the queue.
69 70 71 72 73 74 75 76 |
# File 'lib/tlq_client.rb', line 69 def (body) response = post('/add', { body: body }) JSON.parse(response.body) rescue JSON::ParserError => e raise "Failed to parse response: #{e.message}" rescue StandardError => e raise "HTTP request failed: #{e.message}" end |
#delete_messages(ids) ⇒ Boolean
Deletes messages from the queue permanently.
Use this method to acknowledge successful processing of messages. Once deleted, messages cannot be recovered.
124 125 126 127 128 129 |
# File 'lib/tlq_client.rb', line 124 def (ids) response = post('/delete', { ids: Array(ids) }) response.body == '"Success"' rescue StandardError => e raise "HTTP request failed: #{e.message}" end |
#get_messages(count = 1) ⇒ Array<Hash>
Retrieves messages from the queue.
Messages are leased to the client and must be either deleted (to acknowledge successful processing) or retried (to return them to the queue).
98 99 100 101 102 103 104 105 106 |
# File 'lib/tlq_client.rb', line 98 def (count = 1) response = post('/get', { count: count }) parsed = JSON.parse(response.body) parsed.is_a?(Array) ? parsed : Array(parsed['messages'] || parsed) rescue JSON::ParserError => e raise "Failed to parse response: #{e.message}" rescue StandardError => e raise "HTTP request failed: #{e.message}" end |
#health_check ⇒ Boolean
Checks if the TLQ server is reachable and responding.
181 182 183 184 185 186 187 |
# File 'lib/tlq_client.rb', line 181 def health_check uri = URI("#{@base_uri}/hello") response = Net::HTTP.get_response(uri) response.body == '"Hello World"' rescue StandardError => e raise "Health check failed: #{e.message}" end |
#purge_queue ⇒ Boolean
This operation is irreversible. All messages will be permanently deleted.
Purges all messages from the queue.
164 165 166 167 168 169 |
# File 'lib/tlq_client.rb', line 164 def purge_queue response = post('/purge', {}) response.body == '"Success"' rescue StandardError => e raise "HTTP request failed: #{e.message}" end |
#retry_messages(ids) ⇒ Boolean
Returns messages to the queue for reprocessing.
Use this method when message processing fails and you want to retry later. The messages will be returned to the queue and can be retrieved again.
147 148 149 150 151 152 |
# File 'lib/tlq_client.rb', line 147 def (ids) response = post('/retry', { ids: Array(ids) }) response.body == '"Success"' rescue StandardError => e raise "HTTP request failed: #{e.message}" end |