gcloud 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (67) hide show
  1. checksums.yaml +13 -5
  2. data/AUTHENTICATION.md +71 -0
  3. data/CHANGELOG.md +5 -0
  4. data/README.md +10 -15
  5. data/lib/gcloud.rb +30 -0
  6. data/lib/gcloud/backoff.rb +3 -0
  7. data/lib/gcloud/credentials.rb +47 -30
  8. data/lib/gcloud/datastore.rb +246 -15
  9. data/lib/gcloud/datastore/connection.rb +4 -2
  10. data/lib/gcloud/datastore/credentials.rb +3 -1
  11. data/lib/gcloud/datastore/dataset.rb +130 -25
  12. data/lib/gcloud/datastore/dataset/lookup_results.rb +1 -0
  13. data/lib/gcloud/datastore/dataset/query_results.rb +7 -5
  14. data/lib/gcloud/datastore/entity.rb +99 -17
  15. data/lib/gcloud/datastore/errors.rb +13 -2
  16. data/lib/gcloud/datastore/key.rb +133 -2
  17. data/lib/gcloud/datastore/properties.rb +6 -1
  18. data/lib/gcloud/datastore/proto.rb +2 -1
  19. data/lib/gcloud/datastore/query.rb +4 -4
  20. data/lib/gcloud/datastore/transaction.rb +3 -0
  21. data/lib/gcloud/storage.rb +280 -13
  22. data/lib/gcloud/storage/bucket.rb +248 -11
  23. data/lib/gcloud/storage/bucket/acl.rb +631 -4
  24. data/lib/gcloud/storage/bucket/list.rb +1 -0
  25. data/lib/gcloud/storage/connection.rb +1 -0
  26. data/lib/gcloud/storage/credentials.rb +3 -1
  27. data/lib/gcloud/storage/errors.rb +9 -1
  28. data/lib/gcloud/storage/file.rb +231 -6
  29. data/lib/gcloud/storage/file/acl.rb +365 -2
  30. data/lib/gcloud/storage/file/list.rb +1 -0
  31. data/lib/gcloud/storage/file/verifier.rb +1 -0
  32. data/lib/gcloud/storage/project.rb +119 -10
  33. data/lib/gcloud/version.rb +18 -3
  34. metadata +33 -80
  35. data/.gemtest +0 -0
  36. data/.rubocop.yml +0 -17
  37. data/Manifest.txt +0 -66
  38. data/Rakefile +0 -35
  39. data/gcloud.gemspec +0 -63
  40. data/rakelib/console.rake +0 -28
  41. data/rakelib/manifest.rake +0 -24
  42. data/rakelib/proto.rake +0 -17
  43. data/rakelib/rubocop.rake +0 -17
  44. data/rakelib/test.rake +0 -144
  45. data/test/gcloud/datastore/proto/test_cursor.rb +0 -36
  46. data/test/gcloud/datastore/proto/test_direction.rb +0 -60
  47. data/test/gcloud/datastore/proto/test_operator.rb +0 -76
  48. data/test/gcloud/datastore/proto/test_value.rb +0 -231
  49. data/test/gcloud/datastore/test_connection.rb +0 -93
  50. data/test/gcloud/datastore/test_credentials.rb +0 -38
  51. data/test/gcloud/datastore/test_dataset.rb +0 -413
  52. data/test/gcloud/datastore/test_entity.rb +0 -161
  53. data/test/gcloud/datastore/test_entity_exclude.rb +0 -225
  54. data/test/gcloud/datastore/test_key.rb +0 -189
  55. data/test/gcloud/datastore/test_query.rb +0 -271
  56. data/test/gcloud/datastore/test_transaction.rb +0 -121
  57. data/test/gcloud/storage/test_backoff.rb +0 -127
  58. data/test/gcloud/storage/test_bucket.rb +0 -270
  59. data/test/gcloud/storage/test_bucket_acl.rb +0 -253
  60. data/test/gcloud/storage/test_default_acl.rb +0 -256
  61. data/test/gcloud/storage/test_file.rb +0 -221
  62. data/test/gcloud/storage/test_file_acl.rb +0 -367
  63. data/test/gcloud/storage/test_project.rb +0 -180
  64. data/test/gcloud/storage/test_storage.rb +0 -29
  65. data/test/gcloud/storage/test_verifier.rb +0 -62
  66. data/test/gcloud/test_version.rb +0 -8
  67. data/test/helper.rb +0 -91
@@ -1,3 +1,4 @@
1
+ #--
1
2
  # Copyright 2014 Google Inc. All rights reserved.
2
3
  #
3
4
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -87,8 +88,9 @@ module Gcloud
87
88
  # Commit a transaction, optionally creating, deleting or modifying
88
89
  # some entities.
89
90
  def commit mutation, transaction = nil
90
- mode = transaction ? Proto::CommitRequest::Mode::TRANSACTIONAL :
91
- Proto::CommitRequest::Mode::NON_TRANSACTIONAL
91
+ mode = Proto::CommitRequest::Mode::NON_TRANSACTIONAL
92
+ mode = Proto::CommitRequest::Mode::TRANSACTIONAL if transaction
93
+
92
94
  commit = Proto::CommitRequest.new.tap do |c|
93
95
  c.mutation = mutation
94
96
  c.mode = mode
@@ -1,3 +1,4 @@
1
+ #--
1
2
  # Copyright 2014 Google Inc. All rights reserved.
2
3
  #
3
4
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -25,7 +26,8 @@ module Gcloud
25
26
  class Credentials < Gcloud::Credentials #:nodoc:
26
27
  SCOPE = ["https://www.googleapis.com/auth/datastore",
27
28
  "https://www.googleapis.com/auth/userinfo.email"]
28
- ENV_VARS = ["DATASTORE_KEYFILE"]
29
+ PATH_ENV_VARS = %w(DATASTORE_KEYFILE GOOGLE_CLOUD_KEYFILE)
30
+ JSON_ENV_VARS = %w(DATASTORE_KEYFILE_JSON GOOGLE_CLOUD_KEYFILE_JSON)
29
31
 
30
32
  ##
31
33
  # Sign Oauth2 API calls.
@@ -1,3 +1,4 @@
1
+ #--
1
2
  # Copyright 2014 Google Inc. All rights reserved.
2
3
  #
3
4
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -23,6 +24,8 @@ require "gcloud/datastore/dataset/query_results"
23
24
  module Gcloud
24
25
  module Datastore
25
26
  ##
27
+ # = Dataset
28
+ #
26
29
  # Dataset is the data saved in a project's Datastore.
27
30
  # Dataset is analogous to a database in relational database world.
28
31
  #
@@ -30,8 +33,9 @@ module Gcloud
30
33
  # Google Datastore. Gcloud::Datastore::Entity objects are created,
31
34
  # read, updated, and deleted by Gcloud::Datastore::Dataset.
32
35
  #
33
- # dataset = Gcloud.datastore "my-todo-project",
34
- # "/path/to/keyfile.json"
36
+ # require "gcloud/datastore"
37
+ #
38
+ # dataset = Gcloud.datastore
35
39
  #
36
40
  # query = Gcloud::Datastore::Query.new.kind("Task").
37
41
  # where("completed", "=", true)
@@ -51,17 +55,46 @@ module Gcloud
51
55
  end
52
56
 
53
57
  ##
54
- # The project connected to.
58
+ # The Datastore project connected to.
59
+ #
60
+ # === Example
61
+ #
62
+ # require "gcloud/datastore"
63
+ #
64
+ # dataset = Gcloud.datastore "my-todo-project",
65
+ # "/path/to/keyfile.json"
66
+ #
67
+ # dataset.project #=> "my-todo-project"
68
+ #
55
69
  def project
56
70
  connection.dataset_id
57
71
  end
58
72
 
73
+ ##
74
+ # Default project.
75
+ def self.default_project #:nodoc:
76
+ ENV["DATASTORE_PROJECT"] || ENV["GOOGLE_CLOUD_PROJECT"]
77
+ end
78
+
59
79
  ##
60
80
  # Generate IDs for a Key before creating an entity.
61
81
  #
62
- # dataset = Gcloud.datastore
82
+ # === Parameters
83
+ #
84
+ # +incomplete_key+::
85
+ # A Key without +id+ or +name+ set. (+Key+)
86
+ # +count+::
87
+ # The number of new key IDs to create. (+Integer+)
88
+ #
89
+ # === Returns
90
+ #
91
+ # Array of Gcloud::Datastore::Key
92
+ #
93
+ # === Example
94
+ #
63
95
  # empty_key = Gcloud::Datastore::Key.new "Task"
64
96
  # task_keys = dataset.allocate_ids empty_key, 5
97
+ #
65
98
  def allocate_ids incomplete_key, count = 1
66
99
  if incomplete_key.complete?
67
100
  fail Gcloud::Datastore::Error, "An incomplete key must be provided."
@@ -75,10 +108,22 @@ module Gcloud
75
108
  end
76
109
 
77
110
  ##
78
- # Persist entities to the Datastore.
111
+ # Persist one or more entities to the Datastore.
112
+ #
113
+ # === Parameters
114
+ #
115
+ # +entities+::
116
+ # One or more entity objects to be saved without +id+ or +name+ set.
117
+ # (+Entity+)
118
+ #
119
+ # === Returns
120
+ #
121
+ # Array of Gcloud::Datastore::Entity
122
+ #
123
+ # === Example
79
124
  #
80
- # dataset = Gcloud.datastore
81
125
  # dataset.save task1, task2
126
+ #
82
127
  def save *entities
83
128
  mutation = Proto.new_mutation
84
129
  save_entities_to_mutation entities, mutation
@@ -89,14 +134,30 @@ module Gcloud
89
134
 
90
135
  ##
91
136
  # Retrieve an entity by providing key information.
92
- # Either a Key object or kind and id/name can be provided.
93
137
  #
94
- # dataset = Gcloud.datastore
138
+ # === Parameters
139
+ #
140
+ # +key_or_kind+::
141
+ # A Key object or +kind+ string value. (+Key+ or +String+)
142
+ # +key_or_kind+::
143
+ # The Key's +id+ or +name+ value if a +kind+ was provided in the first
144
+ # parameter. (+Integer+ or +String+ or +nil+)
145
+ #
146
+ # === Returns
147
+ #
148
+ # Gcloud::Datastore::Entity or +nil+
149
+ #
150
+ # === Example
151
+ #
152
+ # Finding an entity with a key:
153
+ #
95
154
  # key = Gcloud::Datastore::Key.new "Task", 123456
96
155
  # task = dataset.find key
97
156
  #
98
- # dataset = Gcloud.datastore
157
+ # Finding an entity with a +kind+ and +id+/+name+:
158
+ #
99
159
  # task = dataset.find "Task", 123456
160
+ #
100
161
  def find key_or_kind, id_or_name = nil
101
162
  key = key_or_kind
102
163
  key = Key.new key_or_kind, id_or_name unless key_or_kind.is_a? Key
@@ -107,13 +168,22 @@ module Gcloud
107
168
  ##
108
169
  # Retrieve the entities for the provided keys.
109
170
  #
171
+ # === Parameters
172
+ #
173
+ # +keys+::
174
+ # One or more Key objects to find records for. (+Key+)
175
+ #
176
+ # === Returns
177
+ #
178
+ # Gcloud::Datastore::Dataset::LookupResults
179
+ #
180
+ # === Example
181
+ #
110
182
  # dataset = Gcloud.datastore
111
183
  # key1 = Gcloud::Datastore::Key.new "Task", 123456
112
184
  # key2 = Gcloud::Datastore::Key.new "Task", 987654
113
185
  # tasks = dataset.find_all key1, key2
114
186
  #
115
- # The entities returned from the lookup are returned in a
116
- # Dataset::LookupResults object.
117
187
  def find_all *keys
118
188
  response = connection.lookup(*keys.map(&:to_proto))
119
189
  entities = to_gcloud_entities response.found
