Skip to content

Commit

Permalink
Make uuid mandatory (#127)
Browse files Browse the repository at this point in the history
  • Loading branch information
kleewho authored Jan 13, 2022
1 parent b382302 commit 3aa4f3b
Show file tree
Hide file tree
Showing 20 changed files with 107 additions and 67 deletions.
13 changes: 9 additions & 4 deletions .pubnub.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
---
version: "4.8.0"
version: "5.0.0"
changelog:
- date: 2022-01-13
version: v5.0.0
changes:
- type: improvement
text: "BREAKING CHANGES: uuid is required parameter to create PubNub instance."
- date: 2021-12-16
version: v4.8.0
changes:
Expand Down Expand Up @@ -621,7 +626,7 @@ sdks:
- x86-64
- distribution-type: package
distribution-repository: RubyGems
package-name: pubnub-4.8.0.gem
package-name: pubnub-5.0.0.gem
location: https://rubygems.org/gems/pubnub
requires:
- name: addressable
Expand Down Expand Up @@ -726,8 +731,8 @@ sdks:
- x86-64
- distribution-type: library
distribution-repository: GitHub release
package-name: pubnub-4.8.0.gem
location: https://github.com/pubnub/ruby/releases/download/v4.8.0/pubnub-4.8.0.gem
package-name: pubnub-5.0.0.gem
location: https://github.com/pubnub/ruby/releases/download/v5.0.0/pubnub-5.0.0.gem
requires:
- name: addressable
min-version: 2.0.0
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## v5.0.0
January 13 2022

#### Modified
- BREAKING CHANGES: uuid is required parameter to create PubNub instance.

## v4.8.0
December 16 2021

Expand Down
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
pubnub (4.8.0)
pubnub (5.0.0)
addressable (>= 2.0.0)
concurrent-ruby (~> 1.1.5)
concurrent-ruby-edge (~> 0.5.0)
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4.8.0
5.0.0
2 changes: 2 additions & 0 deletions features/step_definitions/access_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
logger = Logger.new(STDOUT)
logger.level = Logger::DEBUG
@pn_configuration['logger'] = logger
@pn_configuration['uuid'] = Pubnub::UUID.generate

@pubnub = Pubnub.new(@pn_configuration)
end
Expand All @@ -24,6 +25,7 @@
logger = Logger.new(STDOUT)
logger.level = Logger::DEBUG
@pn_configuration['logger'] = logger
@pn_configuration['uuid'] = Pubnub::UUID.generate

@pubnub = Pubnub.new(@pn_configuration)
end
Expand Down
1 change: 1 addition & 0 deletions lib/pubnub.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

require 'pubnub/version'
require 'pubnub/client'
require 'pubnub/uuid'

# Adding blank? and present? methods to Object.
class Object
Expand Down
5 changes: 0 additions & 5 deletions lib/pubnub/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -384,14 +384,9 @@ def setup_app(options)

def prepare_env
assign_defaults
generate_uuid if @env[:uuid].blank?
setup_pools
end

def generate_uuid
@env[:uuid] = UUID.generate
end

def assign_defaults
@env[:origin] = @env[:origins_pool].first if @env[:origins_pool]
default_values.each do |k, v|
Expand Down
5 changes: 5 additions & 0 deletions lib/pubnub/client/getters_setters.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
require 'pubnub/validators/common_validator'

# Toplevel Pubnub module.
module Pubnub
# Pubnub client Class
class Client
# Module that holds some getters and setters
module GettersSetters

def sdk_version
"PubNub-Ruby/#{Pubnub::VERSION}"
end
Expand All @@ -25,6 +28,8 @@ def sdk_version
def change_uuid(uuid)
Pubnub.logger.debug('Pubnub::Client') { 'Changing uuid' }
raise('Cannot change UUID while subscribed.') if subscribed?
Validator::Client.validate_uuid uuid

@env[:uuid] = uuid
end

Expand Down
1 change: 1 addition & 0 deletions lib/pubnub/validators/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ def validate!(parameters)
validate_origin parameters[:origin], true
validate_subscribe_key parameters[:subscribe_key], true
validate_publish_key parameters[:publish_key]
validate_uuid parameters[:uuid]
end
end
end
Expand Down
17 changes: 17 additions & 0 deletions lib/pubnub/validators/common_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,23 @@ def validate_publish_key(publish_key, required = false)
)
end
end

