| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
The Ruby SDK for Hook0, the open source Webhooks-as-a-Service platform for SaaS applications. It sends events, declares the event types your application uses, verifies the signature of a webhook you receive, and calls every operation the API declares through generated, documented types.
It reaches the network, verifies signatures and decodes what the API answers with the standard library alone: net/http for requests, openssl for signatures, json for documents, securerandom for identifiers. Installing it never drags a transitive dependency into an application that only wanted to send an event.
gem install hook0-clientOr add gem "hook0-client" to your Gemfile. Ruby 3.1 or later.
require "hook0"
client = Hook0::Client.new(
"https://app.hook0.com/api/v1",
application_id,
token
)
event_id = client.send_event(
Hook0::Event.new(
event_type: "billing.invoice.paid",
payload: '{"invoice": "in_123"}',
payload_content_type: "application/json",
labels: { "environment" => "production" }
)
)begin
Hook0.verify_webhook_signature(
request.headers["X-Hook0-Signature"],
request.body,
request.headers,
subscription_secret,
300
)
rescue Hook0::ClientError
# answer 400, and do not act on the delivery
endThe clock window is bilateral, so a delivery dated too far ahead is refused exactly like one dated too far behind, because a window that only looked backwards is one a sender widens by dating its own delivery in the future. A header the signature covers but the request did not carry is refused before any code is computed.
Every bound one send is held to is yours to set, and every one has a default.
| Bound | Default | What it holds back |
|---|---|---|
| max_attempts | 4 | requests one send issues, capped at 16 whatever a policy says |
| initial_backoff | 100 ms | the ceiling of the wait before the first retry |
| max_backoff | 2 s | the ceiling no single wait between attempts crosses |
| max_total_delay | 5 s | the budget every wait of one send shares |
| request_timeout | 10 s | how long one attempt is given |
| max_payload_bytes | 1 MiB | the payload, refused before a socket is opened |
| max_response_bytes | 8 MiB | the body read off a socket |
| max_head_bytes | 16 KiB | the head of an answer, every line taken together |
| max_response_headers | 64 | header lines one answer may carry |
| max_header_bytes | 64 KiB | one header line |
Every default comes from clients/conformance/bounds.json, the corpus every Hook0 SDK reads. A number changed there fails every SDK still carrying the old one, so no two of them can bound different things.
The last three bound what the other end may cost you. A server that is broken or hostile can otherwise stream a head, a header or a body of any length into your process.
client = Hook0::Client.new(
"https://app.hook0.com/api/v1",
application_id,
token,
Hook0::Options.new(
retry_policy: Hook0::RetryPolicy.new(
max_attempts: 4,
initial_backoff: 0.1,
max_backoff: 2.0,
max_total_delay: 5.0
),
request_timeout: 10.0,
max_payload_bytes: 1024 * 1024,
max_response_bytes: 8 * 1024 * 1024
)
)send_event sends every event under an ID it knows, either the one set on the event or a UUIDv7 it mints when the event carries none. Passing no ID does not mean the ID comes from Hook0. The value comes from the client, travels with the request, and is what send_event answers.
That is what makes a retry safe. Hook0 keys events on their ID, so a request repeated after a network failure or a server error ingests the event once rather than twice. Without a client-chosen ID, the repeated request would create a second event and deliver it to every subscriber.
Retrying is limited to what could end differently. A request that got no answer, a server error and an instance saying it is being reached faster than it accepts are all retried. A 429 naming a spent quota is not, because a quota clears when a plan changes or a day turns, and no send can wait for that. A Retry-After the answer carries is honoured, clamped to what is left of the delay budget. A retried request Hook0 answers with EventAlreadyIngested reports success, since an earlier attempt of that same send reached the API. The same answer to a first attempt is a genuine conflict, and is reported as an error.
created = client.upsert_event_types(
%w[billing.invoice.paid billing.invoice.voided]
)Only the ones your application does not declare yet are created, and those are what comes back.
Every operation the API declares is a method of a generated group, one group per entity.
applications = Hook0::Generated::ApplicationsApi.new(
Hook0::Transport.new("https://app.hook0.com", token)
)
begin
application = applications.get(application_id)
rescue Hook0::Generated::NotFoundError
# every problem the API names is its own exception, all of them `ProblemError`
endA value read out of an answer is one of the classes under Hook0::Generated, and to_h writes one back the way the API reads it. A member Ruby keeps for itself, method or until, is spelled with a trailing underscore in Ruby and under its own name on the wire.
clients/ruby/lib/hook0/generated/ is written by hook0-sdkgen from the OpenAPI snapshot the API commits, and is rewritten whole on every regeneration. A hand edit there is reverted the next time anyone regenerates, and the drift guard says so before that. Change the generator, then run:
UPDATE_SDK=ruby cargo test -p hook0-sdkgen sdk_targets
Everything else under lib/ is hand-written and never regenerated, and so is test/.
What a send retries, the bounds it is held to and how a signature is verified are dictated by the shared corpus at clients/conformance, which every SDK's suite reads, so a verdict changed there fails this client until it agrees again.
Every case runs against a real Hook0 over a loopback socket. Nothing here stands in for a part of the client.
rubocop
ruby -Ilib -Itest -e 'Dir["test/**/*_test.rb"].sort.each { |f| require File.expand_path(f) }'
The Hook0 Ruby SDK is free and open source, released under the MIT License. Use it, change it, ship it, in open source and in commercial work alike, as long as the copyright notice travels with it.
Hook0 itself is open source too. Read what Hook0 is, visit hook0.com, join the community, or write to support@hook0.com.
Maintained by David Sferruzza and François-Guillaume Ribreau.
| Back | FazBrowse Home | New Git URL |