gcloud 0.1.1 → 0.2.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.
@@ -33,23 +33,24 @@ module Gcloud
33
33
  # Google Datastore. Gcloud::Datastore::Entity objects are created,
34
34
  # read, updated, and deleted by Gcloud::Datastore::Dataset.
35
35
  #
36
- # require "gcloud/datastore"
36
+ # require "gcloud"
37
37
  #
38
- # dataset = Gcloud.datastore
38
+ # gcloud = Gcloud.new
39
+ # dataset = gcloud.datastore
39
40
  #
40
41
  # query = Gcloud::Datastore::Query.new.kind("Task").
41
42
  # where("completed", "=", true)
42
43
  #
43
44
  # tasks = dataset.run query
44
45
  #
45
- # See Gcloud.datastore
46
+ # See Gcloud#datastore
46
47
  class Dataset
47
48
  attr_accessor :connection #:nodoc:
48
49
 
49
50
  ##
50
51
  # Creates a new Dataset instance.
51
52
  #
52
- # See Gcloud.datastore
53
+ # See Gcloud#datastore
53
54
  def initialize project, credentials #:nodoc:
54
55
  @connection = Connection.new project, credentials
55
56
  end
@@ -59,11 +60,12 @@ module Gcloud
59
60
  #
60
61
  # === Example
61
62
  #
62
- # require "gcloud/datastore"
63
+ # require "gcloud"
63
64
  #
64
- # dataset = Gcloud.datastore "my-todo-project",
65
- # "/path/to/keyfile.json"
65
+ # gcloud = Gcloud.new "my-todo-project",
66
+ # "/path/to/keyfile.json"
66
67
  #
68
+ # dataset = gcloud.datastore
67
69
  # dataset.project #=> "my-todo-project"
68
70
  #
69
71
  def project
@@ -139,7 +141,7 @@ module Gcloud
139
141
  #
140
142
  # +key_or_kind+::
141
143
  # A Key object or +kind+ string value. (+Key+ or +String+)
142
- # +key_or_kind+::
144
+ # +id_or_name+::
143
145
  # The Key's +id+ or +name+ value if a +kind+ was provided in the first
144
146
  # parameter. (+Integer+ or +String+ or +nil+)
145
147
  #
@@ -179,7 +181,8 @@ module Gcloud
179
181
  #
180
182
  # === Example
181
183
  #
182
- # dataset = Gcloud.datastore
184
+ # gcloud = Gcloud.new
185
+ # dataset = gcloud.datastore
183
186
  # key1 = Gcloud::Datastore::Key.new "Task", 123456
184
187
  # key2 = Gcloud::Datastore::Key.new "Task", 987654
185
188
  # tasks = dataset.find_all key1, key2
@@ -207,7 +210,8 @@ module Gcloud
207
210
  #
208
211
  # === Example
209
212
  #
210
- # dataset = Gcloud.datastore
213
+ # gcloud = Gcloud.new
214
+ # dataset = gcloud.datastore
211
215
  # dataset.delete entity1, entity2
212
216
  #
213
217
  def delete *entities_or_keys
@@ -255,9 +259,10 @@ module Gcloud
255
259
  #
256
260
  # Runs the given block in a database transaction:
257
261
  #
258
- # require "gcloud/datastore"
262
+ # require "gcloud"
259
263
  #
260
- # dataset = Gcloud.datastore
264
+ # gcloud = Gcloud.new
265
+ # dataset = gcloud.datastore
261
266
  #
262
267
  # key = Gcloud::Datastore::Key.new "User", "heidi"
263
268
  #
@@ -274,9 +279,10 @@ module Gcloud
274
279
  #
275
280
  # Alternatively, if no block is given a Transaction object is returned:
276
281
  #
277
- # require "gcloud/datastore"
282
+ # require "gcloud"
278
283
  #
279
- # dataset = Gcloud.datastore
284
+ # gcloud = Gcloud.new
285
+ # dataset = gcloud.datastore
280
286
  #
281
287
  # key = Gcloud::Datastore::Key.new "User", "heidi"
282
288
  #
@@ -58,18 +58,20 @@ module Gcloud
58
58
  #
59
59
  # Properties can be retrieved with a string name:
60
60
  #
61
- # require "gcloud/datastore"
61
+ # require "gcloud"
62
62
  #
63
- # dataset = Gcloud.datastore
64
- # user = datastore.find "User", "heidi"
63
+ # gcloud = Gcloud.new
64
+ # dataset = gcloud.datastore
65
+ # user = dataset.find "User", "heidi"
65
66
  # user["name"] #=> "Heidi Henderson"
66
67
  #
67
68
  # Or with a symbol name:
68
69
  #
69
- # require "gcloud/datastore"
70
+ # require "gcloud"
70
71
  #
71
- # dataset = Gcloud.datastore
72
- # user = datastore.find "User", "heidi"
72
+ # gcloud = Gcloud.new
73
+ # dataset = gcloud.datastore
74
+ # user = dataset.find "User", "heidi"
73
75
  # user[:name] #=> "Heidi Henderson"
74
76
  #
75
77
  def [] prop_name
@@ -90,18 +92,20 @@ module Gcloud
90
92
  #
91
93
  # Properties can be set with a string name:
92
94
  #
93
- # require "gcloud/datastore"
95
+ # require "gcloud"
94
96
  #
95
- # dataset = Gcloud.datastore
96
- # user = datastore.find "User", "heidi"
97
+ # gcloud = Gcloud.new
98
+ # dataset = gcloud.datastore
99
+ # user = dataset.find "User", "heidi"
97
100
  # user["name"] = "Heidi H. Henderson"
98
101
  #
99
102
  # Or with a symbol name:
100
103
  #
101
- # require "gcloud/datastore"
104
+ # require "gcloud"
102
105
  #
103
- # dataset = Gcloud.datastore
104
- # user = datastore.find "User", "heidi"
106
+ # gcloud = Gcloud.new
107
+ # dataset = gcloud.datastore
108
+ # user = dataset.find "User", "heidi"
105
109
  # user[:name] = "Heidi H. Henderson"
106
110
  #
107
111
  def []= prop_name, prop_value
@@ -149,9 +153,10 @@ module Gcloud
149
153
  #
150
154
  # The Key can be set before the entity is saved.
151
155
  #
152
- # require "glcoud/datastore"
156
+ # require "gcloud"
153
157
  #
154
- # dataset = Gcloud.datastore
158
+ # gcloud = Gcloud.new
159
+ # dataset = gcloud.datastore
155
160
  # entity = Gcloud::Datastore::Entity.new
156
161
  # entity.key = Gcloud::Datastore::Key.new "User"
157
162
  # dataset.save entity
@@ -159,9 +164,10 @@ module Gcloud
159
164
  # Once the entity is saved, the key is frozen and immutable.
160
165
  # Trying to set a key when immutable will raise a +RuntimeError+.
161
166
  #
162
- # require "gcloud/datastore"
167
+ # require "gcloud"
163
168
  #
164
- # dataset = Gcloud.datastore
169
+ # gcloud = Gcloud.new
170
+ # dataset = gcloud.datastore
165
171
  # entity = dataset.find "User", "heidi"
166
172
  # entity.persisted? #=> true
167
173
  # entity.key = Gcloud::Datastore::Key.new "User" #=> RuntimeError
@@ -178,9 +184,10 @@ module Gcloud
178
184
  #
179
185
  # === Example
180
186
  #
181
- # require "gcloud/datastore"
187
+ # require "gcloud"
182
188
  #
183
- # dataset = Gcloud.datastore
189
+ # gcloud = Gcloud.new
190
+ # dataset = gcloud.datastore
184
191
  #
185
192
  # new_entity = Gcloud::Datastore::Entity.new
186
193
  # new_entity.persisted? #=> false
@@ -51,11 +51,12 @@ module Gcloud
51
51
  #