# Validates given uuid
def validate_uuid(uuid)
if !uuid || uuid.blank?
raise(
InitializationError.new,
'Missing required :uuid parameter.'
)
elsif !([String, Symbol].include?(uuid.class) ||
uuid.blank?)
raise(
InitializationError.new,
'UUID parameter is not valid. \
Should be type of String or Symbol.'
)
end
end
end
end
end
2 changes: 1 addition & 1 deletion lib/pubnub/version.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Toplevel Pubnub module.
module Pubnub
VERSION = '4.8.0'.freeze
VERSION = '5.0.0'.freeze
end
68 changes: 38 additions & 30 deletions spec/lib/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,33 @@

context "#initialize" do
it "returns new Pubnub::Client" do
pubnub = Pubnub.new(subscribe_key: :key)
pubnub = Pubnub.new(uuid: Pubnub::UUID.generate, subscribe_key: :key)
expect(pubnub.is_a?(Pubnub::Client)).to eq true
end

it "requires arguments" do
expect { Pubnub.new }.to raise_error(Pubnub::InitializationError)
end

it "requires uuid" do
expect { Pubnub.new(subscribe_key: :key) }.to raise_error(Pubnub::InitializationError)
end

it "by default sets Pubnub.logger that logs to pubnub.log" do
_pubnub = Pubnub.new(subscribe_key: :key)
_pubnub = Pubnub.new(uuid: Pubnub::UUID.generate, subscribe_key: :key)
expect(Pubnub.logger.instance_eval("@logdev").filename).to eq "pubnub.log"
end

it "sets Pubnub.logger to new one if given" do
_rd, wr = IO.pipe

logger = Logger.new(wr)
_pubnub = Pubnub.new(subscribe_key: :key, logger: logger)
_pubnub = Pubnub.new(uuid: Pubnub::UUID.generate, subscribe_key: :key, logger: logger)
expect(Pubnub.logger).to eq(logger)
end

it "creates required pools" do
pubnub = Pubnub.new(subscribe_key: "key")
pubnub = Pubnub.new(uuid: Pubnub::UUID.generate, subscribe_key: "key")

# Connections
expect(pubnub.env[:single_event_conn_pool]).to eq({})
Expand All @@ -49,77 +53,77 @@

context "parameters" do
it "changes string keys to sym keys" do
pubnub = Pubnub.new("subscribe_key" => "key")
pubnub = Pubnub.new(uuid: Pubnub::UUID.generate, "subscribe_key" => "key")
pubnub.env.each_key do |key|
expect(key.class).to eq Symbol
end
end

it "removes empty parameters" do
pubnub = Pubnub.new(publish_key: "", subscribe_key: "key")
pubnub = Pubnub.new(uuid: Pubnub::UUID.generate, publish_key: "", subscribe_key: "key")
expect(pubnub.env[:publish_key]).to eq(nil)
end

it "does not remove non-empty parameters" do
pubnub = Pubnub.new(publish_key: "", subscribe_key: "key")
pubnub = Pubnub.new(uuid: Pubnub::UUID.generate, publish_key: "", subscribe_key: "key")
expect(pubnub.env[:subscribe_key]).to eq("key")
end

context "validates" do
context "parameter :origin" do
it "must be valid" do
expect { Pubnub.new(origin: 123, subscribe_key: "key") }.to raise_error(Pubnub::InitializationError)
expect { Pubnub.new(uuid: Pubnub::UUID.generate, origin: 123, subscribe_key: "key") }.to raise_error(Pubnub::InitializationError)

expect { Pubnub.new(origin: {a: :b}, subscribe_key: "key") }.to raise_error(Pubnub::InitializationError)
expect { Pubnub.new(uuid: Pubnub::UUID.generate, origin: {a: :b}, subscribe_key: "key") }.to raise_error(Pubnub::InitializationError)

expect { Pubnub.new(origin: nil, subscribe_key: "key") }.not_to raise_error
expect { Pubnub.new(uuid: Pubnub::UUID.generate, origin: nil, subscribe_key: "key") }.not_to raise_error

