pubnub 4.3.0 → 4.4.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of pubnub might be problematic. Click here for more details.

Files changed (118) hide show
  1. checksums.yaml +4 -4
  2. data/.pubnub.yml +32 -24
  3. data/CHANGELOG.md +5 -0
  4. data/Gemfile.lock +1 -1
  5. data/README.md +1 -1
  6. data/VERSION +1 -1
  7. data/fixtures/vcr_cassettes/examples/grant/1913.yml +1 -1
  8. data/fixtures/vcr_cassettes/examples/grant/1945.yml +355 -4
  9. data/fixtures/vcr_cassettes/lib/events/get_all_channels_metadata.yml +39 -0
  10. data/fixtures/vcr_cassettes/lib/events/get_all_uuid_metadata.yml +76 -0
  11. data/fixtures/vcr_cassettes/lib/events/{get_space_memberships.yml → get_channel_members.yml} +5 -6
  12. data/fixtures/vcr_cassettes/lib/events/get_channel_metadata.yml +37 -0
  13. data/fixtures/vcr_cassettes/lib/events/{get_members.yml → get_memberships.yml} +10 -11
  14. data/fixtures/vcr_cassettes/lib/events/get_uuid_metadata.yml +76 -0
  15. data/fixtures/vcr_cassettes/lib/events/grant-error.yml +2 -2
  16. data/fixtures/vcr_cassettes/lib/events/grant.yml +10 -11
  17. data/fixtures/vcr_cassettes/lib/events/remove_channel_members.yml +78 -0
  18. data/fixtures/vcr_cassettes/lib/events/{delete_space.yml → remove_channel_metadata.yml} +5 -6
  19. data/fixtures/vcr_cassettes/lib/events/{manage_memberships_remove.yml → remove_memberships.yml} +8 -9
  20. data/fixtures/vcr_cassettes/lib/events/remove_uuid_metadata.yml +76 -0
  21. data/fixtures/vcr_cassettes/lib/events/set_channel_members.yml +39 -0
  22. data/fixtures/vcr_cassettes/lib/events/set_channel_metadata.yml +39 -0
  23. data/fixtures/vcr_cassettes/lib/events/{manage_members_add.yml → set_memberships.yml} +8 -9
  24. data/fixtures/vcr_cassettes/lib/events/set_uuid_metadata.yml +78 -0
  25. data/fixtures/vcr_cassettes/lib/events/{subscribe-space-delete-async.yml → subscribe-channel-metadata-remove-sync.yml} +12 -14
  26. data/fixtures/vcr_cassettes/lib/events/subscribe-channel-metadata-set-async.yml +75 -0
  27. data/fixtures/vcr_cassettes/lib/events/{subscribe-member-remove-async.yml → subscribe-member-remove-sync.yml} +12 -14
  28. data/fixtures/vcr_cassettes/lib/events/subscribe-member-set-async.yml +75 -0
  29. data/fixtures/vcr_cassettes/lib/events/subscribe-membership-remove-async.yml +12 -14
  30. data/fixtures/vcr_cassettes/lib/events/{subscribe-user-update-async.yml → subscribe-membership-set-async.yml} +12 -14
  31. data/fixtures/vcr_cassettes/lib/events/{subscribe-user-delete-async.yml → subscribe-uuid-metadata-remove-sync.yml} +12 -14
  32. data/fixtures/vcr_cassettes/lib/events/subscribe-uuid-metadata-set-async.yml +75 -0
  33. data/lib/pubnub/client.rb +14 -14
  34. data/lib/pubnub/client/events.rb +4 -2
  35. data/lib/pubnub/constants.rb +19 -18
  36. data/lib/pubnub/event.rb +29 -15
  37. data/lib/pubnub/event/signature.rb +22 -7
  38. data/lib/pubnub/events/get_all_channels_metadata.rb +92 -0
  39. data/lib/pubnub/events/get_all_uuid_metadata.rb +92 -0
  40. data/lib/pubnub/events/get_channel_members.rb +100 -0
  41. data/lib/pubnub/events/{get_user.rb → get_channel_metadata.rb} +22 -15
  42. data/lib/pubnub/events/get_memberships.rb +100 -0
  43. data/lib/pubnub/events/{get_space.rb → get_uuid_metadata.rb} +22 -15
  44. data/lib/pubnub/events/grant.rb +9 -7
  45. data/lib/pubnub/events/remove_channel_members.rb +117 -0
  46. data/lib/pubnub/events/{delete_space.rb → remove_channel_metadata.rb} +12 -12
  47. data/lib/pubnub/events/remove_memberships.rb +117 -0
  48. data/lib/pubnub/events/{delete_user.rb → remove_uuid_metadata.rb} +12 -12
  49. data/lib/pubnub/events/set_channel_members.rb +120 -0
  50. data/lib/pubnub/events/{create_space.rb → set_channel_metadata.rb} +26 -16
  51. data/lib/pubnub/events/set_memberships.rb +120 -0
  52. data/lib/pubnub/events/{update_space.rb → set_uuid_metadata.rb} +27 -18
  53. data/lib/pubnub/pam.rb +2 -11
  54. data/lib/pubnub/subscribe_callback.rb +1 -3
  55. data/lib/pubnub/subscribe_event/formatter.rb +21 -17
  56. data/lib/pubnub/subscriber.rb +5 -6
  57. data/lib/pubnub/validators/get_all_channels_metadata.rb +29 -0
  58. data/lib/pubnub/validators/get_all_uuid_metadata.rb +29 -0
  59. data/lib/pubnub/validators/get_channel_members.rb +39 -0
  60. data/lib/pubnub/validators/get_channel_metadata.rb +39 -0
  61. data/lib/pubnub/validators/get_memberships.rb +39 -0
  62. data/lib/pubnub/validators/get_uuid_metadata.rb +39 -0
  63. data/lib/pubnub/validators/grant.rb +37 -2
  64. data/lib/pubnub/validators/remove_channel_members.rb +52 -0
  65. data/lib/pubnub/validators/remove_channel_metadata.rb +39 -0
  66. data/lib/pubnub/validators/remove_memberships.rb +52 -0
  67. data/lib/pubnub/validators/remove_uuid_metadata.rb +39 -0
  68. data/lib/pubnub/validators/set_channel_members.rb +78 -0
  69. data/lib/pubnub/validators/set_channel_metadata.rb +62 -0
  70. data/lib/pubnub/validators/set_memberships.rb +78 -0
  71. data/lib/pubnub/validators/set_uuid_metadata.rb +62 -0
  72. data/lib/pubnub/version.rb +1 -1
  73. data/spec/examples/grant_examples_spec.rb +2 -2
  74. data/spec/lib/events/channel_metadata_spec.rb +58 -0
  75. data/spec/lib/events/grant_spec.rb +3 -2
  76. data/spec/lib/events/membership_spec.rb +25 -22
  77. data/spec/lib/events/subscribe_spec.rb +58 -45
  78. data/spec/lib/events/uuid_metadata_spec.rb +56 -0
  79. data/spec/spec_helper.rb +1 -1
  80. metadata +55 -57
  81. data/fixtures/vcr_cassettes/lib/events/create_space.yml +0 -40
  82. data/fixtures/vcr_cassettes/lib/events/create_user.yml +0 -40
  83. data/fixtures/vcr_cassettes/lib/events/delete_user.yml +0 -38
  84. data/fixtures/vcr_cassettes/lib/events/get_space.yml +0 -38
  85. data/fixtures/vcr_cassettes/lib/events/get_spaces.yml +0 -40
  86. data/fixtures/vcr_cassettes/lib/events/get_user.yml +0 -38
  87. data/fixtures/vcr_cassettes/lib/events/get_users.yml +0 -40
  88. data/fixtures/vcr_cassettes/lib/events/manage_members_remove.yml +0 -83
  89. data/fixtures/vcr_cassettes/lib/events/manage_memberships_add.yml +0 -40
  90. data/fixtures/vcr_cassettes/lib/events/subscribe-member-add-async.yml +0 -77
  91. data/fixtures/vcr_cassettes/lib/events/subscribe-membership-add-async.yml +0 -77
  92. data/fixtures/vcr_cassettes/lib/events/subscribe-space-update-async.yml +0 -77
  93. data/fixtures/vcr_cassettes/lib/events/update_space.yml +0 -40
  94. data/fixtures/vcr_cassettes/lib/events/update_user.yml +0 -40
  95. data/lib/pubnub/events/create_user.rb +0 -84
  96. data/lib/pubnub/events/get_members.rb +0 -93
  97. data/lib/pubnub/events/get_space_memberships.rb +0 -93
  98. data/lib/pubnub/events/get_spaces.rb +0 -90
  99. data/lib/pubnub/events/get_users.rb +0 -90
  100. data/lib/pubnub/events/manage_members.rb +0 -105
  101. data/lib/pubnub/events/manage_memberships.rb +0 -105
  102. data/lib/pubnub/events/update_user.rb +0 -86
  103. data/lib/pubnub/validators/create_space.rb +0 -44
  104. data/lib/pubnub/validators/create_user.rb +0 -44
  105. data/lib/pubnub/validators/delete_space.rb +0 -32
  106. data/lib/pubnub/validators/delete_user.rb +0 -32
  107. data/lib/pubnub/validators/get_members.rb +0 -32
  108. data/lib/pubnub/validators/get_space.rb +0 -32
  109. data/lib/pubnub/validators/get_space_memberships.rb +0 -32
  110. data/lib/pubnub/validators/get_spaces.rb +0 -16
  111. data/lib/pubnub/validators/get_user.rb +0 -32
  112. data/lib/pubnub/validators/get_users.rb +0 -16
  113. data/lib/pubnub/validators/manage_members.rb +0 -45
  114. data/lib/pubnub/validators/manage_memberships.rb +0 -45
  115. data/lib/pubnub/validators/update_space.rb +0 -45
  116. data/lib/pubnub/validators/update_user.rb +0 -45
  117. data/spec/lib/events/space_spec.rb +0 -75
  118. data/spec/lib/events/user_spec.rb +0 -75