@@ -125,10 +195,21 @@ module Gcloud
125
195
 
126
196
  ##
127
197
  # Remove entities from the Datastore.
128
- # Accepts Entity and Key objects.
198
+ #
199
+ # === Parameters
200
+ #
201
+ # +entities_or_keys+::
202
+ # One or more Entity or Key objects to remove. (+Entity+ or +Key+)
203
+ #
204
+ # === Returns
205
+ #
206
+ # +true+ if successful
207
+ #
208
+ # === Example
129
209
  #
130
210
  # dataset = Gcloud.datastore
131
- # dataset.delete task1, task2
211
+ # dataset.delete entity1, entity2
212
+ #
132
213
  def delete *entities_or_keys
133
214
  keys = entities_or_keys.map do |e_or_k|
134
215
  e_or_k.respond_to?(:key) ? e_or_k.key.to_proto : e_or_k.to_proto
@@ -143,12 +224,21 @@ module Gcloud
143
224
  ##
144
225
  # Retrieve entities specified by a Query.
145
226
  #
227
+ # === Parameters
228
+ #
229
+ # +query+::
230
+ # The Query object with the search criteria. (+Query+)
231
+ #
232
+ # === Returns
233
+ #
234
+ # Gcloud::Datastore::Dataset::QueryResults
235
+ #
236
+ # === Example
237
+ #
146
238
  # query = Gcloud::Datastore::Query.new.kind("Task").
147
239
  # where("completed", "=", true)
148
240
  # tasks = dataset.run query
149
241
  #
150
- # The entities returned from the query are returned in a
151
- # Dataset::QueryResults object.
152
242
  def run query
153
243
  response = connection.run_query query.to_proto
154
244
  entities = to_gcloud_entities response.batch.entity_result
@@ -159,13 +249,22 @@ module Gcloud
159
249
  alias_method :run_query, :run
160
250
 
161
251
  ##
162
- # Runs the given block in a database transaction.
163
- # If no block is given the transaction object is returned.
252
+ # Creates a Datastore Transaction.
253
+ #
254
+ # === Example
255
+ #
256
+ # Runs the given block in a database transaction:
257
+ #
258
+ # require "gcloud/datastore"
259
+ #
260
+ # dataset = Gcloud.datastore
261
+ #
262
+ # key = Gcloud::Datastore::Key.new "User", "heidi"
164
263
  #
165
264
  # user = Gcloud::Datastore::Entity.new
166
- # user.key = Gcloud::Datastore::Key.new "User", "username"
167
- # user["name"] = "Test"
168
- # user["email"] = "test@example.net"
265
+ # user.key = key
266
+ # user["name"] = "Heidi Henderson"
267
+ # user["email"] = "heidi@example.net"
169
268
  #
170
269
  # dataset.transaction do |tx|
171
270
  # if tx.find(user.key).nil?
@@ -173,13 +272,18 @@ module Gcloud
173
272
  # end
174
273
  # end
175
274
  #
176
- # Alternatively, you can manually commit or rollback by
177
- # using the returned transaction object.
275
+ # Alternatively, if no block is given a Transaction object is returned:
276
+ #
277
+ # require "gcloud/datastore"
278
+ #
279
+ # dataset = Gcloud.datastore
280
+ #
281
+ # key = Gcloud::Datastore::Key.new "User", "heidi"
178
282
  #
179
283
  # user = Gcloud::Datastore::Entity.new
180
- # user.key = Gcloud::Datastore::Key.new "User", "username"
181
- # user["name"] = "Test"
182
- # user["email"] = "test@example.net"
284
+ # user.key = key
285
+ # user["name"] = "Heidi Henderson"
286
+ # user["email"] = "heidi@example.net"
183
287
  #
184
288
  # tx = dataset.transaction
185
289
  # begin
@@ -190,6 +294,7 @@ module Gcloud
190
294
  # rescue
191
295
  # tx.rollback
192
296
  # end
297
+ #
193
298
  def transaction
194
299
  tx = Transaction.new connection
195
300
  return tx unless block_given?