expect { Pubnub.new(origin: "", subscribe_key: "key") }.not_to raise_error
expect { Pubnub.new(uuid: Pubnub::UUID.generate, origin: "", subscribe_key: "key") }.not_to raise_error

expect { Pubnub.new(origin: "http://a.com", subscribe_key: "key") }.not_to raise_error
expect { Pubnub.new(uuid: Pubnub::UUID.generate, origin: "http://a.com", subscribe_key: "key") }.not_to raise_error
end
end

context "parameter :subscribe_key" do
it "is required" do
expect { Pubnub.new(http_sync: true) }.to raise_error(Pubnub::InitializationError)
expect { Pubnub.new(uuid: Pubnub::UUID.generate, http_sync: true) }.to raise_error(Pubnub::InitializationError)
end

it "must be valid" do
expect { Pubnub.new(subscribe_key: 123) }.to raise_error(Pubnub::InitializationError)
expect { Pubnub.new(uuid: Pubnub::UUID.generate, subscribe_key: 123) }.to raise_error(Pubnub::InitializationError)

expect { Pubnub.new(subscribe_key: []) }.to raise_error(Pubnub::InitializationError)
expect { Pubnub.new(uuid: Pubnub::UUID.generate, subscribe_key: []) }.to raise_error(Pubnub::InitializationError)

expect { Pubnub.new(subscribe_key: {}) }.to raise_error(Pubnub::InitializationError)
expect { Pubnub.new(uuid: Pubnub::UUID.generate, subscribe_key: {}) }.to raise_error(Pubnub::InitializationError)

expect { Pubnub.new(subscribe_key: "key") }.not_to raise_error
expect { Pubnub.new(subscribe_key: :key) }.not_to raise_error
expect { Pubnub.new(uuid: Pubnub::UUID.generate, subscribe_key: "key") }.not_to raise_error
expect { Pubnub.new(uuid: Pubnub::UUID.generate, subscribe_key: :key) }.not_to raise_error
end
end

context "parameter :publish_key" do
it "must be valid" do
expect do
Pubnub.new(subscribe_key: "valid", publish_key: ["invalid"])
Pubnub.new(uuid: Pubnub::UUID.generate, subscribe_key: "valid", publish_key: ["invalid"])
end.to raise_error(Pubnub::InitializationError)