@@ -0,0 +1,117 @@
1
+ # Toplevel Pubnub module.
2
+ # frozen_string_literal: true
3
+
4
+ module Pubnub
5
+ # Holds RemoveMemberships functionality
6
+ class RemoveMemberships < SingleEvent
7
+ include Concurrent::Async
8
+ include Pubnub::Validator::RemoveMemberships
9
+
10
+ def initialize(options, app)
11
+ @event = current_operation
12
+ @telemetry_name = :l_obj
13
+ @uuid = options[:uuid]
14
+ @limit = [options[:limit], 100].min unless options[:limit].nil?
15
+ @sort = options[:sort].join(",") if options[:sort] && !options[:sort].empty?
16
+ @filter = options[:filter] if options[:filter] && !options[:filter].empty?
17
+ @start = options[:start] if options[:start] && !options[:start].empty?
18
+ @end = options[:end] if options[:start] && !options[:end].empty?
19
+
20
+ if options[:include]
21
+ include = options[:include]
22
+ @include_count = [0, '0', false].include?(include[:count]) ? "0" : "1" unless include[:count].nil?
23
+ @include_custom = "custom" if !include[:custom].nil? && ![0, '0', false].include?(include[:custom])
24
+ @include_channel_metadata = "channel" if !include[:channel_metadata].nil? && ![0, '0', false].include?(include[:channel_metadata])
25
+ @include_channel_custom = "channel.custom" if !include[:channel_custom].nil? && ![0, '0', false].include?(include[:channel_custom])
26
+
27
+ @include = [@include_custom, @include_channel_metadata, @include_channel_custom].reject { |flag| flag.to_s.empty? }
28
+ end
29
+
30
+ @channels = options[:channels] if options[:channels] && !options[:channels].empty?
31
+
32
+ # Collections by default return number of available entries.
33
+ @include_count = "1" if @include_count.nil?
34
+
35
+ super
36
+ end
37
+
38
+ def fire
39
+ Pubnub.logger.debug('Pubnub::RemoveMemberships') { "Fired event #{self.class}" }
40
+
41
+ memberships = @channels.map do |membership|
42
+ { channel: { id: membership[:channel] } }
43
+ end
44
+
45
+ body = Formatter.format_message({ delete: memberships }, @cipher_key, false)
46
+ response = send_request(body)
47
+
48
+ envelopes = fire_callbacks(handle(response, uri))
49
+ finalize_event(envelopes)
50
+ envelopes
51
+ end
52
+
53
+ private
54
+
55
+ def current_operation
56
+ Pubnub::Constants::OPERATION_REMOVE_MEMBERSHIPS
57
+ end
58
+
59
+ def parameters(signature = false)
60
+ parameters = super(signature)
61
+ parameters[:limit] = @limit unless @limit.nil?
62
+ parameters[:sort] = @sort unless @sort.nil?
63
+ parameters[:filter] = @filter unless @filter.nil?
64
+ parameters[:start] = @start unless @start.nil?
65
+ parameters[:end] = @end if @end && !@start
66
+ parameters[:count] = @include_count unless @include_count.nil?
67
+ parameters[:include] = @include.sort.join(",") if @include && !@include.empty?
68
+
69
+ parameters
70
+ end
71
+
72
+ def path
73
+ '/' + [
74
+ 'v2',
75
+ 'objects',
76
+ @subscribe_key,
77
+ 'uuids',
78
+ Formatter.encode(@uuid),
79
+ 'channels'
80
+ ].join('/')
81
+ end
82
+
83
+ def valid_envelope(parsed_response, req_res_objects)
84
+ Pubnub::Envelope.new(
85
+ event: @event,
86
+ event_options: @given_options,
87
+ timetoken: nil,
88
+
89
+ result: {
90
+ code: req_res_objects[:response].code,
91
+ operation: current_operation,
92
+ client_request: req_res_objects[:request],
93
+ server_response: req_res_objects[:response],
94
+ data: parsed_response
95
+ },
96
+
97
+ status: {
98
+ code: req_res_objects[:response].code,
99
+ operation: current_operation,
100
+ client_request: req_res_objects[:request],
101
+ server_response: req_res_objects[:response],
102
+ data: nil,
103
+ category: Pubnub::Constants::STATUS_ACK,
104
+ error: false,
105
+ auto_retried: false,
106
+
107
+ current_timetoken: nil,
108
+ last_timetoken: nil,
109
+ subscribed_channels: nil,
110
+ subscribed_channel_groups: nil,
111
+
112
+ config: get_config
113
+ }
114
+ )
115
+ end
116
+ end
117
+ end
@@ -2,20 +2,20 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  module Pubnub
5
- # Holds DeleteUser functionality
6
- class DeleteUser < SingleEvent
5
+ # Holds RemoveUuidMetadata functionality
6
+ class RemoveUuidMetadata < SingleEvent
7
7
  include Concurrent::Async
