Class: Hookd::Client
- Inherits:
-
Object
- Object
- Hookd::Client
- Defined in:
- lib/hookd/client.rb
Overview
HTTP client for interacting with Hookd server
Instance Attribute Summary collapse
-
#server ⇒ Object
readonly
Returns the value of attribute server.
-
#token ⇒ Object
readonly
Returns the value of attribute token.
Instance Method Summary collapse
-
#initialize(server:, token:) ⇒ Client
constructor
A new instance of Client.
-
#metrics ⇒ Hash
Get server metrics (requires authentication).
-
#poll(hook_id) ⇒ Array<Hookd::Interaction>
Poll for interactions on a hook.
-
#poll_batch(hook_ids) ⇒ Hash<String, Hash>
Poll for interactions on multiple hooks (batch).
-
#register(count: nil) ⇒ Hookd::Hook+
Register one or more hooks.
Constructor Details
#initialize(server:, token:) ⇒ Client
Returns a new instance of Client.
11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/hookd/client.rb', line 11 def initialize(server:, token:) @server = server @token = token @http = HTTPX.with( headers: { 'X-API-Key' => token }, timeout: { connect_timeout: 10, read_timeout: 30 } ) end |
Instance Attribute Details
#server ⇒ Object (readonly)
Returns the value of attribute server.
9 10 11 |
# File 'lib/hookd/client.rb', line 9 def server @server end |
#token ⇒ Object (readonly)
Returns the value of attribute token.
9 10 11 |
# File 'lib/hookd/client.rb', line 9 def token @token end |
Instance Method Details
#metrics ⇒ Hash
Get server metrics (requires authentication)
91 92 93 |
# File 'lib/hookd/client.rb', line 91 def metrics get('/metrics') end |
#poll(hook_id) ⇒ Array<Hookd::Interaction>
Poll for interactions on a hook
53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/hookd/client.rb', line 53 def poll(hook_id) response = get("/poll/#{hook_id}") # Response is {"interactions": [...]} interactions = response['interactions'] return [] if interactions.nil? || interactions.empty? || !interactions.is_a?(Array) interactions.map { |i| Interaction.from_hash(i) } rescue NoMethodError => e raise Error, "Invalid response format: #{e.message}" end |
#poll_batch(hook_ids) ⇒ Hash<String, Hash>
Poll for interactions on multiple hooks (batch)
73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/hookd/client.rb', line 73 def poll_batch(hook_ids) validate_hook_ids(hook_ids) url = "#{@server}/poll" = { headers: { 'Content-Type' => 'application/json' }, json: hook_ids } response = @http.post(url, **) response_data = handle_response(response) transform_batch_results(response_data['results']) rescue NoMethodError => e raise Error, "Invalid response format: #{e.message}" end |
#register(count: nil) ⇒ Hookd::Hook+
Register one or more hooks
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/hookd/client.rb', line 30 def register(count: nil) body = count.nil? ? nil : { count: count } raise ArgumentError, 'count must be a positive integer' if count && (!count.is_a?(Integer) || count < 1) response = post('/register', body) # Single hook response (backward compatible) return Hook.from_hash(response) if response.key?('id') # Multiple hooks response return [] if response['hooks'].nil? || response['hooks'].empty? response['hooks'].map { |h| Hook.from_hash(h) } end |