assinafy 2.1.1 → 2.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -41,6 +41,10 @@ module Assinafy
41
41
  attr_reader :signer_documents
42
42
  # @return [Resources::AssignmentResource]
43
43
  attr_reader :assignments
44
+ # @return [Resources::EnvelopeResource]
45
+ attr_reader :envelopes
46
+ # @return [Resources::ScheduledSendResource]
47
+ attr_reader :scheduled_sends
44
48
  # @return [Resources::WebhookResource]
45
49
  attr_reader :webhooks
46
50
  # @return [Resources::TemplateResource]
@@ -90,6 +94,8 @@ module Assinafy
90
94
  @signers = Resources::SignerResource.new(@connection, account_id, @logger)
91
95
  @signer_documents = Resources::SignerDocumentResource.new(@connection, nil, @logger)
92
96
  @assignments = Resources::AssignmentResource.new(@connection, account_id, @logger)
97
+ @envelopes = Resources::EnvelopeResource.new(@connection, account_id, @logger)
98
+ @scheduled_sends = Resources::ScheduledSendResource.new(@connection, account_id, @logger)
93
99
  @webhooks = Resources::WebhookResource.new(@connection, account_id, @logger)
94
100
  @templates = Resources::TemplateResource.new(@connection, account_id, @logger)
95
101
  @fields = Resources::FieldResource.new(@connection, account_id, @logger)
@@ -33,7 +33,7 @@ module Assinafy
33
33
  # @!attribute [rw] api_key
34
34
  # @return [String, nil] sent as `X-Api-Key`
35
35
  # @!attribute [rw] token
36
- # @return [String, nil] legacy bearer token (used when `api_key` is nil)
36
+ # @return [String, nil] session or OAuth bearer token (used when `api_key` is blank)
37
37
  # @!attribute [rw] account_id
38
38
  # @return [String, nil] default workspace ID
39
39
  # @!attribute [rw] base_url
@@ -48,11 +48,11 @@ module Assinafy
48
48
  attr_reader :base_url, :timeout
49
49
 
50
50
  # Build a configuration directly from keyword arguments. Prefer passing
51
- # `api_key` (the documented `X-Api-Key` mechanism); `token` is the legacy
51
+ # `api_key` (the documented `X-Api-Key` mechanism); `token` is the session/OAuth
52
52
  # bearer fallback. `base_url` has its trailing slash stripped on assignment.
53
53
  #
54
54
  # @param api_key [String, nil] sent as the `X-Api-Key` header
55
- # @param token [String, nil] legacy bearer token (used only when `api_key` is nil)
55
+ # @param token [String, nil] session or OAuth bearer token (used only when `api_key` is blank)
56
56
  # @param account_id [String, nil] default workspace account ID
57
57
  # @param base_url [String] API base URL (trailing slash is stripped)
58
58
  # @param webhook_secret [String, nil] secret for {Support::WebhookVerifier}
@@ -39,7 +39,7 @@ module Assinafy
39
39
  attr_reader :response_data
40
40
  # @return [String, nil] the API's `name` field (framework errors only)
41
41
  attr_reader :error_name
42
- # @return [Integer, String, nil] the API's `code` field, when present
42
+ # @return [Integer, String, nil] the API's `code` field, including actionable refusal codes
43
43
  attr_reader :error_code
44
44
 
45
45
  def initialize(message, status_code, response_data = nil)
@@ -79,7 +79,8 @@ module Assinafy
79
79
  end
80
80
 
81
81
  # Build an {ApiError} from an HTTP status and parsed body. Reads the human
82
- # message from `message`, `error`, or `name` (in that order).
82
+ # message from `message`, `error`, or `name` (in that order). Field errors
83
+ # remain available as `response_data['errors']` with their field, code and message.
83
84
  #
84
85
  # @param status_code [Integer]
85
86
  # @param response_data [Hash, Object]
@@ -73,12 +73,14 @@ module Assinafy
73
73
  end
74
74
 
75
75
  # Fetch an account by ID (defaults to the client's account).
76
+ # Pass `expand: 'signing_features'` to include current plan gates for signing.
76
77
  #
77
78
  # @param account_id_override [String, nil]
78
79
  # @return [Hash] the account (envelope `data` unwrapped)
79
80
  # @raise [Assinafy::ApiError] on an unsuccessful API response
