Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 78 additions & 1 deletion lib/beyond_api/services/webhook/subscription.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def delete_all
# @example
# @client.delete('84ce4b3a-9a10-46d0-afcd-a71bcd0c0e9a')
def delete(id)
super("webhook-subscriptions/#{id}") # Concerns::Connection delete method
super("webhook-subscriptions/#{id}")
end

# Retrieve the details of a webhook subscription.
Expand All @@ -79,6 +79,83 @@ def delete(id)
def find(id)
get("webhook-subscriptions/#{id}")
end

# Activate a webhook subscription. You might want to use this request for a deactivated webhook.
#
# @see https://developer.epages.com/beyond-docs/#activate_webhook_subscription
#
# @param id [String] the webhook subscription UUID
#
# @return [Hash]
#
# @example
# @client.activate('1d61c1b4-49aa-4827-bd04-b1c4374f3499')
def activate(id)
post("webhook-subscriptions/#{id}/activate")
end

# Deactivate a webhook subscription. You might want to use this request for a currently active webhook.
#
# @see https://developer.epages.com/beyond-docs/#deactivate_webhook_subscription
#
# @param id [String] the webhook subscription UUID
#
# @return [Hash]
#
# @example
# @client.deactivate('8517ab39-db0e-417a-87ed-f96e437c03e4')
def deactivate(id)
post("webhook-subscriptions/#{id}/deactivate")
end

# List all failed deliveries of a webhook subscription in a paged way.
#
# @see https://developer.epages.com/beyond-docs/#list_failed_webhook_deliveries
#
# @param id [String] the webhook subscription UUID
# @option params [Boolean] :paginated
# @option params [Integer] :size the page size
# @option params [Integer] :page the page number
#
# @return [Hash]
#
# @example
# @client.failures('a3dd55a4-dbc3-4f20-8a5e-d0fe4ba59afd', { size: 100, page: 0 })
def failures(id, params = {})
fetch_all_pages("webhook-subscriptions/#{id}/failures", params)
end

# Retry a failed webhook delivery.
#
# @see https://developer.epages.com/beyond-docs/#retry_failed_webhook_delivery
#
# @param webhook_id [String] the webhook subscription UUID
# @param failure_id [String] the failure UUID
#
# @return [Hash]
#
# @example
# @client.retry('d9828fba-3922-4750-a70c-174daeeb37e4',
# 'b21146e0-16b9-468f-806b-a23c8e04a7fc')
def retry(webhook_id, failure_id)
post("webhook-subscriptions/#{webhook_id}/failures/#{failure_id}/retry")
end

# Delete a webhook failure.
#
# @see https://developer.epages.com/beyond-docs/#delete_webhook_delivery_failure
#
# @param webhook_id [String] the webhook subscription UUID
# @param failure_id [String] the failure UUID
#
# @return [Hash] an empty hash
#
# @example
# @client.delete_failure('cbcebfc4-9dee-4344-83b2-8c25b2287a60',
# 'f090610b-19da-4e40-a986-3c9562fefeab')
def delete_failure(webhook_id, failure_id)
delete("webhook-subscriptions/#{webhook_id}/failures/#{failure_id}")
end
end
end
end
40 changes: 40 additions & 0 deletions spec/beyond_api/services/webhook/subscription_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,46 @@
end
end

describe '.deactivate' do
it 'deactivates a webhook subscription' do
response = client.deactivate(@webhook_subscription[:id])
expect(response).not_to be nil
expect(response[:active]).to eq(false)
end
end

describe '.activate' do
it 'activates a webhook subscription' do
response = client.activate(@webhook_subscription[:id])
expect(response).not_to be nil
expect(response[:active]).to eq(true)
end
end

describe '.failures' do
it 'lists all failed deliveries of a webhook subscription' do
response = client.failures(@webhook_subscription[:id], { size: 100, page: 0 })
expect(response).not_to be nil
expect(response.dig(:embedded, :failures)).to be_kind_of(Array)
end
end

describe '.retry' do
it 'retries a failed webhook delivery' do
failure_id = '987e6543-e21b-45d3-b123-654321098765'
# Non existing failure ID will raise an error
expect { client.retry(@webhook_subscription[:id], failure_id) }.to raise_error(BeyondApi::Error)
end
end

describe '.delete_failure' do
it 'deletes a webhook failure' do
failure_id = '123e4567-e89b-12d3-a456-426614174000'
# Non existing failure ID will raise an error
expect { client.delete_failure(@webhook_subscription[:id], failure_id) }.to raise_error(BeyondApi::Error)
end
end

after(:each) do
client.delete(@webhook_subscription[:id])
rescue BeyondApi::Error # rubocop:disable Lint/SuppressedException
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading