Skip to content

Commit b94b4f2

Browse files
authored
chore: Update appengine samples to google-cloud-pubsub/v3.x (#1590)
1 parent e8938cd commit b94b4f2

File tree

10 files changed

+46
-31
lines changed

10 files changed

+46
-31
lines changed

CONTRIBUTING.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -254,18 +254,18 @@ require "google/cloud/pubsub"
254254

255255
```ruby
256256
def send_notification message
257-
topic = @pubsub.topic "notifications"
257+
publisher = @pubsub.publisher "notifications"
258258

259-
topic.publish message
259+
publisher.publish message
260260
end
261261
```
262262

263263
##### The client can pull notifications by pulling from subscription
264264

265265
```ruby
266266
def get_latest_notifications
267-
subscription = @pubsub.subscription "mobile-notifications"
268-
messages = subscription.pull
267+
subscriber = @pubsub.subscriber "mobile-notifications"
268+
messages = subscriber.pull
269269
notifications = messages.map { |msg| Notifiction.new msg.data }
270270

271271
notifications

appengine/flexible/pubsub/Gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
source "https://rubygems.org"
1616

17-
gem "google-cloud-pubsub"
17+
gem "google-cloud-pubsub", "~> 3.0"
1818
gem "json"
1919
gem "sinatra"
2020
gem "slim"

appengine/flexible/pubsub/app.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@
1919
require "google/cloud/pubsub"
2020
require "googleauth"
2121

22-
pubsub = Google::Cloud::Pubsub.new
22+
pubsub = Google::Cloud::PubSub.new
2323

2424
# [START gae_flex_pubsub_env]
25-
topic = pubsub.topic ENV["PUBSUB_TOPIC"]
25+
publisher = pubsub.publisher ENV["PUBSUB_TOPIC"]
2626
PUBSUB_VERIFICATION_TOKEN = ENV["PUBSUB_VERIFICATION_TOKEN"]
2727
# [END gae_flex_pubsub_env]
2828

@@ -42,7 +42,7 @@
4242
end
4343

4444
post "/publish" do
45-
topic.publish params[:payload]
45+
publisher.publish params[:payload]
4646

4747
redirect "/", 303
4848
end

appengine/flexible/pubsub/spec/pubsub_spec.rb

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,16 @@ def app
2929
ENV["PUBSUB_TOPIC"] = "flexible-topic" unless ENV["PUBSUB_TOPIC"]
3030
ENV["PUBSUB_VERIFICATION_TOKEN"] = "abc123" unless ENV["PUBSUB_VERIFICATION_TOKEN"]
3131
@topic_name = ENV["PUBSUB_TOPIC"]
32-
@pubsub = Google::Cloud::Pubsub.new
32+
@pubsub = Google::Cloud::PubSub.new
33+
@topic_admin = @pubsub.topic_admin
3334

34-
topic = @pubsub.topic @topic_name
35-
@pubsub.create_topic @topic_name if topic.nil?
35+
begin
36+
@topic = @topic_admin.get_topic topic: @pubsub.topic_path(@topic_name)
37+
rescue Google::Cloud::NotFoundError
38+
@topic = nil
39+
end
40+
41+
@topic_admin.create_topic name: @pubsub.topic_path(@topic_name) if @topic.nil?
3642
require_relative "../app.rb"
3743
end
3844

@@ -89,8 +95,7 @@ def app
8995
end
9096

9197
after :all do
92-
topic = @pubsub.topic @topic_name
93-
topic&.delete
98+
@topic_admin.delete_topic topic: @topic.name if @topic
9499
Google::Auth::IDTokens.forget_sources!
95100
end
96101
end

appengine/flexible/ruby31-and-earlier/pubsub/Gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
source "https://rubygems.org"
1616

17-
gem "google-cloud-pubsub"
17+
gem "google-cloud-pubsub", "~> 3.0"
1818
gem "json"
1919
gem "sinatra"
2020
gem "slim"

appengine/flexible/ruby31-and-earlier/pubsub/app.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@
1919
require "google/cloud/pubsub"
2020
require "googleauth"
2121

22-
pubsub = Google::Cloud::Pubsub.new
22+
pubsub = Google::Cloud::PubSub.new
2323

2424
# [START gae_flex_pubsub_env]
25-
topic = pubsub.topic ENV["PUBSUB_TOPIC"]
25+
publisher = pubsub.publisher ENV["PUBSUB_TOPIC"]
2626
PUBSUB_VERIFICATION_TOKEN = ENV["PUBSUB_VERIFICATION_TOKEN"]
2727
# [END gae_flex_pubsub_env]
2828

@@ -42,7 +42,7 @@
4242
end
4343

4444
post "/publish" do
45-
topic.publish params[:payload]
45+
publisher.publish params[:payload]
4646

4747
redirect "/", 303
4848
end

appengine/flexible/ruby31-and-earlier/pubsub/spec/pubsub_spec.rb

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,16 @@ def app
2929
ENV["PUBSUB_TOPIC"] = "flexible-topic" unless ENV["PUBSUB_TOPIC"]
3030
ENV["PUBSUB_VERIFICATION_TOKEN"] = "abc123" unless ENV["PUBSUB_VERIFICATION_TOKEN"]
3131
@topic_name = ENV["PUBSUB_TOPIC"]
32-
@pubsub = Google::Cloud::Pubsub.new
32+
@pubsub = Google::Cloud::PubSub.new
33+
@topic_admin = @pubsub.topic_admin
3334

34-
topic = @pubsub.topic @topic_name
35-
@pubsub.create_topic @topic_name if topic.nil?
35+
begin
36+
@topic = @topic_admin.get_topic topic: @pubsub.topic_path(@topic_name)
37+
rescue Google::Cloud::NotFoundError
38+
@topic = nil
39+
end
40+
41+
@topic_admin.create_topic name: @pubsub.topic_path(@topic_name) if @topic.nil?
3642
require_relative "../app.rb"
3743
end
3844

@@ -89,8 +95,7 @@ def app
8995
end
9096

9197
after :all do
92-
topic = @pubsub.topic @topic_name
93-
topic&.delete
98+
@topic_admin.delete_topic topic: @topic.name if @topic
9499
Google::Auth::IDTokens.forget_sources!
95100
end
96101
end

appengine/pubsub/Gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
source "https://rubygems.org"
1616

17-
gem "google-cloud-pubsub"
17+
gem "google-cloud-pubsub", "~> 3.0"
1818
gem "json"
1919
gem "sinatra"
2020
gem "slim"

appengine/pubsub/app.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@
1919
require "google/cloud/pubsub"
2020
require "googleauth"
2121

22-
pubsub = Google::Cloud::Pubsub.new
22+
pubsub = Google::Cloud::PubSub.new
2323

2424
# Allows all hosts in development
2525
configure :development do
2626
set :host_authorization, { permitted_hosts: [] }
2727
end
2828

2929
# [START gae_flex_pubsub_env]
30-
topic = pubsub.topic ENV["PUBSUB_TOPIC"]
30+
publisher = pubsub.publisher ENV["PUBSUB_TOPIC"]
3131
PUBSUB_VERIFICATION_TOKEN = ENV["PUBSUB_VERIFICATION_TOKEN"]
3232
# [END gae_flex_pubsub_env]
3333

@@ -47,7 +47,7 @@
4747
end
4848

4949
post "/publish" do
50-
topic.publish params[:payload]
50+
publisher.publish params[:payload]
5151

5252
redirect "/", 303
5353
end

appengine/pubsub/spec/pubsub_spec.rb

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,16 @@ def app
2929
ENV["PUBSUB_TOPIC"] = "flexible-topic" unless ENV["PUBSUB_TOPIC"]
3030
ENV["PUBSUB_VERIFICATION_TOKEN"] = "abc123" unless ENV["PUBSUB_VERIFICATION_TOKEN"]
3131
@topic_name = ENV["PUBSUB_TOPIC"]
32-
@pubsub = Google::Cloud::Pubsub.new
32+
@pubsub = Google::Cloud::PubSub.new
33+
@topic_admin = @pubsub.topic_admin
3334

34-
topic = @pubsub.topic @topic_name
35-
@pubsub.create_topic @topic_name if topic.nil?
35+
begin
36+
@topic = @topic_admin.get_topic topic: @pubsub.topic_path(@topic_name)
37+
rescue Google::Cloud::NotFoundError
38+
@topic = nil
39+
end
40+
41+
@topic_admin.create_topic name: @pubsub.topic_path(@topic_name) if @topic.nil?
3642
require_relative "../app.rb"
3743
end
3844

@@ -90,8 +96,7 @@ def app
9096
end
9197

9298
after :all do
93-
topic = @pubsub.topic @topic_name
94-
topic&.delete
99+
@topic_admin.delete_topic topic: @topic.name if @topic
95100
Google::Auth::IDTokens.forget_sources!
96101
end
97102
end

0 commit comments

Comments
 (0)