google-cloud-pubsub 0.20.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,176 @@
1
+ # Copyright 2015 Google Inc. All rights reserved.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+
16
+ require "delegate"
17
+
18
+ module Google
19
+ module Cloud
20
+ module Pubsub
21
+ class Topic
22
+ ##
23
+ # Topic::List is a special case Array with additional values.
24
+ class List < DelegateClass(::Array)
25
+ ##
26
+ # If not empty, indicates that there are more topics
27
+ # that match the request and this value should be passed to
28
+ # the next {Google::Cloud::Pubsub::Project#topics} to continue.
29
+ attr_accessor :token
30
+
31
+ ##
32
+ # @private Create a new Topic::List with an array of values.
33
+ def initialize arr = []
34
+ super arr
35
+ end
36
+
37
+ ##
38
+ # Whether there a next page of topics.
39
+ #
40
+ # @return [Boolean]
41
+ #
42
+ # @example
43
+ # require "google/cloud"
44
+ #
45
+ # gcloud = Google::Cloud.new
46
+ # pubsub = gcloud.pubsub
47
+ #
48
+ # topics = pubsub.topics
49
+ # if topics.next?
50
+ # next_topics = topics.next
51
+ # end
52
+ #
53
+ def next?
54
+ !token.nil?
55
+ end
56
+
57
+ ##
58
+ # Retrieve the next page of topics.
59
+ #
60
+ # @return [Topic::List]
61
+ #
62
+ # @example
63
+ # require "google/cloud"
64
+ #
65
+ # gcloud = Google::Cloud.new
66
+ # pubsub = gcloud.pubsub
67
+ #
68
+ # topics = pubsub.topics
69
+ # if topics.next?
70
+ # next_topics = topics.next
71
+ # end
72
+ #
73
+ def next
74
+ return nil unless next?
75
+ ensure_service!
76
+ options = { token: token, max: @max }
77
+ grpc = @service.list_topics options
78
+ self.class.from_grpc grpc, @service, @max
79
+ end
80
+
81
+ ##
82
+ # Retrieves all topics by repeatedly loading {#next} until {#next?}
83
+ # returns `false`. Calls the given block once for each topic, which is
84
+ # passed as the parameter.
85
+ #
86
+ # An Enumerator is returned if no block is given.
87
+ #
88
+ # This method may make several API calls until all topics are
89
+ # retrieved. Be sure to use as narrow a search criteria as possible.
90
+ # Please use with caution.
91
+ #
92
+ # @param [Integer] request_limit The upper limit of API requests to
93
+ # make to load all topics. Default is no limit.
94
+ # @yield [topic] The block for accessing each topic.
95
+ # @yieldparam [Topic] topic The topic object.
96
+ #
97
+ # @return [Enumerator]
98
+ #
99
+ # @example Iterating each topic by passing a block:
100
+ # require "google/cloud"
101
+ #
102
+ # gcloud = Google::Cloud.new
103
+ # pubsub = gcloud.pubsub
104
+ #
105
+ # topics = pubsub.topics
106
+ # topics.all do |topic|
107
+ # puts topic.name
108
+ # end
109
+ #
110
+ # @example Using the enumerator by not passing a block:
111
+ # require "google/cloud"
112
+ #
113
+ # gcloud = Google::Cloud.new
114
+ # pubsub = gcloud.pubsub
115
+ #
116
+ # topics = pubsub.topics
117
+ # all_names = topics.all.map do |topic|
118
+ # topic.name
119
+ # end
120
+ #
121
+ # @example Limit the number of API calls made:
122
+ # require "google/cloud"
123
+ #
124
+ # gcloud = Google::Cloud.new
125
+ # pubsub = gcloud.pubsub
126
+ #
127
+ # topics = pubsub.topics
128
+ # topics.all(request_limit: 10) do |topic|
129
+ # puts topic.name
130
+ # end
131
+ #
132
+ def all request_limit: nil
133
+ request_limit = request_limit.to_i if request_limit
134
+ unless block_given?
135
+ return enum_for(:all, request_limit: request_limit)
136
+ end
137
+ results = self
138
+ loop do
139
+ results.each { |r| yield r }
140
+ if request_limit
141
+ request_limit -= 1
142
+ break if request_limit < 0
143
+ end
144
+ break unless results.next?
145
+ results = results.next
146
+ end
147
+ end
148
+
149
+ ##
150
+ # @private New Topic::List from a
151
+ # Google::Pubsub::V1::ListTopicsResponse object.
152
+ def self.from_grpc grpc_list, service, max = nil
153
+ topics = new(Array(grpc_list.topics).map do |grpc|
154
+ Topic.from_grpc grpc, service
155
+ end)
156
+ token = grpc_list.next_page_token
157
+ token = nil if token == ""
158
+ topics.instance_variable_set "@token", token
159
+ topics.instance_variable_set "@service", service
160
+ topics.instance_variable_set "@max", max
161
+ topics
162
+ end
163
+
164
+ protected
165
+
166
+ ##
167
+ # @private Raise an error unless an active connection to the service
168
+ # is available.
169
+ def ensure_service!
170
+ fail "Must have active connection to service" unless @service
171
+ end
172
+ end
173
+ end
174
+ end
175
+ end
176
+ end
@@ -0,0 +1,87 @@
1
+ # Copyright 2015 Google Inc. All rights reserved.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+
16
+ module Google
17
+ module Cloud
18
+ module Pubsub
19
+ class Topic
20
+ ##
21
+ # Topic Publisher object used to publish multiple messages at once.
22
+ #
23
+ # @example
24
+ # require "google/cloud"
25
+ #
26
+ # gcloud = Google::Cloud.new
27
+ # pubsub = gcloud.pubsub
28
+ #
29
+ # topic = pubsub.topic "my-topic"
30
+ # msgs = topic.publish do |t|
31
+ # t.publish "new-message-1", foo: :bar
32
+ # t.publish "new-message-2", foo: :baz
33
+ # t.publish "new-message-3", foo: :bif
34
+ # end
35
+ class Publisher
36
+ ##
37
+ # @private The messages to publish
38
+ attr_reader :messages
39
+
40
+ ##
41
+ # @private Create a new instance of the object.
42
+ def initialize data = nil, attributes = {}
43
+ @messages = []
44
+ @mode = :batch
45
+ return if data.nil?
46
+ @mode = :single
47
+ publish data, attributes
48
+ end
49
+
50
+ ##
51
+ # Add multiple messages to the topic.
52
+ # All messages added will be published at once.
53
+ # See {Google::Cloud::Pubsub::Topic#publish}
54
+ def publish data, attributes = {}
55
+ # Convert IO-ish objects to strings
56
+ if data.respond_to?(:read) && data.respond_to?(:rewind)
57
+ data.rewind
58
+ data = data.read
59
+ end
60
+ # Convert data to encoded byte array to match the protobuf defn
61
+ data = String(data).force_encoding("ASCII-8BIT")
62
+ # Convert attributes to strings to match the protobuf definition
63
+ attributes = Hash[attributes.map { |k, v| [String(k), String(v)] }]
64
+ @messages << [data, attributes]
65
+ end
66
+
67
+ ##
68
+ # @private Create Message objects with message ids.
69
+ def to_gcloud_messages message_ids
70
+ msgs = @messages.zip(Array(message_ids)).map do |arr, id|
71
+ Message.from_grpc(
72
+ Google::Pubsub::V1::PubsubMessage.new(
73
+ data: arr[0], attributes: arr[1], message_id: id))
74
+ end
75
+ # Return just one Message if a single publish,
76
+ # otherwise return the array of Messages.
77
+ if @mode == :single && msgs.count <= 1
78
+ msgs.first
79
+ else
80
+ msgs
81
+ end
82
+ end
83
+ end
84
+ end
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,22 @@
1
+ # Copyright 2016 Google Inc. All rights reserved.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+
16
+ module Google
17
+ module Cloud
18
+ module Pubsub
19
+ VERSION = "0.20.0"
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,33 @@
1
+ # Generated by the protocol buffer compiler. DO NOT EDIT!
2
+ # source: google/iam/v1/iam_policy.proto
3
+
4
+ require 'google/protobuf'
5
+
6
+ require 'google/iam/v1/policy'
7
+ Google::Protobuf::DescriptorPool.generated_pool.build do
8
+ add_message "google.iam.v1.SetIamPolicyRequest" do
9
+ optional :resource, :string, 1
10
+ optional :policy, :message, 2, "google.iam.v1.Policy"
11
+ end
12
+ add_message "google.iam.v1.GetIamPolicyRequest" do
13
+ optional :resource, :string, 1
14
+ end
15
+ add_message "google.iam.v1.TestIamPermissionsRequest" do
16
+ optional :resource, :string, 1
17
+ repeated :permissions, :string, 2
18
+ end
19
+ add_message "google.iam.v1.TestIamPermissionsResponse" do
20
+ repeated :permissions, :string, 1
21
+ end
22
+ end
23
+
24
+ module Google
25
+ module Iam
26
+ module V1
27
+ SetIamPolicyRequest = Google::Protobuf::DescriptorPool.generated_pool.lookup("google.iam.v1.SetIamPolicyRequest").msgclass
28
+ GetIamPolicyRequest = Google::Protobuf::DescriptorPool.generated_pool.lookup("google.iam.v1.GetIamPolicyRequest").msgclass
29
+ TestIamPermissionsRequest = Google::Protobuf::DescriptorPool.generated_pool.lookup("google.iam.v1.TestIamPermissionsRequest").msgclass
30
+ TestIamPermissionsResponse = Google::Protobuf::DescriptorPool.generated_pool.lookup("google.iam.v1.TestIamPermissionsResponse").msgclass
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,30 @@
1
+ # Generated by the protocol buffer compiler. DO NOT EDIT!
2
+ # Source: google/iam/v1/iam_policy.proto for package 'google.iam.v1'
3
+
4
+ require 'grpc'
5
+ require 'google/iam/v1/iam_policy'
6
+
7
+ module Google
8
+ module Iam
9
+ module V1
10
+ module IAMPolicy
11
+
12
+ # TODO: add proto service documentation here
13
+ class Service
14
+
15
+ include GRPC::GenericService
16
+
17
+ self.marshal_class_method = :encode
18
+ self.unmarshal_class_method = :decode
19
+ self.service_name = 'google.iam.v1.IAMPolicy'
20
+
21
+ rpc :SetIamPolicy, SetIamPolicyRequest, Policy
22
+ rpc :GetIamPolicy, GetIamPolicyRequest, Policy
23
+ rpc :TestIamPermissions, TestIamPermissionsRequest, TestIamPermissionsResponse
24
+ end
25
+
26
+ Stub = Service.rpc_stub_class
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,25 @@
1
+ # Generated by the protocol buffer compiler. DO NOT EDIT!
2
+ # source: google/iam/v1/policy.proto
3
+
4
+ require 'google/protobuf'
5
+
6
+ Google::Protobuf::DescriptorPool.generated_pool.build do
7
+ add_message "google.iam.v1.Policy" do
8
+ optional :version, :int32, 1
9
+ repeated :bindings, :message, 4, "google.iam.v1.Binding"
10
+ optional :etag, :bytes, 3
11
+ end
12
+ add_message "google.iam.v1.Binding" do
13
+ optional :role, :string, 1
14
+ repeated :members, :string, 2
15
+ end
16
+ end
17
+
18
+ module Google
19
+ module Iam
20
+ module V1
21
+ Policy = Google::Protobuf::DescriptorPool.generated_pool.lookup("google.iam.v1.Policy").msgclass
22
+ Binding = Google::Protobuf::DescriptorPool.generated_pool.lookup("google.iam.v1.Binding").msgclass
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,129 @@
1
+ # Generated by the protocol buffer compiler. DO NOT EDIT!
2
+ # source: google/pubsub/v1/pubsub.proto
3
+
4
+ require 'google/protobuf'
5
+
6
+ require 'google/api/annotations_pb'
7
+ require 'google/protobuf/empty_pb'
8
+ require 'google/protobuf/timestamp_pb'
9
+ Google::Protobuf::DescriptorPool.generated_pool.build do
10
+ add_message "google.pubsub.v1.Topic" do
11
+ optional :name, :string, 1
12
+ end
13
+ add_message "google.pubsub.v1.PubsubMessage" do
14
+ optional :data, :bytes, 1
15
+ map :attributes, :string, :string, 2
16
+ optional :message_id, :string, 3
17
+ optional :publish_time, :message, 4, "google.protobuf.Timestamp"
18
+ end
19
+ add_message "google.pubsub.v1.GetTopicRequest" do
20
+ optional :topic, :string, 1
21
+ end
22
+ add_message "google.pubsub.v1.PublishRequest" do
23
+ optional :topic, :string, 1
24
+ repeated :messages, :message, 2, "google.pubsub.v1.PubsubMessage"
25
+ end
26
+ add_message "google.pubsub.v1.PublishResponse" do
27
+ repeated :message_ids, :string, 1
28
+ end
29
+ add_message "google.pubsub.v1.ListTopicsRequest" do
30
+ optional :project, :string, 1
31
+ optional :page_size, :int32, 2
32
+ optional :page_token, :string, 3
33
+ end
34
+ add_message "google.pubsub.v1.ListTopicsResponse" do
35
+ repeated :topics, :message, 1, "google.pubsub.v1.Topic"
36
+ optional :next_page_token, :string, 2
37
+ end
38
+ add_message "google.pubsub.v1.ListTopicSubscriptionsRequest" do
39
+ optional :topic, :string, 1
40
+ optional :page_size, :int32, 2
41
+ optional :page_token, :string, 3
42
+ end
43
+ add_message "google.pubsub.v1.ListTopicSubscriptionsResponse" do
44
+ repeated :subscriptions, :string, 1
45
+ optional :next_page_token, :string, 2
46
+ end
47
+ add_message "google.pubsub.v1.DeleteTopicRequest" do
48
+ optional :topic, :string, 1
49
+ end
50
+ add_message "google.pubsub.v1.Subscription" do
51
+ optional :name, :string, 1
52
+ optional :topic, :string, 2
53
+ optional :push_config, :message, 4, "google.pubsub.v1.PushConfig"
54
+ optional :ack_deadline_seconds, :int32, 5
55
+ end
56
+ add_message "google.pubsub.v1.PushConfig" do
57
+ optional :push_endpoint, :string, 1
58
+ map :attributes, :string, :string, 2
59
+ end
60
+ add_message "google.pubsub.v1.ReceivedMessage" do
61
+ optional :ack_id, :string, 1
62
+ optional :message, :message, 2, "google.pubsub.v1.PubsubMessage"
63
+ end
64
+ add_message "google.pubsub.v1.GetSubscriptionRequest" do
65
+ optional :subscription, :string, 1
66
+ end
67
+ add_message "google.pubsub.v1.ListSubscriptionsRequest" do
68
+ optional :project, :string, 1
69
+ optional :page_size, :int32, 2
70
+ optional :page_token, :string, 3
71
+ end
72
+ add_message "google.pubsub.v1.ListSubscriptionsResponse" do
73
+ repeated :subscriptions, :message, 1, "google.pubsub.v1.Subscription"
74
+ optional :next_page_token, :string, 2
75
+ end
76
+ add_message "google.pubsub.v1.DeleteSubscriptionRequest" do
77
+ optional :subscription, :string, 1
78
+ end
79
+ add_message "google.pubsub.v1.ModifyPushConfigRequest" do
80
+ optional :subscription, :string, 1
81
+ optional :push_config, :message, 2, "google.pubsub.v1.PushConfig"
82
+ end
83
+ add_message "google.pubsub.v1.PullRequest" do
84
+ optional :subscription, :string, 1
85
+ optional :return_immediately, :bool, 2
86
+ optional :max_messages, :int32, 3
87
+ end
88
+ add_message "google.pubsub.v1.PullResponse" do
89
+ repeated :received_messages, :message, 1, "google.pubsub.v1.ReceivedMessage"
90
+ end
91
+ add_message "google.pubsub.v1.ModifyAckDeadlineRequest" do
92
+ optional :subscription, :string, 1
93
+ repeated :ack_ids, :string, 4
94
+ optional :ack_deadline_seconds, :int32, 3
95
+ end
96
+ add_message "google.pubsub.v1.AcknowledgeRequest" do
97
+ optional :subscription, :string, 1
98
+ repeated :ack_ids, :string, 2
99
+ end
100
+ end
101
+
102
+ module Google
103
+ module Pubsub
104
+ module V1
105
+ Topic = Google::Protobuf::DescriptorPool.generated_pool.lookup("google.pubsub.v1.Topic").msgclass
106
+ PubsubMessage = Google::Protobuf::DescriptorPool.generated_pool.lookup("google.pubsub.v1.PubsubMessage").msgclass
107
+ GetTopicRequest = Google::Protobuf::DescriptorPool.generated_pool.lookup("google.pubsub.v1.GetTopicRequest").msgclass
108
+ PublishRequest = Google::Protobuf::DescriptorPool.generated_pool.lookup("google.pubsub.v1.PublishRequest").msgclass
109
+ PublishResponse = Google::Protobuf::DescriptorPool.generated_pool.lookup("google.pubsub.v1.PublishResponse").msgclass
110
+ ListTopicsRequest = Google::Protobuf::DescriptorPool.generated_pool.lookup("google.pubsub.v1.ListTopicsRequest").msgclass
111
+ ListTopicsResponse = Google::Protobuf::DescriptorPool.generated_pool.lookup("google.pubsub.v1.ListTopicsResponse").msgclass
112
+ ListTopicSubscriptionsRequest = Google::Protobuf::DescriptorPool.generated_pool.lookup("google.pubsub.v1.ListTopicSubscriptionsRequest").msgclass
113
+ ListTopicSubscriptionsResponse = Google::Protobuf::DescriptorPool.generated_pool.lookup("google.pubsub.v1.ListTopicSubscriptionsResponse").msgclass
114
+ DeleteTopicRequest = Google::Protobuf::DescriptorPool.generated_pool.lookup("google.pubsub.v1.DeleteTopicRequest").msgclass
115
+ Subscription = Google::Protobuf::DescriptorPool.generated_pool.lookup("google.pubsub.v1.Subscription").msgclass
116
+ PushConfig = Google::Protobuf::DescriptorPool.generated_pool.lookup("google.pubsub.v1.PushConfig").msgclass
117
+ ReceivedMessage = Google::Protobuf::DescriptorPool.generated_pool.lookup("google.pubsub.v1.ReceivedMessage").msgclass
118
+ GetSubscriptionRequest = Google::Protobuf::DescriptorPool.generated_pool.lookup("google.pubsub.v1.GetSubscriptionRequest").msgclass
119
+ ListSubscriptionsRequest = Google::Protobuf::DescriptorPool.generated_pool.lookup("google.pubsub.v1.ListSubscriptionsRequest").msgclass
120
+ ListSubscriptionsResponse = Google::Protobuf::DescriptorPool.generated_pool.lookup("google.pubsub.v1.ListSubscriptionsResponse").msgclass
121
+ DeleteSubscriptionRequest = Google::Protobuf::DescriptorPool.generated_pool.lookup("google.pubsub.v1.DeleteSubscriptionRequest").msgclass
122
+ ModifyPushConfigRequest = Google::Protobuf::DescriptorPool.generated_pool.lookup("google.pubsub.v1.ModifyPushConfigRequest").msgclass
123
+ PullRequest = Google::Protobuf::DescriptorPool.generated_pool.lookup("google.pubsub.v1.PullRequest").msgclass
124
+ PullResponse = Google::Protobuf::DescriptorPool.generated_pool.lookup("google.pubsub.v1.PullResponse").msgclass
125
+ ModifyAckDeadlineRequest = Google::Protobuf::DescriptorPool.generated_pool.lookup("google.pubsub.v1.ModifyAckDeadlineRequest").msgclass
126
+ AcknowledgeRequest = Google::Protobuf::DescriptorPool.generated_pool.lookup("google.pubsub.v1.AcknowledgeRequest").msgclass
127
+ end
128
+ end
129
+ end