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

#metricsHash

Get server metrics (requires authentication)

Returns:

  • (Hash)

    metrics data

Raises:



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

Parameters:

  • hook_id (String)

    the hook ID to poll

Returns:

Raises:



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)

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:



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"
  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) ⇒ Hookd::Hook+

Register one or more hooks

Parameters:

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

    number of hooks to register (default: 1)

Returns:

Raises:



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