expect do
Pubnub.new(
Pubnub.new(uuid: Pubnub::UUID.generate,
subscribe_key: "valid",
publish_key: {invalid: "yeah"},
)
end.to raise_error(Pubnub::InitializationError)

expect do
Pubnub.new(subscribe_key: "valid", publish_key: ["invalid"])
Pubnub.new(uuid: Pubnub::UUID.generate, subscribe_key: "valid", publish_key: ["invalid"])
end.to raise_error(Pubnub::InitializationError)

expect do
Pubnub.new(subscribe_key: "valid", publish_key: "key")
Pubnub.new(uuid: Pubnub::UUID.generate, subscribe_key: "valid", publish_key: "key")
end.not_to raise_error

expect do
Pubnub.new(subscribe_key: "valid", publish_key: :key)
Pubnub.new(uuid: Pubnub::UUID.generate, subscribe_key: "valid", publish_key: :key)
end.not_to raise_error
end
end
Expand All @@ -128,7 +132,7 @@
end

context "timetoken manipulation" do
let(:pubnub_client) { Pubnub.new(subscribe_key: "demo") }
let(:pubnub_client) { Pubnub.new(uuid: Pubnub::UUID.generate, subscribe_key: "demo") }

context "#timetoken" do
it "returns timetoken hold in env" do
Expand All @@ -146,7 +150,7 @@
end

context "callback listeners" do
let(:pubnub_client) { Pubnub.new(subscribe_key: "demo") }
let(:pubnub_client) { Pubnub.new(uuid: Pubnub::UUID.generate, subscribe_key: "demo") }

it "can be added" do
callbacks = Pubnub::SubscribeCallback.new(
Expand Down Expand Up @@ -198,7 +202,7 @@
end

context "helper methods" do
let(:pubnub_client) { Pubnub.new(subscribe_key: "demo") }
let(:pubnub_client) { Pubnub.new(uuid: Pubnub::UUID.generate, subscribe_key: "demo") }

it "can change uuid while not subscribed" do
expect(pubnub_client.change_uuid("whatever")).to eq "whatever"
Expand All @@ -210,6 +214,10 @@
expect { pubnub_client.change_uuid("whatever") }.to raise_error(RuntimeError)
end

it "cannot change uuid to empty" do
expect { pubnub_client.change_uuid("") }.to raise_error(Pubnub::InitializationError)
end

it "can show what channels are subscribed" do
expect(pubnub_client.subscribed?).to eq(false)
pubnub_client.subscribe(channel: ["demo", "demo.*"], presence: "demo", group: "demo")
Expand All @@ -222,7 +230,7 @@

context "connections" do
it "are keep_alive by default" do
pubnub = Pubnub.new(subscribe_key: :demo, publish_key: :demo)
pubnub = Pubnub.new(uuid: Pubnub::UUID.generate, subscribe_key: :demo, publish_key: :demo)

VCR.use_cassette("lib/client/keep_alive_default", record: :once) do
pubnub.subscribe(channel: :demo)
Expand All @@ -233,7 +241,7 @@
end

it "respect :disable_keep_alive" do
pubnub = Pubnub.new(subscribe_key: :demo, publish_key: :demo, disable_keepalive: true)
pubnub = Pubnub.new(uuid: Pubnub::UUID.generate, subscribe_key: :demo, publish_key: :demo, disable_keepalive: true)

VCR.use_cassette("lib/client/keep_alive_disabled", record: :once) do
pubnub.subscribe(channel: :demo)
Expand All @@ -244,7 +252,7 @@
end

it "respect :disable_subscribe_keep_alive init parameter" do
pubnub = Pubnub.new(subscribe_key: :demo, publish_key: :demo, disable_subscribe_keepalive: true)
pubnub = Pubnub.new(uuid: Pubnub::UUID.generate, subscribe_key: :demo, publish_key: :demo, disable_subscribe_keepalive: true)

VCR.use_cassette("lib/client/keep_alive_disable_subscribe_keep_alive", record: :once) do
pubnub.subscribe(channel: :demo)
Expand All @@ -258,7 +266,7 @@
end

it "respect :disable_non_subscribe_keep_alive init parameter" do
pubnub = Pubnub.new(subscribe_key: :demo, publish_key: :demo, disable_non_subscribe_keepalive: true)
pubnub = Pubnub.new(uuid: Pubnub::UUID.generate, subscribe_key: :demo, publish_key: :demo, disable_non_subscribe_keepalive: true)

VCR.use_cassette("lib/client/keep_alive_disable_non_subscribe_keep_alive", record: :once) do
pubnub.subscribe(channel: :demo)
Expand Down
2 changes: 1 addition & 1 deletion spec/lib/connection_callback_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
end,
)

@pubnub = Pubnub.new(
@pubnub = Pubnub.new(uuid: Pubnub::UUID.generate,
subscribe_key: :demo,
publish_key: :demo,
reconnect_interval: 0,
Expand Down
4 changes: 2 additions & 2 deletions spec/lib/event_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
end

context "#initialize" do
let(:pubnub_client) { Pubnub.new(subscribe_key: "demo") }
let(:pubnub_client) { Pubnub.new(uuid: Pubnub::UUID.generate, subscribe_key: "demo") }

it "sets :channel_group parameter as @group internal variable" do
event = described_class.new({channel_group: "somegroup", skip_validate: true}, pubnub_client)
Expand All @@ -28,7 +28,7 @@
it "logs when initialized" do
rd, wr = IO.pipe
logger = Logger.new(wr)
pubnub = Pubnub.new(subscribe_key: :key, logger: logger)
pubnub = Pubnub.new(uuid: Pubnub::UUID.generate, subscribe_key: :key, logger: logger)

described_class.new({channel: "channel", skip_validate: true}, pubnub)

Expand Down
2 changes: 1 addition & 1 deletion spec/lib/events/message_counts_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

describe Pubnub::Presence do

let(:client) {Pubnub.new(subscribe_key: :key)}
let(:client) {Pubnub.new(uuid: Pubnub::UUID.generate, subscribe_key: :key)}

around :each do |example|
example.run_with_retry retry: 10
Expand Down
Loading

0 comments on commit 3aa4f3b

Please sign in to comment.