52
52
  # === Example
53
53
  #
54
- # require "glcoud/datastore"
54
+ # require "gcloud"
55
55
  #
56
- # dataset = Gcloud.datastore "my-todo-project",
57
- # "/path/to/keyfile.json"
56
+ # gcloud = Gcloud.new "my-todo-project",
57
+ # "/path/to/keyfile.json"
58
58
  #
59
+ # dataset = gcloud.datastore
59
60
  # entity = dataset.find "User", "heidi"
60
61
  # entity.key.dataset_id #=> "my-todo-project"
61
62
  #
@@ -70,11 +71,12 @@ module Gcloud
70
71
  #
71
72
  # === Example
72
73
  #
73
- # require "glcoud/datastore"
74
+ # require "gcloud"
74
75
  #
75
- # dataset = Gcloud.datastore "my-todo-project",
76
- # "/path/to/keyfile.json"
76
+ # gcloud = Gcloud.new "my-todo-project",
77
+ # "/path/to/keyfile.json"
77
78
  #
79
+ # dataset = gcloud.datastore
78
80
  # entity = dataset.find "User", "heidi"
79
81
  # entity.key.namespace #=> "ns~todo-project"
80
82
  #
@@ -188,9 +190,10 @@ module Gcloud
188
190
  #
189
191
  # === Example
190
192
  #
191
- # require "glcoud/datastore"
193
+ # require "gcloud"
192
194
  #
193
- # dataset = Gcloud.datastore
195
+ # gcloud = Gcloud.new
196
+ # dataset = gcloud.datastore
194
197
  #
195
198
  # user = dataset.find "User", "heidi"
196
199
  # query = Gcloud::Datastore::Query.new