80
81
  # @raise [Assinafy::NetworkError] on transport or TLS failure
81
82
  # @raise [Assinafy::ValidationError] on invalid required input
83
+ # @param expand [String, nil] `signing_features` adds five boolean feature flags
82
84
  # @see GET /accounts/{account_id}
83
85
  # @example Fetch the current account
84
86
  # # Request: GET /accounts/{account_id}
@@ -93,11 +95,14 @@ module Assinafy
93
95
  # 'limits' => { 'webhook_endpoints' => 1 },
94
96
  # 'created_at' => '2026-05-12T18:05:11Z'
95
97
  # }
96
- def get(account_id_override = nil)
98
+ def get(account_id_override = nil, expand: nil)
97
99
  acc_id = account_id(account_id_override)
100
+ unless expand.nil? || expand == 'signing_features'
101
+ raise ValidationError.new('expand must be signing_features')
102
+ end
98
103
 
99
104
  call('Failed to fetch account') do
100
- http_get("accounts/#{acc_id}")
105
+ http_get("accounts/#{acc_id}", { expand: expand })
101
106
  end
102
107
  end
103
108
 
@@ -303,8 +308,191 @@ module Assinafy
303
308
  end
304
309
  end
305
310
 
311
+ # Read the plan name, renewal date, document allowance and credit balance.
312
+ # Billing data is unavailable to third-party OAuth applications.
313
+ #
314
+ # @param account_id_override [String, nil]
315
+ # @return [Hash] AccountUsage
316
+ # @raise [Assinafy::ValidationError] on an invalid account ID
317
+ # @raise [Assinafy::ApiError] on an unsuccessful API response
318
+ # @raise [Assinafy::NetworkError] on transport or TLS failure
319
+ # @see GET /accounts/{accountId}/usage
320
+ # @example Read the workspace allowance
321
+ # client.accounts.usage
322
+ # # Request: GET /accounts/{accountId}/usage (no body)
323
+ # # => { 'plan' => 'Pro', 'documents_renewal_date' => '2026-12-01',
324
+ # # 'balances' => { 'documents' => { 'balance' => 90, 'limit' => 100, 'spent' => 10 },
325
+ # # 'credits' => { 'balance' => 40, 'limit' => nil, 'spent' => 5 } } }
326
+ def usage(account_id_override = nil)
327
+ acc_id = account_id(account_id_override)
328
+ call('Failed to fetch account usage') { http_get("accounts/#{acc_id}/usage") }
329
+ end
330
+
331
+ # Read workspace document-alert rules. `editable` identifies owners who can change them.
332
+ # Third-party OAuth applications cannot access this policy.
333
+ #
334
+ # @param account_id_override [String, nil]
335
+ # @return [Hash] NotificationPolicy with all event flags
336
+ # @raise [Assinafy::ValidationError] on an invalid account ID
337
+ # @raise [Assinafy::ApiError] on an unsuccessful API response
338
+ # @raise [Assinafy::NetworkError] on transport or TLS failure
339
+ # @see GET /accounts/{accountId}/notification-policy
340
+ # @example Read workspace notification rules
341
+ # client.accounts.notification_policy
342
+ # # Request: GET /accounts/{accountId}/notification-policy (no body)
343
+ # # => { 'editable' => true, 'owners_receive_all' => false, 'events' => {
344
+ # # 'DocumentCompleted' => true, 'SignerDeclined' => true, 'DocumentCancelled' => true,
345
+ # # 'DocumentAboutToExpire' => true, 'DocumentExpired' => true, 'DocumentExpirationReset' => true,
346
+ # # 'DocumentProcessingFailed' => true, 'TemplateProcessingFailed' => true,
347
+ # # 'SignerWhatsappFailed' => true, 'ContractRenewalNotice' => true, 'ContractExpiring' => true,
348
+ # # 'ContractExpired' => true, 'ContractRenewed' => true, 'SignerViewed' => false,
349
+ # # 'SignerSigned' => true, 'SignerIdentityReviewRequired' => true } }
350
+ def notification_policy(account_id_override = nil)
351
+ acc_id = account_id(account_id_override)
352
+ call('Failed to fetch notification policy') { http_get("accounts/#{acc_id}/notification-policy") }
353
+ end
354
+
355
+ # Merge workspace notification rules. Only owners can write this policy.
356
+ # Disabling identity-review alerts stops push only; its email is always sent.
357
+ # Third-party OAuth applications cannot access this policy.
358
+ #
359
+ # @param payload [Hash] partial `events` map and/or boolean `owners_receive_all`
360
+ # @param account_id_override [String, nil]
361
+ # @return [Hash] the full NotificationPolicy shown by {#notification_policy}
362
+ # @raise [Assinafy::ValidationError] on empty, unknown or invalid fields
363
+ # @raise [Assinafy::ApiError] on an unsuccessful API response
364
+ # @raise [Assinafy::NetworkError] on transport or TLS failure
365
+ # @see PUT /accounts/{accountId}/notification-policy
366
+ # @example Stop signer-viewed alerts
367
+ # client.accounts.update_notification_policy(events: { SignerViewed: false }, owners_receive_all: true)
368
+ # # Request: PUT /accounts/{accountId}/notification-policy
369
+ # # Body: { "events": { "SignerViewed": false }, "owners_receive_all": true }
370
+ # # Response: the full policy from #notification_policy with owners_receive_all true.
371
+ def update_notification_policy(payload, account_id_override = nil)
372
+ acc_id = account_id(account_id_override)
373
+ raw = require_payload(payload, 'Notification policy').transform_keys(&:to_s)
374
+ validate_notification_policy!(raw)
375
+ call('Failed to update notification policy') do
376
+ http_put("accounts/#{acc_id}/notification-policy", body_params(raw))
377
+ end
378
+ end
379
+
380
+ # List allowed embedding origins, oldest first. Requires embedded signing on the plan;
381
+ # OAuth applications cannot manage origins.
382
+ #
383
+ # @param account_id_override [String, nil]
384
+ # @return [Array<Hash>] EmbedOrigin objects
385
+ # @raise [Assinafy::ValidationError] on an invalid account ID
386
+ # @raise [Assinafy::ApiError] on an unsuccessful API response
387
+ # @raise [Assinafy::NetworkError] on transport or TLS failure
388
+ # @see GET /accounts/{accountId}/embed-origins
389
+ # @example List embedding origins
390
+ # client.accounts.list_embed_origins
391
+ # # Request: GET /accounts/{accountId}/embed-origins (no body)
392
+ # # => [{ 'id' => 'origin-id', 'origin' => 'https://app.example.com',
393
+ # # 'created_at' => '2026-11-04T12:05:00Z' }]
394
+ def list_embed_origins(account_id_override = nil)
395
+ acc_id = account_id(account_id_override)
396
+ call_array('Failed to list embed origins') { http_get("accounts/#{acc_id}/embed-origins") }
397
+ end
398
+
399
+ # Allow an exact HTTPS origin to embed signing pages. HTTP is accepted only for localhost
400
+ # and 127.0.0.1. At most ten origins are allowed; requires the plan feature and non-OAuth auth.
401
+ #
402
+ # @param origin [String] scheme://host[:port], optionally with a trailing slash
403
+ # @param account_id_override [String, nil]
404
+ # @return [Hash] created EmbedOrigin
405
+ # @raise [Assinafy::ValidationError] on an invalid origin or account ID
406
+ # @raise [Assinafy::ApiError] on an unsuccessful API response
407
+ # @raise [Assinafy::NetworkError] on transport or TLS failure
408
+ # @see POST /accounts/{accountId}/embed-origins
409
+ # @example Allow the application's origin
410
+ # client.accounts.create_embed_origin('https://app.example.com')
411
+ # # Request: POST /accounts/{accountId}/embed-origins
412
+ # # Body: { "origin": "https://app.example.com" }
413
+ # # => { 'id' => 'origin-id', 'origin' => 'https://app.example.com',
414
+ # # 'created_at' => '2026-11-04T12:05:00Z' }
415
+ def create_embed_origin(origin, account_id_override = nil)
416
+ acc_id = account_id(account_id_override)
417
+ validate_embed_origin!(origin)
418
+ call('Failed to create embed origin') do
419
+ http_post("accounts/#{acc_id}/embed-origins", body_params(origin: origin))
420
+ end
421
+ end
422
+
423
+ # Replace one allowed origin, retaining its ID. The same restrictions as {#create_embed_origin} apply.
424
+ #
425
+ # @param origin_id [String]
426
+ # @param origin [String] exact allowed origin
427
+ # @param account_id_override [String, nil]
428
+ # @return [Hash] updated EmbedOrigin
429
+ # @raise [Assinafy::ValidationError] on an invalid origin or ID
430
+ # @raise [Assinafy::ApiError] on an unsuccessful API response
431
+ # @raise [Assinafy::NetworkError] on transport or TLS failure
432
+ # @see PUT /accounts/{accountId}/embed-origins/{originId}
433
+ # @example Replace an embedding origin
434
+ # client.accounts.update_embed_origin('origin-id', 'https://new.example.com')
435
+ # # Request: PUT /accounts/{accountId}/embed-origins/origin-id
436
+ # # Body: { "origin": "https://new.example.com" }
437
+ # # => { 'id' => 'origin-id', 'origin' => 'https://new.example.com',
438
+ # # 'created_at' => '2026-11-04T12:05:00Z' }
439
+ def update_embed_origin(origin_id, origin, account_id_override = nil)
440
+ acc_id = account_id(account_id_override)
441
+ oid = require_id(origin_id, 'Origin ID')
442
+ validate_embed_origin!(origin)
443
+ call('Failed to update embed origin') do
444
+ http_put("accounts/#{acc_id}/embed-origins/#{oid}", body_params(origin: origin))
445
+ end
446
+ end
447
+
448
+ # Remove an allowed origin. Removing the last origin prevents new embed sessions.
449
+ # Requires embedded signing on the plan; OAuth applications cannot manage origins.
450
+ #
451
+ # @param origin_id [String]
452
+ # @param account_id_override [String, nil]
453
+ # @return [nil] the empty success payload is discarded
454
+ # @raise [Assinafy::ValidationError] on an invalid ID
455
+ # @raise [Assinafy::ApiError] on an unsuccessful API response
456
+ # @raise [Assinafy::NetworkError] on transport or TLS failure
457
+ # @see DELETE /accounts/{accountId}/embed-origins/{originId}
458
+ # @example Stop allowing an origin
459
+ # client.accounts.delete_embed_origin('origin-id')
460
+ # # Request: DELETE /accounts/{accountId}/embed-origins/origin-id (no body)
461
+ # # Response data: []; SDK return: nil
462
+ def delete_embed_origin(origin_id, account_id_override = nil)
463
+ acc_id = account_id(account_id_override)
464
+ oid = require_id(origin_id, 'Origin ID')
465
+ call_void('Failed to delete embed origin') { http_delete("accounts/#{acc_id}/embed-origins/#{oid}") }
466
+ end
467
+
306
468
  private
