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
-
#activity ⇒ Array<Hookd::HookActivity>
List long-lived hooks that currently have pending interactions, so you can discover which of your long-lived hooks fired without polling each one.
-
#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, ttl: nil, metadata: 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
#activity ⇒ Array<Hookd::HookActivity>
List long-lived hooks that currently have pending interactions, so you can discover which of your long-lived hooks fired without polling each one. Drain the details with #poll. Returns an empty array when none have fired (or the server has long-lived hooks disabled).
98 99 100 101 102 103 104 105 106 107 |
# File 'lib/hookd/client.rb', line 98 def activity response = get('/activity') hooks = response['hooks'] return [] if hooks.nil? || hooks.empty? || !hooks.is_a?(Array) hooks.map { |h| HookActivity.from_hash(h) } rescue NoMethodError => e raise Error, "Invalid response format: #{e.}" end |
#metrics ⇒ Hash
Get server metrics (requires authentication)
86 87 88 |
# File 'lib/hookd/client.rb', line 86 def metrics get('/metrics') end |
#poll(hook_id) ⇒ Array<Hookd::Interaction>
Poll for interactions on a hook
48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/hookd/client.rb', line 48 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.}" end |
#poll_batch(hook_ids) ⇒ Hash<String, Hash>
Poll for interactions on multiple hooks (batch)
68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/hookd/client.rb', line 68 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.}" end |
#register(count: nil, ttl: nil, metadata: nil) ⇒ Hookd::Hook+
Register one or more hooks
35 36 37 38 39 |
# File 'lib/hookd/client.rb', line 35 def register(count: nil, ttl: nil, metadata: nil) raise ArgumentError, 'count must be a positive integer' if count && (!count.is_a?(Integer) || count < 1) parse_register_response(post('/register', register_body(count, ttl, ))) end |