From 50c976bb2801bb34c22d4ca9a8710660b5f4f3fe Mon Sep 17 00:00:00 2001 From: Mateusz Wiktor Date: Fri, 23 Oct 2026 11:35:39 +0200 Subject: [PATCH] docs: add Access Manager categories snippets Add docs-snippets/access_manager.rb with grant_token, parse_token and grant snippets that use category permissions. Co-Authored-By: Claude Sonnet 5.5 --- docs-snippets/access_manager.rb | 49 +++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 docs-snippets/access_manager.rb diff --git a/docs-snippets/access_manager.rb b/docs-snippets/access_manager.rb new file mode 100644 index 000000000..99573f4e8 --- /dev/null +++ b/docs-snippets/access_manager.rb @@ -0,0 +1,49 @@ +require 'pubnub' + +pubnub = Pubnub.new( + subscribe_key: 'demo', + publish_key: 'demo', + secret_key: 'demo', + user_id: 'myUniqueUserId' +) + +# snippet.grantTokenWithCategories +# Grants `my-authorized-user-id` permission to list all channel metadata and all uuid metadata. +# Category permissions support only the `get` operation. +envelope = pubnub.grant_token( + ttl: 15, + authorized_user_id: 'my-authorized-user-id', + categories: { + channels: Pubnub::Permissions.cat(get: true), + uuids: Pubnub::Permissions.cat(get: true) + }, + http_sync: true +) + +puts envelope.result[:data][:token] +# snippet.end + +# snippet.parseTokenWithCategories +# Decodes a token that was granted with category permissions. +# The `cat` section holds the permission bitmask for each category: `chan` for channel +# metadata and `uuid` for uuid metadata. The `get` permission has the value 32. +parsed_token = pubnub.parse_token('use-token-string-generated-by-grant_token') + +categories = parsed_token['cat'] +puts "Channel metadata category: #{categories['chan']}" +puts "Uuid metadata category: #{categories['uuid']}" +# snippet.end + +# snippet.grantWithCategories +# Grants the auth key permission to list all channel metadata and all uuid metadata. +# This legacy `grant` method requires the secret key to be set on the client. +envelope = pubnub.grant( + categories: %w[channels uuids], + auth_key: 'my-auth-key', + get: true, + ttl: 15, + http_sync: true +) + +puts envelope.status +# snippet.end