@@ -0,0 +1,375 @@
1
+ #--
2
+ # Copyright 2015 Google Inc. All rights reserved.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+
16
+ require "gcloud"
17
+ require "gcloud/pubsub/project"
18
+
19
+ #--
20
+ # Google Cloud Pub/Sub
21
+ module Gcloud
22
+ ##
23
+ # Creates a new object for connecting to the Pub/Sub service.
24
+ # Each call creates a new connection.
25
+ #
26
+ # === Parameters
27
+ #
28
+ # +project+::
29
+ # Project identifier for the Pub/Sub service you are connecting to.
30
+ # (+String+)
31
+ # +keyfile+::
32
+ # Keyfile downloaded from Google Cloud. If file path the file must be
33
+ # readable. (+String+ or +Hash+)
34
+ #
35
+ # === Returns
36
+ #
37
+ # Gcloud::Pubsub::Project
38
+ #
39
+ # === Example
40
+ #
41
+ # require "gcloud/pubsub"
42
+ #
43
+ # pubsub = Gcloud.pubsub
44
+ #
45
+ # topic = pubsub.topic "my-topic"
46
+ # topic.publish "task completed"
47
+ #
48
+ def self.pubsub project = nil, keyfile = nil
49
+ project ||= Gcloud::Pubsub::Project.default_project
50
+ if keyfile.nil?
51
+ credentials = Gcloud::Pubsub::Credentials.default
52
+ else
53
+ credentials = Gcloud::Pubsub::Credentials.new keyfile
54
+ end
55
+ Gcloud::Pubsub::Project.new project, credentials
56
+ end
57
+
58
+ ##
59
+ # = Google Cloud Pub/Sub
60
+ #
61
+ # Google Cloud Pub/Sub is designed to provide reliable, many-to-many,
62
+ # asynchronous messaging between applications. Publisher applications can
63
+ # send messages to a "topic" and other applications can subscribe to that
64
+ # topic to receive the messages. By decoupling senders and receivers, Google
65
+ # Cloud Pub/Sub allows developers to communicate between independently written
66
+ # applications.
67
+ #
68
+ # Gcloud's goal is to provide a API that is familiar and comfortable to
69
+ # Rubyists. Authentication is handled by Gcloud#pubsub. You can provide the
70
+ # project and credential information to connect to the Pub/Sub service, or if
71
+ # you are running on Google Compute Engine this configuration is taken care
72
+ # of for you.
73
+ #
74
+ # require "gcloud"
75
+ #
76
+ # gcloud = Gcloud.new
77
+ # pubsub = gcloud.pubsub
78
+ #
79
+ # topic = pubsub.topic "my-topic"
80
+ # topic.publish "task completed"
81
+ #
82
+ # To learn more about Pub/Sub, read the {Google Cloud Pub/Sub Overview
83
+ # }[https://cloud.google.com/pubsub/overview].
84
+ #
85
+ # == Retrieving Topics
86
+ #
87
+ # A Topic is a named resource to which messages are sent by publishers.
88
+ # A Topic is found by its name. (See Project#topic)
89
+ #
90
+ # require "gcloud"
91
+ #
92
+ # gcloud = Gcloud.new
93
+ # pubsub = gcloud.pubsub
94
+ # topic = pubsub.topic "my-topic"
95
+ #
96
+ # == Creating a Topic
97
+ #
98
+ # A Topic is created from a Project. (See Project#create_topic)
99
+ #
100
+ # require "gcloud"
101
+ #
102
+ # gcloud = Gcloud.new
103
+ # pubsub = gcloud.pubsub
104
+ # topic = pubsub.create_topic "my-topic"
105
+ #
106
+ # == Publishing Messages
107
+ #
108
+ # Messages are published to a topic. (See Topic#publish)
109
+ #
110
+ # require "gcloud"
111
+ #
112
+ # gcloud = Gcloud.new
113
+ # pubsub = gcloud.pubsub
114
+ #
115
+ # topic = pubsub.topic "my-topic"
116
+ # msg = topic.publish "new-message"
117
+ #
118
+ # Messages can also be published with attributes:
119
+ #
120
+ # require "gcloud"
121
+ #
122
+ # gcloud = Gcloud.new
123
+ # pubsub = gcloud.pubsub
124
+ #
125
+ # topic = pubsub.topic "my-topic"
126
+ # msg = topic.publish "new-message",
127
+ # foo: :bar,
128
+ # this: :that
129
+ #
130
+ # Multiple messages can be published at the same time by passing a block:
131
+ #
132
+ # require "gcloud"
133
+ #
134
+ # gcloud = Gcloud.new
135
+ # pubsub = gcloud.pubsub
136
+ #
137
+ # topic = pubsub.topic "my-topic"
138
+ # msgs = topic.publish do |batch|
139
+ # batch.publish "new-message-1", foo: :bar
140
+ # batch.publish "new-message-2", foo: :baz
141
+ # batch.publish "new-message-3", foo: :bif
142
+ # end
143
+ #
144
+ # == Retrieving Subscriptions
145
+ #
146
+ # A Subscription is a named resource representing the stream of messages from
147
+ # a single, specific Topic, to be delivered to the subscribing application.
148
+ # A Subscription is found by its name. (See Topic#subscription)
149
+ #
150
+ # require "gcloud"
151
+ #
152
+ # gcloud = Gcloud.new
153
+ # pubsub = gcloud.pubsub
154
+ #
155
+ # topic = pubsub.topic "my-topic"
156
+ # subscription = topic.subscription "my-topic-subscription"
157
+ # puts subscription.name
158
+ #
159
+ # == Creating a Subscription
160
+ #
161
+ # A Subscription is created from a Topic. (See Topic#subscribe)
162
+ #
163
+ # require "gcloud"
164
+ #
165
+ # gcloud = Gcloud.new
166
+ # pubsub = gcloud.pubsub
167
+ #
168
+ # topic = pubsub.topic "my-topic"
169
+ # sub = topic.subscribe "my-topic-sub"
170
+ # puts sub.name # => "my-topic-sub"
171
+ #
172
+ # The subscription can be created that specifies the number of seconds to
173
+ # wait to be acknowledged as well as an endpoint URL to push the messages to:
174
+ #
175
+ # require "gcloud"
176
+ #
177
+ # gcloud = Gcloud.new
178
+ # pubsub = gcloud.pubsub
179
+ #
180
+ # topic = pubsub.topic "my-topic"
181
+ # sub = topic.subscribe "my-topic-sub",
182
+ # deadline: 120,
183
+ # endpoint: "https://example.com/push"
184
+ #
185
+ # == Working Across Projects
186
+ #
187
+ # All calls to the Pub/Sub service use the same project and credentials
188
+ # provided to the Gcloud#pubsub method. However, it is common to reference
189
+ # topics or subscriptions in other projects, which can be achieved by using
190
+ # the +project+ option. The main credentials must have permissions to the
191
+ # topics and subscriptions in other projects.
192
+ #
193
+ # require "gcloud"
194
+ #
195
+ # gcloud = Gcloud.new # my-project-id
196
+ # pubsub = gcloud.pubsub
197
+ #
198
+ # # Get a topic in the current project
199
+ # my_topic = pubsub.topic "my-topic"
200
+ # my_topic.name #=> "projects/my-project-id/topics/my-topic"
201
+ # # Get a topic in another project
202
+ # other_topic = pubsub.topic "other-topic", project: "other-project-id"
203
+ # other_topic.name #=> "projects/other-project-id/topics/other-topic"
204
+ #
205
+ # It is possible to create a subscription in the current project that pulls
206
+ # from a topic in another project:
207
+ #
208
+ # require "gcloud"
209
+ #
210
+ # gcloud = Gcloud.new # my-project-id
211
+ # pubsub = gcloud.pubsub
212
+ #
213
+ # # Get a topic in another project
214
+ # topic = pubsub.topic "other-topic", project: "other-project-id"
215
+ # # Create a subscription in the current project that pulls from
216
+ # # the topic in another project
217
+ # sub = topic.subscribe "my-sub"
218
+ # sub.name #=> "projects/my-project-id/subscriptions/my-sub"
219
+ # sub.topic.name #=> "projects/other-project-id/topics/other-topic"
220
+ #
221
+ # == Pulling Messages
222
+ #
223
+ # Messages are pulled from a Subscription. (See Subscription#pull)
224
+ #
225
+ # require "gcloud"
226
+ #
227
+ # gcloud = Gcloud.new
228
+ # pubsub = gcloud.pubsub
229
+ #
230
+ # sub = pubsub.subscription "my-topic-sub"
231
+ # msgs = sub.pull
232
+ #
233
+ # A maximum number of messages returned can also be specified:
234
+ #
235
+ # require "gcloud"
236
+ #
237
+ # gcloud = Gcloud.new
238
+ # pubsub = gcloud.pubsub
239
+ #
240
+ # sub = pubsub.subscription "my-topic-sub", max: 10
241
+ # msgs = sub.pull
242
+ #
243
+ # The request for messages can also block until messages are available.
244
+ # (See Subscription#wait_for_messages)
245
+ #
246
+ # require "gcloud"
247
+ #
248
+ # gcloud = Gcloud.new
249
+ # pubsub = gcloud.pubsub
250
+ #
251
+ # sub = pubsub.subscription "my-topic-sub"
252
+ # msgs = sub.wait_for_messages
253
+ #
254
+ # == Acknowledging a Message
255
+ #
256
+ # Messages that are received can be acknowledged in Pub/Sub, marking the
257
+ # message to be removed so it cannot be pulled again.
258
+ #
259
+ # A Message that can be acknowledged is called a ReceivedMessage.
260
+ # ReceivedMessages can be acknowledged one at a time:
261
+ # (See ReceivedMessage#acknowledge!)
262
+ #
263
+ # require "gcloud"
264
+ #
265
+ # gcloud = Gcloud.new
266
+ # pubsub = gcloud.pubsub
267
+ #
268
+ # sub = pubsub.subscription "my-topic-sub"
269
+ # sub.pull.each { |msg| msg.acknowledge! }
270
+ #
271
+ # Or, multiple messages can be acknowledged in a single API call:
272
+ # (See Subscription#acknowledge)
273
+ #
274
+ # require "gcloud"
275
+ #
276
+ # gcloud = Gcloud.new
277
+ # pubsub = gcloud.pubsub
278
+ #
279
+ # sub = pubsub.subscription "my-topic-sub"
280
+ # received_messages = sub.pull
281
+ # sub.acknowledge received_messages
282
+ #
283
+ # == Modifying a Deadline
284
+ #
285
+ # A message must be acknowledged after it is pulled, or Pub/Sub will mark the
286
+ # message for redelivery. The message acknowledgement deadline can delayed if
287
+ # more time is needed. This will allow more time to process the message before
288
+ # the message is marked for redelivery. (See ReceivedMessage#delay!)
289
+ #
290
+ # require "gcloud"
291
+ #
292
+ # gcloud = Gcloud.new
293
+ # pubsub = gcloud.pubsub
294
+ #
295
+ # sub = pubsub.subscription "my-topic-sub"
296
+ # received_message = sub.pull.first
297
+ # if received_message
298
+ # puts received_message.message.data
299
+ # # Delay for 2 minutes
300
+ # received_message.delay! 120
301
+ # end
302
+ #
303
+ # The message can also be made available for immediate redelivery:
304
+ #
305
+ # require "gcloud"
306
+ #
307
+ # gcloud = Gcloud.new
308
+ # pubsub = gcloud.pubsub
309
+ #
310
+ # sub = pubsub.subscription "my-topic-sub"
311
+ # received_message = sub.pull.first
312
+ # if received_message
313
+ # puts received_message.message.data
314
+ # # Mark for redelivery by setting the deadline to now
315
+ # received_message.delay! 0
316
+ # end
317
+ #
318
+ # Multiple messages can be delayed or made available for immediate redelivery:
319
+ # (See Subscription#delay)
320
+ #
321
+ # require "gcloud"
322
+ #
323
+ # gcloud = Gcloud.new
324
+ # pubsub = gcloud.pubsub
325
+ #
326
+ # sub = pubsub.subscription "my-topic-sub"
327
+ # received_messages = sub.pull
328
+ # sub.delay 120, received_messages
329
+ #
330
+ # == Listening for Messages
331
+ #
332
+ # Long running workers are easy to create with +listen+, which runs an
333
+ # infinitely blocking loop to process messages as they are received. (See
334
+ # Subscription#listen)
335
+ #
336
+ # require "gcloud"
337
+ #
338
+ # gcloud = Gcloud.new
339
+ # pubsub = gcloud.pubsub
340
+ #
341
+ # sub = pubsub.subscription "my-topic-sub"
342
+ # sub.listen do |msg|
343
+ # # process msg
344
+ # end
345
+ #
346
+ # Messages are retrieved in batches for efficiency. The number of messages
347
+ # pulled per batch can be limited with the +max+ option:
348
+ #
349
+ # require "gcloud"
350
+ #
351
+ # gcloud = Gcloud.new
352
+ # pubsub = gcloud.pubsub
353
+ #
354
+ # sub = pubsub.subscription "my-topic-sub"
355
+ # sub.listen max: 20 do |msg|
356
+ # # process msg
357
+ # end
358
+ #
359
+ # When processing time and the acknowledgement deadline are a concern,
360
+ # messages can be automatically acknowledged as they are pulled with the
361
+ # +autoack+ option:
362
+ #
363
+ # require "gcloud"
364
+ #
365
+ # gcloud = Gcloud.new
366
+ # pubsub = gcloud.pubsub
367
+ #
368
+ # sub = pubsub.subscription "my-topic-sub"
369
+ # sub.listen autoack: true do |msg|
370
+ # # process msg
371
+ # end
372
+ #
373
+ module Pubsub
374
+ end
375
+ end