8
- include Pubnub::Validator::DeleteUser
8
+ include Pubnub::Validator::RemoveUuidMetadata
9
9
 
10
10
  def initialize(options, app)
11
- @event = :delete_user
11
+ @event = current_operation
12
12
  @telemetry_name = :l_obj
13
- @user_id = options[:user_id]
13
+ @uuid = options[:uuid]
14
14
  super
15
15
  end
16
16
 
17
17
  def fire
18
- Pubnub.logger.debug('Pubnub::DeleteUser') { "Fired event #{self.class}" }
18
+ Pubnub.logger.debug('Pubnub::RemoveUuidMetadata') { "Fired event #{self.class}" }
19
19
 
20
20
  body = Formatter.format_message(@data, @cipher_key, false)
21
21
  response = send_request(body)
@@ -28,16 +28,16 @@ module Pubnub
28
28
  private
29
29
 
30
30
  def current_operation
31
- Pubnub::Constants::OPERATION_DELETE_USER
31
+ Pubnub::Constants::OPERATION_REMOVE_UUID_METADATA
32
32
  end
33
33
 
34
34
  def path
35
35
  '/' + [
36
- 'v1',
36
+ 'v2',
37
37
  'objects',
38
38
  @subscribe_key,
39
- 'users',
40
- @user_id
39
+ 'uuids',
40
+ Formatter.encode(@uuid)
41
41
  ].join('/')
