Class: Hookd::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/hookd/client.rb

Overview

HTTP client for interacting with Hookd server

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#serverObject (readonly)

Returns the value of attribute server.



9
10
11
# File 'lib/hookd/client.rb', line 9

def server
  @server
end

#tokenObject (readonly)

Returns the value of attribute token.



9
10
11
# File 'lib/hookd/client.rb', line 9

def token
  @token
end

Instance Method Details

#activityArray<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).

Returns:

Raises:



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.message}"
end

#metricsHash

Get server metrics (requires authentication)

Returns:

  • (Hash)

    metrics data

Raises:



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

Parameters:

  • hook_id (String)

    the hook ID to poll

Returns:

Raises:



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.message}"
end

#poll_batch(hook_ids) ⇒ Hash<String, Hash>

Poll for interactions on multiple hooks (batch)

Parameters:

  • hook_ids (Array<String>)

    the hook IDs to poll

Returns:

  • (Hash<String, Hash>)

    hash mapping hook_id to results Results format: { "hook_id" => { interactions: [...], error: "..." } }

Raises:



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"
  options = { headers: { 'Content-Type' => 'application/json' }, json: hook_ids }
  response = @http.post(url, **options)
  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, ttl: nil, metadata: nil) ⇒ Hookd::Hook+

Register one or more hooks

Parameters:

  • count (Integer, nil) (defaults to: nil)

    number of hooks to register (default: 1)

  • ttl (String, nil) (defaults to: nil)

    lifetime as a Go duration ("168h") or day count ("7d"); a value above the server's ephemeral hook_ttl registers a durable long-lived hook. Omit for an ephemeral hook.

  • metadata (Hash, nil) (defaults to: nil)

    arbitrary data stored with the hook and echoed back on poll

Returns:

Raises:



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