@@ -1,3 +1,4 @@
1
+ #--
1
2
  # Copyright 2014 Google Inc. All rights reserved.
2
3
  #
3
4
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -1,3 +1,4 @@
1
+ #--
1
2
  # Copyright 2014 Google Inc. All rights reserved.
2
3
  #
3
4
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -30,15 +31,16 @@ module Gcloud
30
31
  #
31
32
  # entities = dataset.run query
32
33
  # entities.size #=> 3
33
- # entities.cursor #=> "c3VwZXJhd2Vzb21lIQ"
34
+ # entities.end_cursor #=> "c3VwZXJhd2Vzb21lIQ"
34
35
  # names = entities.map { |e| e.name }
35
36
  # names.size #=> 3
36
37
  # names.cursor #=> NoMethodError
37
38
  #
38
39
  class QueryResults < DelegateClass(::Array)
39
40
  ##
40
- # The cursor of the QueryResults.
41
- attr_reader :cursor
41
+ # The end_cursor of the QueryResults.
42
+ attr_reader :end_cursor
43
+ alias_method :cursor, :end_cursor
42
44
 
43
45
  ##
44
46
  # The state of the query after the current batch.
@@ -76,9 +78,9 @@ module Gcloud
76
78
 
77
79
  ##
78
80
  # Create a new QueryResults with an array of values.
79
- def initialize arr = [], cursor = nil, more_results = nil
81
+ def initialize arr = [], end_cursor = nil, more_results = nil
80
82
  super arr
81
- @cursor = cursor
83
+ @end_cursor = end_cursor
82
84
  @more_results = more_results
83
85
  end
84
86
  end
@@ -1,3 +1,4 @@
1
+ #--
1
2
  # Copyright 2014 Google Inc. All rights reserved.
2
3
  #
3
4
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -19,7 +20,9 @@ require "gcloud/datastore/proto"
19
20
  module Gcloud
20
21
  module Datastore
21
22
  ##
22
- # Entity represents the Datastore record.
23
+ # = Entity
24
+ #
25
+ # Entity represents a Datastore record.
23
26
  # Every Entity has a Key, and a list of properties.
24
27
  #
25
28
  # entity = Gcloud::Datastore::Entity.new
@@ -40,19 +43,67 @@ module Gcloud
40
43
  end
41
44
 
42
45
  ##
43
- # Retrieve a property value.
46
+ # Retrieve a property value by providing the name.
47
+ #
48
+ # === Parameters
49
+ #
50
+ # +prop_name+::
51
+ # The name of the property. (+String+ or +Symbol+)
52
+ #
53
+ # === Returns
54
+ #
55
+ # Object if the property exists, +nil+ if the property doesn't exist
56
+ #
57
+ # === Example
58
+ #
59
+ # Properties can be retrieved with a string name:
60
+ #
61
+ # require "gcloud/datastore"
62
+ #
63
+ # dataset = Gcloud.datastore
64
+ # user = datastore.find "User", "heidi"
65
+ # user["name"] #=> "Heidi Henderson"
44
66
  #
45
- # puts entity["name"]
67
+ # Or with a symbol name:
68
+ #
69
+ # require "gcloud/datastore"
70
+ #
71
+ # dataset = Gcloud.datastore
72
+ # user = datastore.find "User", "heidi"
73
+ # user[:name] #=> "Heidi Henderson"
46
74
  #
47
- # If a property doesn't exist then nil will be returned.
48
75
  def [] prop_name
49
76
  @properties[prop_name]
50
77
  end
51
78
 
52
79
  ##
53
- # Set a property value.
80
+ # Set a property value by name.
81
+ #
82
+ # === Parameters
83
+ #
84
+ # +prop_name+::
85
+ # The name of the property. (+String+ or +Symbol+)
86
+ # +prop_value+::
87
+ # The value of the property. (+Object+)
88
+ #
89
+ # === Example
90
+ #
91
+ # Properties can be set with a string name:
92
+ #
93
+ # require "gcloud/datastore"
94
+ #
95
+ # dataset = Gcloud.datastore
96
+ # user = datastore.find "User", "heidi"
97
+ # user["name"] = "Heidi H. Henderson"
98
+ #
99
+ # Or with a symbol name:
100
+ #
101
+ # require "gcloud/datastore"
102
+ #
103
+ # dataset = Gcloud.datastore
104
+ # user = datastore.find "User", "heidi"
105
+ # user[:name] = "Heidi H. Henderson"
54
106
  #