307
469
 
470
+ def validate_notification_policy!(body)
471
+ if body.empty? || (body.keys - %w[events owners_receive_all]).any?
472
+ raise ValidationError.new('Notification policy requires events and/or owners_receive_all')
473
+ end
474
+
475
+ require_boolean(body['owners_receive_all'], 'owners_receive_all') if body.key?('owners_receive_all')
476
+ return unless body.key?('events')
477
+
478
+ events = require_payload(body['events'], 'Notification policy events')
479
+ codes = UserResource::NOTIFICATION_PREFERENCE_CODES | UserResource::PUSH_PREFERENCE_CODES
480
+ require_preferences(events, codes, 'Notification policy events') unless events.empty?
481
+ end
482
+
483
+ def validate_embed_origin!(origin)
484
+ uri = URI.parse(require_string(origin, 'Embed origin'))
485
+ secure = uri.is_a?(URI::HTTPS) || (uri.is_a?(URI::HTTP) && %w[localhost
486
+ 127.0.0.1].include?(uri.host.to_s.downcase))
487
+ valid = secure && uri.host && !uri.host.include?('*') && (1..65_535).cover?(uri.port) &&
488
+ ['', '/'].include?(uri.path) && !uri.userinfo && !uri.query && !uri.fragment
489
+ return if valid
490
+
491
+ raise ValidationError.new('Embed origin must be an exact HTTPS origin (HTTP is allowed only on localhost)')
492
+ rescue URI::InvalidURIError
493
+ raise ValidationError.new('Embed origin must be a valid origin URL')
494
+ end
495
+
308
496
  def validate_sender_type!(body)
309
497
  return unless body.key?('notification_sender_type')
310
498
  return if %w[User Account].include?(body['notification_sender_type'])