@@ -87,9 +87,56 @@ def async_publisher
8787 # `projects/{project_id}/topics/{topic_id}`.
8888 #
8989 def name
90+ return @resource_name if reference?
9091 @grpc . name
9192 end
9293
94+ ##
95+ # Determines whether the publisher object was created with a resource
96+ # representation from the Pub/Sub service.
97+ #
98+ # @return [Boolean] `true` when the publisher was created with a resource
99+ # representation, `false` otherwise.
100+ #
101+ def reference?
102+ @grpc . nil?
103+ end
104+
105+ ##
106+ # Determines whether the publisher object was created with a resource
107+ # representation from the Pub/Sub service.
108+ #
109+ # @return [Boolean] `true` when the publisher was created with a resource
110+ # representation, `false` otherwise.
111+ #
112+ def resource?
113+ !@grpc . nil?
114+ end
115+
116+
117+ ##
118+ # Reloads the publisher with current data from the Pub/Sub service.
119+ #
120+ # @return [Google::Cloud::PubSub::Publisher] Returns the reloaded
121+ # publisher
122+ #
123+ # @example
124+ # require "google/cloud/pubsub"
125+ #
126+ # pubsub = Google::Cloud::PubSub.new
127+ #
128+ # publisher = pubsub.publisher "my-topic", skip_lookup: true
129+ #
130+ # publisher.reload!
131+ #
132+ def reload!
133+ ensure_service!
134+ topic_path = service . topic_path name
135+ @grpc = service . topic_admin . get_topic topic : topic_path
136+ @resource_name = nil
137+ self
138+ end
139+
93140 ##
94141 # Publishes one or more messages to the publisher.
95142 #
@@ -166,7 +213,7 @@ def name
166213 #
167214 def publish data = nil , attributes = nil , ordering_key : nil , compress : nil , compression_bytes_threshold : nil ,
168215 **extra_attrs , &block
169- ensure_service !
216+ ensure_grpc !
170217 batch = BatchPublisher . new data ,
171218 attributes ,
172219 ordering_key ,
@@ -290,8 +337,7 @@ def publish data = nil, attributes = nil, ordering_key: nil, compress: nil, comp
290337 # publisher.async_publisher.stop!
291338 #
292339 def publish_async data = nil , attributes = nil , ordering_key : nil , **extra_attrs , &callback
293- ensure_service!
294-
340+ ensure_grpc!
295341 @async_publisher ||= AsyncPublisher . new name , service , **@async_opts
296342 @async_publisher . publish data , attributes , ordering_key : ordering_key , **extra_attrs , &callback
297343 end
@@ -351,6 +397,16 @@ def self.from_grpc grpc, service, async: nil
351397 end
352398 end
353399
400+ ##
401+ # @private New reference {Publisher} from a topic name without making
402+ # a HTTP request.
403+ def self . from_name name , service , options = { }
404+ name = service . topic_path name , options
405+ from_grpc ( nil , service , async : options [ :async ] ) . tap do |t |
406+ t . instance_variable_set :@resource_name , name
407+ end
408+ end
409+
354410 protected
355411
356412 ##
@@ -364,6 +420,7 @@ def ensure_service!
364420 # Ensures a Google::Cloud::PubSub::V1::Topic object exists.
365421 def ensure_grpc!
366422 ensure_service!
423+ reload! if reference?
367424 end
368425 end
369426 end
0 commit comments