File tree Expand file tree Collapse file tree 10 files changed +46
-31
lines changed
ruby31-and-earlier/pubsub Expand file tree Collapse file tree 10 files changed +46
-31
lines changed Original file line number Diff line number Diff line change @@ -254,18 +254,18 @@ require "google/cloud/pubsub"
254254
255255``` ruby
256256def send_notification message
257- topic = @pubsub .topic " notifications"
257+ publisher = @pubsub .publisher " notifications"
258258
259- topic .publish message
259+ publisher .publish message
260260end
261261```
262262
263263##### The client can pull notifications by pulling from subscription
264264
265265``` ruby
266266def 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
Original file line number Diff line number Diff line change 1414
1515source "https://rubygems.org"
1616
17- gem "google-cloud-pubsub"
17+ gem "google-cloud-pubsub" , "~> 3.0"
1818gem "json"
1919gem "sinatra"
2020gem "slim"
Original file line number Diff line number Diff line change 1919require "google/cloud/pubsub"
2020require "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" ]
2626PUBSUB_VERIFICATION_TOKEN = ENV [ "PUBSUB_VERIFICATION_TOKEN" ]
2727# [END gae_flex_pubsub_env]
2828
4242end
4343
4444post "/publish" do
45- topic . publish params [ :payload ]
45+ publisher . publish params [ :payload ]
4646
4747 redirect "/" , 303
4848end
Original file line number Diff line number Diff 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
96101end
Original file line number Diff line number Diff line change 1414
1515source "https://rubygems.org"
1616
17- gem "google-cloud-pubsub"
17+ gem "google-cloud-pubsub" , "~> 3.0"
1818gem "json"
1919gem "sinatra"
2020gem "slim"
Original file line number Diff line number Diff line change 1919require "google/cloud/pubsub"
2020require "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" ]
2626PUBSUB_VERIFICATION_TOKEN = ENV [ "PUBSUB_VERIFICATION_TOKEN" ]
2727# [END gae_flex_pubsub_env]
2828
4242end
4343
4444post "/publish" do
45- topic . publish params [ :payload ]
45+ publisher . publish params [ :payload ]
4646
4747 redirect "/" , 303
4848end
Original file line number Diff line number Diff 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
96101end
Original file line number Diff line number Diff line change 1414
1515source "https://rubygems.org"
1616
17- gem "google-cloud-pubsub"
17+ gem "google-cloud-pubsub" , "~> 3.0"
1818gem "json"
1919gem "sinatra"
2020gem "slim"
Original file line number Diff line number Diff line change 1919require "google/cloud/pubsub"
2020require "googleauth"
2121
22- pubsub = Google ::Cloud ::Pubsub . new
22+ pubsub = Google ::Cloud ::PubSub . new
2323
2424# Allows all hosts in development
2525configure :development do
2626 set :host_authorization , { permitted_hosts : [ ] }
2727end
2828
2929# [START gae_flex_pubsub_env]
30- topic = pubsub . topic ENV [ "PUBSUB_TOPIC" ]
30+ publisher = pubsub . publisher ENV [ "PUBSUB_TOPIC" ]
3131PUBSUB_VERIFICATION_TOKEN = ENV [ "PUBSUB_VERIFICATION_TOKEN" ]
3232# [END gae_flex_pubsub_env]
3333
4747end
4848
4949post "/publish" do
50- topic . publish params [ :payload ]
50+ publisher . publish params [ :payload ]
5151
5252 redirect "/" , 303
5353end
Original file line number Diff line number Diff 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
97102end
You can’t perform that action at this time.
0 commit comments