42
42
  end
43
43
 
@@ -49,7 +49,7 @@ module Pubnub
49
49
 
50
50
  result: {
51
51
  code: req_res_objects[:response].code,
52
- operation: Pubnub::Constants::OPERATION_DELETE_USER,
52
+ operation: current_operation,
53
53
  client_request: req_res_objects[:request],
54
54
  server_response: req_res_objects[:response],
55
55
  data: parsed_response
@@ -57,7 +57,7 @@ module Pubnub
57
57
 
58
58
  status: {
59
59
  code: req_res_objects[:response].code,
60
- operation: Pubnub::Constants::OPERATION_DELETE_USER,
60
+ operation: current_operation,
61
61
  client_request: req_res_objects[:request],
62
62
  server_response: req_res_objects[:response],
63
63
  data: nil,
@@ -0,0 +1,120 @@
1
+ # Toplevel Pubnub module.
2
+ # frozen_string_literal: true
3
+
4
+ module Pubnub
5
+ # Holds SetChannelMembers functionality
6
+ class SetChannelMembers < SingleEvent
7
+ include Concurrent::Async
8
+ include Pubnub::Validator::SetChannelMembers
9
+
10
+ def initialize(options, app)
11
+ @event = current_operation
12
+ @telemetry_name = :l_obj
13
+ @channel = options[:channel]
14
+ @limit = [options[:limit], 100].min unless options[:limit].nil?
15
+ @sort = options[:sort].join(",") if options[:sort] && !options[:sort].empty?
16
+ @filter = options[:filter] if options[:filter] && !options[:filter].empty?
17
+ @start = options[:start] if options[:start] && !options[:start].empty?
18
+ @end = options[:end] if options[:start] && !options[:end].empty?
19
+
20
+ if options[:include]
21
+ include = options[:include]
22
+ @include_count = [0, '0', false].include?(include[:count]) ? "0" : "1" unless include[:count].nil?
23
+ @include_custom = "custom" if !include[:custom].nil? && ![0, '0', false].include?(include[:custom])
24
+ @include_uuid_metadata = "uuid" if !include[:uuid_metadata].nil? && ![0, '0', false].include?(include[:uuid_metadata])
25
+ @include_uuid_custom = "uuid.custom" if !include[:uuid_custom].nil? && ![0, '0', false].include?(include[:uuid_custom])
26
+
27
+ @include = [@include_custom, @include_uuid_metadata, @include_uuid_custom].reject { |flag| flag.to_s.empty? }
28
+ end
29
+
30
+ @uuids = options[:uuids] if options[:uuids] && !options[:uuids].empty?
31
+
32
+ # Collections by default return number of available entries.
33
+ @include_count = "1" if @include_count.nil?
34
+
35
+ super
36
+ end
37
+
38
+ def fire
39
+ Pubnub.logger.debug('Pubnub::SetChannelMembers') { "Fired event #{self.class}" }
40
+
41
+ members = @uuids.map do |member|
42
+ member_object = { uuid: { id: member[:uuid] } }
43
+ member_object[:custom] = member[:custom] if member[:custom] && !member[:custom].empty?
44
+
45
+ member_object
46
+ end
47
+
48
+ body = Formatter.format_message({ set: members }, @cipher_key, false)
49
+ response = send_request(body)
50
+
51
+ envelopes = fire_callbacks(handle(response, uri))
52
+ finalize_event(envelopes)
53
+ envelopes
54
+ end
55
+
56
+ private
57
+
58
+ def current_operation
59
+ Pubnub::Constants::OPERATION_SET_CHANNEL_MEMBERS
60
+ end
61
+
62
+ def parameters(signature = false)
63
+ parameters = super(signature)
64
+ parameters[:limit] = @limit unless @limit.nil?
65
+ parameters[:sort] = @sort unless @sort.nil?
66
+ parameters[:filter] = @filter unless @filter.nil?
67
+ parameters[:start] = @start unless @start.nil?
68
+ parameters[:end] = @end if @end && !@start
69
+ parameters[:count] = @include_count unless @include_count.nil?
70
+ parameters[:include] = @include.sort.join(",") if @include && !@include.empty?
71
+
72
+ parameters
73
+ end
74
+
75
+ def path
76
+ '/' + [
77
+ 'v2',
78
+ 'objects',
79
+ @subscribe_key,
80
+ 'channels',
81
+ Formatter.channels_for_url(@channel),
82
+ 'uuids'
83
+ ].join('/')
84
+ end
85
+
86
+ def valid_envelope(parsed_response, req_res_objects)
87
+ Pubnub::Envelope.new(
88
+ event: @event,
89
+ event_options: @given_options,
90
+ timetoken: nil,
91
+
92
+ result: {
93
+ code: req_res_objects[:response].code,
94
+ operation: current_operation,
95
+ client_request: req_res_objects[:request],
96
+ server_response: req_res_objects[:response],
97
+ data: parsed_response
98
+ },
99
+
100
+ status: {
101
+ code: req_res_objects[:response].code,
102
+ operation: current_operation,
103
+ client_request: req_res_objects[:request],
104
+ server_response: req_res_objects[:response],
105
+ data: nil,
106
+ category: Pubnub::Constants::STATUS_ACK,
107
+ error: false,
108
+ auto_retried: false,
109
+
110
+ current_timetoken: nil,
111
+ last_timetoken: nil,
112
+ subscribed_channels: nil,
113
+ subscribed_channel_groups: nil,
114
+
115
+ config: get_config
116
+ }
117
+ )
118
+ end
119
+ end
120
+ end
@@ -2,23 +2,32 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  module Pubnub
5
- # Holds CreateSpace functionality
6
- class CreateSpace < SingleEvent
5
+ # Holds SetChannelMetadata functionality
6
+ class SetChannelMetadata < SingleEvent
7
7
  include Concurrent::Async
8
- include Pubnub::Validator::CreateSpace
8
+ include Pubnub::Validator::SetChannelMetadata
9
9
 
10
10
  def initialize(options, app)
11
- @event = :create_space
11
+ @event = current_operation
12
12
  @telemetry_name = :l_obj
13
- @data = options[:data]
14
- @include = options[:include]
13
+ @channel = options[:channel]
14
+
15
+ # Clean up user-provided metadata object from nils.
16
+ @metadata = options[:metadata].delete_if { |_k, v| v.blank? } unless options[:metadata].nil?
17
+
18
+ if options[:include]
19
+ @include = [0, '0', false].include?(options[:include][:custom]) ? "0" : "1" unless options[:include][:custom].nil?
20
+ end
21
+
22
+ # Single entity creation should return it's 'custom' field by default.
23
+ @include = "custom" if @include.nil?
15
24
  super
16
25
  end
17
26
 
18
27
  def fire
19
- Pubnub.logger.debug('Pubnub::CreateSpace') { "Fired event #{self.class}" }
28
+ Pubnub.logger.debug('Pubnub::SetChannelMetadata') { "Fired event #{self.class}" }
20
29
 
21
- body = Formatter.format_message(@data, @cipher_key, false)
30
+ body = Formatter.format_message(@metadata, @cipher_key, false)
22
31
  response = send_request(body)
23
32
 
24
33
  envelopes = fire_callbacks(handle(response, uri))
@@ -29,21 +38,22 @@ module Pubnub
29
38
  private
30
39
 
31
40
  def current_operation
32
- Pubnub::Constants::OPERATION_CREATE_SPACE
41
+ Pubnub::Constants::OPERATION_SET_CHANNEL_METADATA
33
42
  end
34
43
 
35
- def parameters(*_args)
36
- parameters = super
37
- parameters[:include] = @include if @include
44
+ def parameters(signature = false)
45
+ parameters = super(signature)
46
+ parameters[:include] = @include
38
47
  parameters
39
48
  end
40
49
 
41
50
  def path
42
51
  '/' + [
43
- 'v1',
52
+ 'v2',
44
53
  'objects',
45
54
  @subscribe_key,
46
- 'spaces'
55
+ 'channels',
56
+ Formatter.channels_for_url(@channel)
47
57
  ].join('/')
48
58
  end
49
59
 
@@ -55,7 +65,7 @@ module Pubnub
55
65
 
56
66
  result: {
57
67
  code: req_res_objects[:response].code,
58
- operation: Pubnub::Constants::OPERATION_CREATE_SPACE,
68
+ operation: current_operation,
59
69
  client_request: req_res_objects[:request],
60
70
  server_response: req_res_objects[:response],
61
71
  data: parsed_response
@@ -63,7 +73,7 @@ module Pubnub
63
73
 
64
74
  status: {
65
75
  code: req_res_objects[:response].code,
66
- operation: Pubnub::Constants::OPERATION_CREATE_SPACE,
76
+ operation: current_operation,
67
77
  client_request: req_res_objects[:request],
68
78
  server_response: req_res_objects[:response],
69
79
  data: nil,
@@ -0,0 +1,120 @@
1
+ # Toplevel Pubnub module.
2
+ # frozen_string_literal: true
3
+
4
+ module Pubnub
5
+ # Holds SetMemberships functionality
6
+ class SetMemberships < SingleEvent
7
+ include Concurrent::Async
8
+ include Pubnub::Validator::SetMemberships
9
+
10
+ def initialize(options, app)
11
+ @event = current_operation
12
+ @telemetry_name = :l_obj
13
+ @uuid = options[:uuid]
14
+ @limit = [options[:limit], 100].min unless options[:limit].nil?
15
+ @sort = options[:sort].join(",") if options[:sort] && !options[:sort].empty?
16
+ @filter = options[:filter] if options[:filter] && !options[:filter].empty?
17
+ @start = options[:start] if options[:start] && !options[:start].empty?
18
+ @end = options[:end] if options[:start] && !options[:end].empty?
19
+
20
+ if options[:include]
21
+ include = options[:include]
22
+ @include_count = [0, '0', false].include?(include[:count]) ? "0" : "1" unless include[:count].nil?
23
+ @include_custom = "custom" if !include[:custom].nil? && ![0, '0', false].include?(include[:custom])
24
+ @include_channel_metadata = "channel" if !include[:channel_metadata].nil? && ![0, '0', false].include?(include[:channel_metadata])
25
+ @include_channel_custom = "channel.custom" if !include[:channel_custom].nil? && ![0, '0', false].include?(include[:channel_custom])
26
+
27
+ @include = [@include_custom, @include_channel_metadata, @include_channel_custom].reject { |flag| flag.to_s.empty? }
28
+ end
29
+
30
+ @channels = options[:channels] if options[:channels] && !options[:channels].empty?
31
+
32
+ # Collections by default return number of available entries.
33
+ @include_count = "1" if @include_count.nil?
34
+
35
+ super
36
+ end
37
+
38
+ def fire
39
+ Pubnub.logger.debug('Pubnub::SetMemberships') { "Fired event #{self.class}" }
40
+
41
+ memberships = @channels.map do |membership|
42
+ membership_object = { channel: { id: membership[:channel] } }
43
+ membership_object[:custom] = membership[:custom] if membership[:custom] && !membership[:custom].empty?
44
+
45
+ membership_object
46
+ end
47
+
48
+ body = Formatter.format_message({ set: memberships }, @cipher_key, false)
49
+ response = send_request(body)
50
+
51
+ envelopes = fire_callbacks(handle(response, uri))
52
+ finalize_event(envelopes)
53
+ envelopes
54
+ end
55
+
56
+ private
57
+
58
+ def current_operation
59
+ Pubnub::Constants::OPERATION_SET_MEMBERSHIPS
60
+ end
61
+
62
+ def parameters(signature = false)
63
+ parameters = super(signature)
64
+ parameters[:limit] = @limit unless @limit.nil?
65
+ parameters[:sort] = @sort unless @sort.nil?
66
+ parameters[:filter] = @filter unless @filter.nil?
67
+ parameters[:start] = @start unless @start.nil?
68
+ parameters[:end] = @end if @end && !@start
69
+ parameters[:count] = @include_count unless @include_count.nil?
70
+ parameters[:include] = @include.sort.join(",") if @include && !@include.empty?
71
+
72
+ parameters
73
+ end
74
+
75
+ def path
76
+ '/' + [
77
+ 'v2',
78
+ 'objects',
79
+ @subscribe_key,
80
+ 'uuids',
81
+ Formatter.encode(@uuid),
82
+ 'channels'
83
+ ].join('/')
84
+ end
85
+
86
+ def valid_envelope(parsed_response, req_res_objects)
87
+ Pubnub::Envelope.new(
88
+ event: @event,
89
+ event_options: @given_options,
90
+ timetoken: nil,
91
+
92
+ result: {
93
+ code: req_res_objects[:response].code,
94
+ operation: current_operation,
95
+ client_request: req_res_objects[:request],
96
+ server_response: req_res_objects[:response],
97
+ data: parsed_response
98
+ },
99
+
100
+ status: {
101
+ code: req_res_objects[:response].code,
102
+ operation: current_operation,
103
+ client_request: req_res_objects[:request],
104
+ server_response: req_res_objects[:response],
105
+ data: nil,
106
+ category: Pubnub::Constants::STATUS_ACK,
107
+ error: false,
108
+ auto_retried: false,
109
+
110
+ current_timetoken: nil,
111
+ last_timetoken: nil,
112
+ subscribed_channels: nil,
113
+ subscribed_channel_groups: nil,
114
+
115
+ config: get_config
116
+ }
117
+ )
118
+ end
119
+ end
120
+ end