55
- # entity["name"] = "User McUser"
56
107
  def []= prop_name, prop_value
57
108
  @properties[prop_name] = prop_value
58
109
  end
@@ -61,6 +112,12 @@ module Gcloud
61
112
  # Retrieve properties in a hash-like structure.
62
113
  # Properties can be accessed or set by string or symbol.
63
114
  #
115
+ # === Returns
116
+ #
117
+ # Gcloud::Datastore::Properties
118
+ #
119
+ # === Example
120
+ #
64
121
  # entity.properties[:name] = "User McUser"
65
122
  # entity.properties["name"] #=> "User McUser"
66
123
  #
@@ -83,29 +140,54 @@ module Gcloud
83
140
  #
84
141
  # prop_hash = entity.properties.to_h
85
142
  #
86
- # See Gcloud::Datastore::Properties for more.
87
143
  attr_reader :properties
88
144
 
89
145
  ##
90
146
  # Sets the Key that identifies the entity.
91
- # This can only be set before the entity is saved.
147
+ #
148
+ # === Example
149
+ #
150
+ # The Key can be set before the entity is saved.
151
+ #
152
+ # require "glcoud/datastore"
153
+ #
154
+ # dataset = Gcloud.datastore
155
+ # entity = Gcloud::Datastore::Entity.new
156
+ # entity.key = Gcloud::Datastore::Key.new "User"
157
+ # dataset.save entity
158
+ #
92
159
  # Once the entity is saved, the key is frozen and immutable.
160
+ # Trying to set a key when immutable will raise a +RuntimeError+.
161
+ #
162
+ # require "gcloud/datastore"
93
163
  #
94
- # Trying to set a key when immutable will raise a RuntimeError.
164
+ # dataset = Gcloud.datastore
165
+ # entity = dataset.find "User", "heidi"
166
+ # entity.persisted? #=> true
167
+ # entity.key = Gcloud::Datastore::Key.new "User" #=> RuntimeError
168
+ # entity.key.frozen? #=> true
169
+ # entity.key.id = 9876543221 #=> RuntimeError
95
170
  #
96
- # task = dataset.find "Task", 123456
97
- # task.persisted? #=> true
98
- # task.key = Gcloud::Datastore::Key.new "Task", 456789 #=> RuntimeError
99
- # task.key.frozen? #=> true
100
- # task.key.id = 456789 #=> RuntimeError
101
171
  def key= new_key
102
172
  fail "This entity's key is immutable." if persisted?
103
173
  @key = new_key
104
174
  end
105
175
 
106
176
  ##
107
- # Returns true if the record is persisted to the datastore.
108
- # Otherwise returns false.
177
+ # Indicates if the record is persisted. Default is false.
178
+ #
179
+ # === Example
180
+ #
181
+ # require "gcloud/datastore"
182
+ #
183
+ # dataset = Gcloud.datastore
184
+ #
185
+ # new_entity = Gcloud::Datastore::Entity.new
186
+ # new_entity.persisted? #=> false
187
+ #
188
+ # found_entity = dataset.find "User", "heidi"
189
+ # found_entity.persisted? #=> true
190
+ #
109
191
  def persisted?
110
192
  @key && @key.frozen?
111
193
  end
@@ -122,7 +204,7 @@ module Gcloud
122
204
  # Array property values will return an array of flag settings.
123
205
  #
124
206
  # entity["tags"] = ["ruby", "code"]
125
- # entity.exclude_from_indexes? "tags" #=> true [false, false]
207
+ # entity.exclude_from_indexes? "tags" #=> [false, false]
126
208
  def exclude_from_indexes? name
127
209
  value = self[name]
128
210
  flag = @_exclude_indexes[name.to_s]