gcloud 0.12.2 → 0.20.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (169) hide show
  1. checksums.yaml +5 -13
  2. data/lib/gcloud.rb +27 -456
  3. data/lib/gcloud/bigquery.rb +2 -382
  4. data/lib/gcloud/datastore.rb +2 -576
  5. data/lib/gcloud/dns.rb +2 -321
  6. data/lib/gcloud/logging.rb +1 -322
  7. data/lib/gcloud/pubsub.rb +2 -476
  8. data/lib/gcloud/resource_manager.rb +2 -273
  9. data/lib/gcloud/storage.rb +2 -440
  10. data/lib/gcloud/translate.rb +1 -250
  11. data/lib/gcloud/version.rb +2 -2
  12. data/lib/gcloud/vision.rb +1 -501
  13. metadata +36 -332
  14. data/AUTHENTICATION.md +0 -75
  15. data/CHANGELOG.md +0 -382
  16. data/OVERVIEW.md +0 -259
  17. data/lib/gcloud/backoff.rb +0 -150
  18. data/lib/gcloud/bigquery/copy_job.rb +0 -97
  19. data/lib/gcloud/bigquery/credentials.rb +0 -29
  20. data/lib/gcloud/bigquery/data.rb +0 -239
  21. data/lib/gcloud/bigquery/dataset.rb +0 -753
  22. data/lib/gcloud/bigquery/dataset/access.rb +0 -507
  23. data/lib/gcloud/bigquery/dataset/list.rb +0 -169
  24. data/lib/gcloud/bigquery/extract_job.rb +0 -117
  25. data/lib/gcloud/bigquery/insert_response.rb +0 -81
  26. data/lib/gcloud/bigquery/job.rb +0 -299
  27. data/lib/gcloud/bigquery/job/list.rb +0 -172
  28. data/lib/gcloud/bigquery/load_job.rb +0 -202
  29. data/lib/gcloud/bigquery/project.rb +0 -475
  30. data/lib/gcloud/bigquery/query_data.rb +0 -234
  31. data/lib/gcloud/bigquery/query_job.rb +0 -137
  32. data/lib/gcloud/bigquery/schema.rb +0 -359
  33. data/lib/gcloud/bigquery/service.rb +0 -506
  34. data/lib/gcloud/bigquery/table.rb +0 -1141
  35. data/lib/gcloud/bigquery/table/list.rb +0 -180
  36. data/lib/gcloud/bigquery/view.rb +0 -475
  37. data/lib/gcloud/credentials.rb +0 -129
  38. data/lib/gcloud/datastore/commit.rb +0 -148
  39. data/lib/gcloud/datastore/credentials.rb +0 -35
  40. data/lib/gcloud/datastore/cursor.rb +0 -76
  41. data/lib/gcloud/datastore/dataset.rb +0 -660
  42. data/lib/gcloud/datastore/dataset/lookup_results.rb +0 -219
  43. data/lib/gcloud/datastore/dataset/query_results.rb +0 -386
  44. data/lib/gcloud/datastore/entity.rb +0 -449
  45. data/lib/gcloud/datastore/errors.rb +0 -41
  46. data/lib/gcloud/datastore/gql_query.rb +0 -211
  47. data/lib/gcloud/datastore/grpc_utils.rb +0 -132
  48. data/lib/gcloud/datastore/key.rb +0 -281
  49. data/lib/gcloud/datastore/properties.rb +0 -128
  50. data/lib/gcloud/datastore/query.rb +0 -348
  51. data/lib/gcloud/datastore/service.rb +0 -167
  52. data/lib/gcloud/datastore/transaction.rb +0 -362
  53. data/lib/gcloud/dns/change.rb +0 -158
  54. data/lib/gcloud/dns/change/list.rb +0 -173
  55. data/lib/gcloud/dns/credentials.rb +0 -29
  56. data/lib/gcloud/dns/importer.rb +0 -183
  57. data/lib/gcloud/dns/project.rb +0 -247
  58. data/lib/gcloud/dns/record.rb +0 -170
  59. data/lib/gcloud/dns/record/list.rb +0 -174
  60. data/lib/gcloud/dns/service.rb +0 -167
  61. data/lib/gcloud/dns/zone.rb +0 -759
  62. data/lib/gcloud/dns/zone/list.rb +0 -168
  63. data/lib/gcloud/dns/zone/transaction.rb +0 -176
  64. data/lib/gcloud/errors.rb +0 -206
  65. data/lib/gcloud/gce.rb +0 -56
  66. data/lib/gcloud/grpc_utils.rb +0 -87
  67. data/lib/gcloud/logging/credentials.rb +0 -29
  68. data/lib/gcloud/logging/entry.rb +0 -465
  69. data/lib/gcloud/logging/entry/http_request.rb +0 -141
  70. data/lib/gcloud/logging/entry/list.rb +0 -177
  71. data/lib/gcloud/logging/entry/operation.rb +0 -90
  72. data/lib/gcloud/logging/logger.rb +0 -307
  73. data/lib/gcloud/logging/metric.rb +0 -169
  74. data/lib/gcloud/logging/metric/list.rb +0 -172
  75. data/lib/gcloud/logging/project.rb +0 -642
  76. data/lib/gcloud/logging/resource.rb +0 -84
  77. data/lib/gcloud/logging/resource_descriptor.rb +0 -137
  78. data/lib/gcloud/logging/resource_descriptor/list.rb +0 -174
  79. data/lib/gcloud/logging/service.rb +0 -267
  80. data/lib/gcloud/logging/sink.rb +0 -227
  81. data/lib/gcloud/logging/sink/list.rb +0 -171
  82. data/lib/gcloud/pubsub/credentials.rb +0 -29
  83. data/lib/gcloud/pubsub/message.rb +0 -94
  84. data/lib/gcloud/pubsub/policy.rb +0 -204
  85. data/lib/gcloud/pubsub/project.rb +0 -482
  86. data/lib/gcloud/pubsub/received_message.rb +0 -160
  87. data/lib/gcloud/pubsub/service.rb +0 -334
  88. data/lib/gcloud/pubsub/subscription.rb +0 -565
  89. data/lib/gcloud/pubsub/subscription/list.rb +0 -208
  90. data/lib/gcloud/pubsub/topic.rb +0 -511
  91. data/lib/gcloud/pubsub/topic/list.rb +0 -174
  92. data/lib/gcloud/pubsub/topic/publisher.rb +0 -85
  93. data/lib/gcloud/resource_manager/credentials.rb +0 -30
  94. data/lib/gcloud/resource_manager/manager.rb +0 -266
  95. data/lib/gcloud/resource_manager/policy.rb +0 -211
  96. data/lib/gcloud/resource_manager/project.rb +0 -484
  97. data/lib/gcloud/resource_manager/project/list.rb +0 -167
  98. data/lib/gcloud/resource_manager/project/updater.rb +0 -130
  99. data/lib/gcloud/resource_manager/service.rb +0 -127
  100. data/lib/gcloud/storage/bucket.rb +0 -775
  101. data/lib/gcloud/storage/bucket/acl.rb +0 -810
  102. data/lib/gcloud/storage/bucket/cors.rb +0 -153
  103. data/lib/gcloud/storage/bucket/list.rb +0 -172
  104. data/lib/gcloud/storage/credentials.rb +0 -29
  105. data/lib/gcloud/storage/errors.rb +0 -65
  106. data/lib/gcloud/storage/file.rb +0 -842
  107. data/lib/gcloud/storage/file/acl.rb +0 -425
  108. data/lib/gcloud/storage/file/list.rb +0 -191
  109. data/lib/gcloud/storage/file/verifier.rb +0 -67
  110. data/lib/gcloud/storage/project.rb +0 -316
  111. data/lib/gcloud/storage/service.rb +0 -347
  112. data/lib/gcloud/translate/api.rb +0 -241
  113. data/lib/gcloud/translate/detection.rb +0 -137
  114. data/lib/gcloud/translate/language.rb +0 -69
  115. data/lib/gcloud/translate/service.rb +0 -80
  116. data/lib/gcloud/translate/translation.rb +0 -112
  117. data/lib/gcloud/vision/annotate.rb +0 -224
  118. data/lib/gcloud/vision/annotation.rb +0 -455
  119. data/lib/gcloud/vision/annotation/entity.rb +0 -234
  120. data/lib/gcloud/vision/annotation/face.rb +0 -1750
  121. data/lib/gcloud/vision/annotation/properties.rb +0 -245
  122. data/lib/gcloud/vision/annotation/safe_search.rb +0 -161
  123. data/lib/gcloud/vision/annotation/text.rb +0 -236
  124. data/lib/gcloud/vision/annotation/vertex.rb +0 -108
  125. data/lib/gcloud/vision/credentials.rb +0 -29
  126. data/lib/gcloud/vision/image.rb +0 -590
  127. data/lib/gcloud/vision/location.rb +0 -115
  128. data/lib/gcloud/vision/project.rb +0 -278
  129. data/lib/gcloud/vision/service.rb +0 -66
  130. data/lib/google/api/annotations.rb +0 -14
  131. data/lib/google/api/http.rb +0 -30
  132. data/lib/google/api/label.rb +0 -24
  133. data/lib/google/api/monitored_resource.rb +0 -25
  134. data/lib/google/datastore/v1beta3/datastore.rb +0 -115
  135. data/lib/google/datastore/v1beta3/datastore_services.rb +0 -33
  136. data/lib/google/datastore/v1beta3/entity.rb +0 -63
  137. data/lib/google/datastore/v1beta3/query.rb +0 -128
  138. data/lib/google/devtools/cloudtrace/v1/trace.rb +0 -78
  139. data/lib/google/devtools/cloudtrace/v1/trace_services.rb +0 -32
  140. data/lib/google/example/library/v1/library.rb +0 -91
  141. data/lib/google/example/library/v1/library_services.rb +0 -40
  142. data/lib/google/iam/v1/iam_policy.rb +0 -33
  143. data/lib/google/iam/v1/iam_policy_services.rb +0 -30
  144. data/lib/google/iam/v1/policy.rb +0 -25
  145. data/lib/google/logging/type/http_request.rb +0 -28
  146. data/lib/google/logging/type/log_severity.rb +0 -27
  147. data/lib/google/logging/v2/log_entry.rb +0 -44
  148. data/lib/google/logging/v2/logging.rb +0 -56
  149. data/lib/google/logging/v2/logging_config.rb +0 -59
  150. data/lib/google/logging/v2/logging_config_services.rb +0 -32
  151. data/lib/google/logging/v2/logging_metrics.rb +0 -51
  152. data/lib/google/logging/v2/logging_metrics_services.rb +0 -32
  153. data/lib/google/logging/v2/logging_services.rb +0 -31
  154. data/lib/google/longrunning/operations.rb +0 -50
  155. data/lib/google/longrunning/operations_services.rb +0 -29
  156. data/lib/google/protobuf/descriptor.rb +0 -0
  157. data/lib/google/pubsub/v1/pubsub.rb +0 -129
  158. data/lib/google/pubsub/v1/pubsub_services.rb +0 -56
  159. data/lib/google/pubsub/v1beta2/pubsub.rb +0 -126
  160. data/lib/google/pubsub/v1beta2/pubsub_services.rb +0 -56
  161. data/lib/google/rpc/code.rb +0 -32
  162. data/lib/google/rpc/error_details.rb +0 -61
  163. data/lib/google/rpc/status.rb +0 -19
  164. data/lib/google/type/color.rb +0 -20
  165. data/lib/google/type/date.rb +0 -18
  166. data/lib/google/type/dayofweek.rb +0 -23
  167. data/lib/google/type/latlng.rb +0 -17
  168. data/lib/google/type/money.rb +0 -18
  169. data/lib/google/type/timeofday.rb +0 -19
@@ -1,234 +0,0 @@
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 "gcloud/bigquery/service"
17
- require "gcloud/bigquery/data"
18
-
19
- module Gcloud
20
- module Bigquery
21
- ##
22
- # # QueryData
23
- #
24
- # Represents Data returned from a query a a list of name/value pairs.
25
- class QueryData < Data
26
- ##
27
- # @private The Service object.
28
- attr_accessor :service
29
-
30
- # @private
31
- def initialize arr = []
32
- @job = nil
33
- super
34
- end
35
-
36
- # The total number of bytes processed for this query.
37
- def total_bytes
38
- Integer @gapi.total_bytes_processed
39
- rescue
40
- nil
41
- end
42
-
43
- # Whether the query has completed or not. When data is present this will
44
- # always be `true`. When `false`, `total` will not be available.
45
- def complete?
46
- @gapi.job_complete
47
- end
48
-
49
- # Whether the query result was fetched from the query cache.
50
- def cache_hit?
51
- @gapi.cache_hit
52
- end
53
-
54
- ##
55
- # The schema of the data.
56
- def schema
57
- Schema.from_gapi(@gapi.schema).freeze
58
- end
59
-
60
- ##
61
- # The fields of the data.
62
- def fields
63
- f = schema.fields
64
- f = f.to_hash if f.respond_to? :to_hash
65
- f = [] if f.nil?
66
- f
67
- end
68
-
69
- ##
70
- # The name of the columns in the data.
71
- def headers
72
- fields.map(&:name)
73
- end
74
-
75
- ##
76
- # Whether there is a next page of query data.
77
- #
78
- # @return [Boolean]
79
- #
80
- # @example
81
- # require "gcloud"
82
- #
83
- # gcloud = Gcloud.new
84
- # bigquery = gcloud.bigquery
85
- # job = bigquery.job "my_job"
86
- #
87
- # data = job.query_results
88
- # if data.next?
89
- # next_data = data.next
90
- # end
91
- #
92
- def next?
93
- !token.nil?
94
- end
95
-
96
- ##
97
- # Retrieve the next page of query data.
98
- #
99
- # @return [QueryData]
100
- #
101
- # @example
102
- # require "gcloud"
103
- #
104
- # gcloud = Gcloud.new
105
- # bigquery = gcloud.bigquery
106
- # job = bigquery.job "my_job"
107
- #
108
- # data = job.query_results
109
- # if data.next?
110
- # next_data = data.next
111
- # end
112
- #
113
- def next
114
- return nil unless next?
115
- ensure_service!
116
- gapi = service.job_query_results job_id, token: token
117
- QueryData.from_gapi gapi, service
118
- end
119
-
120
- ##
121
- # Retrieves all rows by repeatedly loading {#next} until {#next?} returns
122
- # `false`. Calls the given block once for each row, which is passed as the
123
- # parameter.
124
- #
125
- # An Enumerator is returned if no block is given.
126
- #
127
- # This method may make several API calls until all rows are retrieved. Be
128
- # sure to use as narrow a search criteria as possible. Please use with
129
- # caution.
130
- #
131
- # @param [Integer] request_limit The upper limit of API requests to make
132
- # to load all data. Default is no limit.
133
- # @yield [row] The block for accessing each row of data.
134
- # @yieldparam [Hash] row The row object.
135
- #
136
- # @return [Enumerator]
137
- #
138
- # @example Iterating each row by passing a block:
139
- # require "gcloud"
140
- #
141
- # gcloud = Gcloud.new
142
- # bigquery = gcloud.bigquery
143
- # job = bigquery.job "my_job"
144
- #
145
- # data = job.query_results
146
- # data.all do |row|
147
- # puts row["word"]
148
- # end
149
- #
150
- # @example Using the enumerator by not passing a block:
151
- # require "gcloud"
152
- #
153
- # gcloud = Gcloud.new
154
- # bigquery = gcloud.bigquery
155
- # job = bigquery.job "my_job"
156
- #
157
- # data = job.query_results
158
- # words = data.all.map do |row|
159
- # row["word"]
160
- # end
161
- #
162
- # @example Limit the number of API calls made:
163
- # require "gcloud"
164
- #
165
- # gcloud = Gcloud.new
166
- # bigquery = gcloud.bigquery
167
- # job = bigquery.job "my_job"
168
- #
169
- # data = job.query_results
170
- # data.all(request_limit: 10) do |row|
171
- # puts row["word"]
172
- # end
173
- #
174
- def all request_limit: nil
175
- request_limit = request_limit.to_i if request_limit
176
- return enum_for(:all, request_limit: request_limit) unless block_given?
177
- results = self
178
- loop do
179
- results.each { |r| yield r }
180
- if request_limit
181
- request_limit -= 1
182
- break if request_limit < 0
183
- end
184
- break unless results.next?
185
- results = results.next
186
- end
187
- end
188
-
189
- ##
190
- # The BigQuery {Job} that was created to run the query.
191
- def job
192
- return @job if @job
193
- return nil unless job?
194
- ensure_service!
195
- gapi = service.get_job job_id
196
- @job = Job.from_gapi gapi, service
197
- rescue Gcloud::NotFoundError
198
- nil
199
- end
200
-
201
- ##
202
- # @private New Data from a response object.
203
- def self.from_gapi gapi, service
204
- if gapi.schema.nil?
205
- formatted_rows = []
206
- else
207
- formatted_rows = format_rows gapi.rows,
208
- gapi.schema.fields
209
- end
210
-
211
- data = new formatted_rows
212
- data.gapi = gapi
213
- data.service = service
214
- data
215
- end
216
-
217
- protected
218
-
219
- ##
220
- # Raise an error unless an active connection is available.
221
- def ensure_service!
222
- fail "Must have active connection" unless service
223
- end
224
-
225
- def job?
226
- @gapi.job_reference && @gapi.job_reference.job_id
227
- end
228
-
229
- def job_id
230
- @gapi.job_reference.job_id
231
- end
232
- end
233
- end
234
- end
@@ -1,137 +0,0 @@
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 "gcloud/bigquery/service"
17
-
18
- module Gcloud
19
- module Bigquery
20
- ##
21
- # # QueryJob
22
- #
23
- # A {Job} subclass representing a query operation that may be performed
24
- # on a {Table}. A QueryJob instance is created when you call
25
- # {Project#query_job}, {Dataset#query_job}, or {View#data}.
26
- #
27
- # @see https://cloud.google.com/bigquery/querying-data Querying Data
28
- # @see https://cloud.google.com/bigquery/docs/reference/v2/jobs Jobs API
29
- # reference
30
- #
31
- class QueryJob < Job
32
- ##
33
- # Checks if the priority for the query is `BATCH`.
34
- def batch?
35
- val = @gapi.configuration.query.priority
36
- val == "BATCH"
37
- end
38
-
39
- ##
40
- # Checks if the priority for the query is `INTERACTIVE`.
41
- def interactive?
42
- val = @gapi.configuration.query.priority
43
- return true if val.nil?
44
- val == "INTERACTIVE"
45
- end
46
-
47
- ##
48
- # Checks if the the query job allows arbitrarily large results at a slight
49
- # cost to performance.
50
- def large_results?
51
- val = @gapi.configuration.query.allow_large_results
52
- return false if val.nil?
53
- val
54
- end
55
-
56
- ##
57
- # Checks if the query job looks for an existing result in the query cache.
58
- # For more information, see [Query
59
- # Caching](https://cloud.google.com/bigquery/querying-data#querycaching).
60
- def cache?
61
- val = @gapi.configuration.query.use_query_cache
62
- return false if val.nil?
63
- val
64
- end
65
-
66
- ##
67
- # Checks if the query job flattens nested and repeated fields in the query
68
- # results. The default is `true`. If the value is `false`, #large_results?
69
- # should return `true`.
70
- def flatten?
71
- val = @gapi.configuration.query.flatten_results
72
- return true if val.nil?
73
- val
74
- end
75
-
76
- ##
77
- # Checks if the query results are from the query cache.
78
- def cache_hit?
79
- @gapi.statistics.query.cache_hit
80
- end
81
-
82
- ##
83
- # The number of bytes processed by the query.
84
- def bytes_processed
85
- Integer @gapi.statistics.query.total_bytes_processed
86
- rescue
87
- nil
88
- end
89
-
90
- ##
91
- # The table in which the query results are stored.
92
- def destination
93
- table = @gapi.configuration.query.destination_table
94
- return nil unless table
95
- retrieve_table table.project_id,
96
- table.dataset_id,
97
- table.table_id
98
- end
99
-
100
- ##
101
- # Retrieves the query results for the job.
102
- #
103
- # @param [String] token Page token, returned by a previous call,
104
- # identifying the result set.
105
- # @param [Integer] max Maximum number of results to return.
106
- # @param [Integer] start Zero-based index of the starting row to read.
107
- # @param [Integer] timeout How long to wait for the query to complete, in
108
- # milliseconds, before returning. Default is 10,000 milliseconds (10
109
- # seconds).
110
- #
111
- # @return [Gcloud::Bigquery::QueryData]
112
- #
113
- # @example
114
- # require "gcloud"
115
- #
116
- # gcloud = Gcloud.new
117
- # bigquery = gcloud.bigquery
118
- #
119
- # q = "SELECT word FROM publicdata:samples.shakespeare"
120
- # job = bigquery.query_job q
121
- #
122
- # job.wait_until_done!
123
- # data = job.query_results
124
- # data.each do |row|
125
- # puts row["word"]
126
- # end
127
- # data = data.next if data.next?
128
- #
129
- def query_results token: nil, max: nil, start: nil, timeout: nil
130
- ensure_service!
131
- options = { token: token, max: max, start: start, timeout: timeout }
132
- gapi = service.job_query_results job_id, options
133
- QueryData.from_gapi gapi, service
134
- end
135
- end
136
- end
137
- end
@@ -1,359 +0,0 @@
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 Gcloud
17
- module Bigquery
18
- ##
19
- # # Table Schema
20
- #
21
- # A builder for BigQuery table schemas, passed to block arguments to
22
- # {Dataset#create_table} and {Table#schema}. Supports nested and
23
- # repeated fields via a nested block.
24
- #
25
- # @see https://cloud.google.com/bigquery/preparing-data-for-bigquery
26
- # Preparing Data for BigQuery
27
- #
28
- # @example
29
- # require "gcloud"
30
- #
31
- # gcloud = Gcloud.new
32
- # bigquery = gcloud.bigquery
33
- # dataset = bigquery.dataset "my_dataset"
34
- # table = dataset.create_table "my_table"
35
- #
36
- # table.schema do |schema|
37
- # schema.string "first_name", mode: :required
38
- # schema.record "cities_lived", mode: :repeated do |cities_lived|
39
- # cities_lived.string "place", mode: :required
40
- # cities_lived.integer "number_of_years", mode: :required
41
- # end
42
- # end
43
- #
44
- class Schema
45
- def initialize
46
- @nested = nil
47
- end
48
-
49
- def fields
50
- @fields ||= @gapi.fields.map { |f| Field.from_gapi f }
51
- end
52
-
53
- def fields= new_fields
54
- @gapi.fields = Array(new_fields).map(&:to_gapi)
55
- @fields = @gapi.fields.map { |f| Field.from_gapi f }
56
- end
57
-
58
- def empty?
59
- fields.empty?
60
- end
61
-
62
- # @private
63
- def changed?
64
- return false if frozen?
65
- check_for_mutated_schema!
66
- @original_json != @gapi.to_json
67
- end
68
-
69
- # @private
70
- def freeze
71
- @gapi = @gapi.dup.freeze
72
- @gapi.fields.freeze
73
- @fields = @gapi.fields.map { |f| Field.from_gapi(f).freeze }
74
- @fields.freeze
75
- super
76
- end
77
-
78
- ##
79
- # @private Make sure any changes are saved.
80
- def check_for_mutated_schema!
81
- return if frozen?
82
- return if @gapi.frozen?
83
- return if @fields.nil?
84
- gapi_fields = Array(@fields).map(&:to_gapi)
85
- @gapi.update! fields: gapi_fields
86
- end
87
-
88
- # @private
89
- def self.from_gapi gapi
90
- gapi ||= Google::Apis::BigqueryV2::TableSchema.new fields: []
91
- gapi.fields ||= []
92
- new.tap do |s|
93
- s.instance_variable_set :@gapi, gapi
94
- s.instance_variable_set :@original_json, gapi.to_json
95
- end
96
- end
97
-
98
- # @private
99
- def to_gapi
100
- check_for_mutated_schema!
101
- @gapi
102
- end
103
-
104
- # @private
105
- def == other
106
- return false unless other.is_a? Schema
107
- to_gapi.to_h == other.to_gapi.to_h
108
- end
109
-
110
- ##
111
- # Adds a string field to the schema.
112
- #
113
- # @param [String] name The field name. The name must contain only
114
- # letters (a-z, A-Z), numbers (0-9), or underscores (_), and must
115
- # start with a letter or underscore. The maximum length is 128
116
- # characters.
117
- # @param [String] description A description of the field.
118
- # @param [Symbol] mode The field's mode. The possible values are
119
- # `:nullable`, `:required`, and `:repeated`. The default value is
120
- # `:nullable`.
121
- def string name, description: nil, mode: :nullable
122
- add_field name, :string, nil, description: description, mode: mode
123
- end
124
-
125
- ##
126
- # Adds an integer field to the schema.
127
- #
128
- # @param [String] name The field name. The name must contain only
129
- # letters (a-z, A-Z), numbers (0-9), or underscores (_), and must
130
- # start with a letter or underscore. The maximum length is 128
131
- # characters.
132
- # @param [String] description A description of the field.
133
- # @param [Symbol] mode The field's mode. The possible values are
134
- # `:nullable`, `:required`, and `:repeated`. The default value is
135
- # `:nullable`.
136
- def integer name, description: nil, mode: :nullable
137
- add_field name, :integer, nil, description: description, mode: mode
138
- end
139
-
140
- ##
141
- # Adds a floating-point number field to the schema.
142
- #
143
- # @param [String] name The field name. The name must contain only
144
- # letters (a-z, A-Z), numbers (0-9), or underscores (_), and must
145
- # start with a letter or underscore. The maximum length is 128
146
- # characters.
147
- # @param [String] description A description of the field.
148
- # @param [Symbol] mode The field's mode. The possible values are
149
- # `:nullable`, `:required`, and `:repeated`. The default value is
150
- # `:nullable`.
151
- def float name, description: nil, mode: :nullable
152
- add_field name, :float, nil, description: description, mode: mode
153
- end
154
-
155
- ##
156
- # Adds a boolean field to the schema.
157
- #
158
- # @param [String] name The field name. The name must contain only
159
- # letters (a-z, A-Z), numbers (0-9), or underscores (_), and must
160
- # start with a letter or underscore. The maximum length is 128
161
- # characters.
162
- # @param [String] description A description of the field.
163
- # @param [Symbol] mode The field's mode. The possible values are
164
- # `:nullable`, `:required`, and `:repeated`. The default value is
165
- # `:nullable`.
166
- def boolean name, description: nil, mode: :nullable
167
- add_field name, :boolean, nil, description: description, mode: mode
168
- end
169
-
170
- ##
171
- # Adds a timestamp field to the schema.
172
- #
173
- # @param [String] name The field name. The name must contain only
174
- # letters (a-z, A-Z), numbers (0-9), or underscores (_), and must
175
- # start with a letter or underscore. The maximum length is 128
176
- # characters.
177
- # @param [String] description A description of the field.
178
- # @param [Symbol] mode The field's mode. The possible values are
179
- # `:nullable`, `:required`, and `:repeated`. The default value is
180
- # `:nullable`.
181
- def timestamp name, description: nil, mode: :nullable
182
- add_field name, :timestamp, nil, description: description, mode: mode
183
- end
184
-
185
- ##
186
- # Adds a record field to the schema. A block must be passed describing
187
- # the nested fields of the record. For more information about nested
188
- # and repeated records, see [Preparing Data for BigQuery
189
- # ](https://cloud.google.com/bigquery/preparing-data-for-bigquery).
190
- #
191
- # @param [String] name The field name. The name must contain only
192
- # letters (a-z, A-Z), numbers (0-9), or underscores (_), and must
193
- # start with a letter or underscore. The maximum length is 128
194
- # characters.
195
- # @param [String] description A description of the field.
196
- # @param [Symbol] mode The field's mode. The possible values are
197
- # `:nullable`, `:required`, and `:repeated`. The default value is
198
- # `:nullable`.
199
- # @yield [nested_schema] a block for setting the nested schema
200
- # @yieldparam [Schema] nested_schema the object accepting the nested
201
- # schema
202
- #
203
- # @example
204
- # require "gcloud"
205
- #
206
- # gcloud = Gcloud.new
207
- # bigquery = gcloud.bigquery
208
- # dataset = bigquery.dataset "my_dataset"
209
- # table = dataset.create_table "my_table"
210
- #
211
- # table.schema do |schema|
212
- # schema.string "first_name", mode: :required
213
- # schema.record "cities_lived", mode: :repeated do |cities_lived|
214
- # cities_lived.string "place", mode: :required
215
- # cities_lived.integer "number_of_years", mode: :required
216
- # end
217
- # end
218
- #
219
- def record name, description: nil, mode: nil
220
- fail ArgumentError, "nested RECORD type is not permitted" if @nested
221
- fail ArgumentError, "a block is required" unless block_given?
222
- empty_schema = Google::Apis::BigqueryV2::TableSchema.new fields: []
223
- nested_schema = self.class.from_gapi(empty_schema).tap do |s|
224
- s.instance_variable_set :@nested, true
225
- end
226
- yield nested_schema
227
- add_field name, :record, nested_schema.fields,
228
- description: description, mode: mode
229
- end
230
-
231
- protected
232
-
233
- def add_field name, type, nested_fields, description: nil,
234
- mode: :nullable
235
- # Remove any existing field of this name
236
- fields.reject! { |f| f.name == name }
237
- fields << Field.new(name, type, description: description,
238
- mode: mode, fields: nested_fields)
239
- end
240
-
241
- class Field
242
- # @private
243
- MODES = %w( NULLABLE REQUIRED REPEATED )
244
-
245
- # @private
246
- TYPES = %w( STRING INTEGER FLOAT BOOLEAN TIMESTAMP RECORD )
247
-
248
- def initialize name, type, description: nil,
249
- mode: :nullable, fields: nil
250
- @gapi = Google::Apis::BigqueryV2::TableFieldSchema.new
251
- @gapi.update! name: name
252
- @gapi.update! type: verify_type(type)
253
- @gapi.update! description: description if description
254
- @gapi.update! mode: verify_mode(mode) if mode
255
- if fields
256
- @fields = fields
257
- check_for_changed_fields!
258
- end
259
- @original_json = @gapi.to_json
260
- end
261
-
262
- def name
263
- @gapi.name
264
- end
265
-
266
- def name= new_name
267
- @gapi.update! name: new_name
268
- end
269
-
270
- def type
271
- @gapi.type
272
- end
273
-
274
- def type= new_type
275
- @gapi.update! type: verify_type(new_type)
276
- end
277
-
278
- def description
279
- @gapi.description
280
- end
281
-
282
- def description= new_description
283
- @gapi.update! description: new_description
284
- end
285
-
286
- def mode
287
- @gapi.mode
288
- end
289
-
290
- def mode= new_mode
291
- @gapi.update! mode: verify_mode(new_mode)
292
- end
293
-
294
- def fields
295
- @fields ||= Array(@gapi.fields).map { |f| Field.from_gapi f }
296
- end
297
-
298
- def fields= new_fields
299
- @fields = new_fields
300
- end
301
-
302
- ##
303
- # @private Make sure any fields are saved.
304
- def check_for_changed_fields!
305
- return if frozen?
306
- fields.each(&:check_for_changed_fields!)
307
- gapi_fields = Array(fields).map(&:to_gapi)
308
- gapi_fields = nil if gapi_fields.empty?
309
- @gapi.update! fields: gapi_fields
310
- end
311
-
312
- # @private
313
- def changed?
314
- @original_json == to_gapi.to_json
315
- end
316
-
317
- # @private
318
- def self.from_gapi gapi
319
- new("to-be-replaced", "STRING").tap do |f|
320
- f.instance_variable_set :@gapi, gapi
321
- f.instance_variable_set :@original_json, gapi.to_json
322
- end
323
- end
324
-
325
- # @private
326
- def to_gapi
327
- # make sure any changes are saved.
328
- check_for_changed_fields!
329
- @gapi
330
- end
331
-
332
- # @private
333
- def == other
334
- return false unless other.is_a? Field
335
- to_gapi.to_h == other.to_gapi.to_h
336
- end
337
-
338
- protected
339
-
340
- def verify_type type
341
- upcase_type = type.to_s.upcase
342
- unless TYPES.include? upcase_type
343
- fail ArgumentError,
344
- "Type '#{upcase_type}' not found in #{TYPES.inspect}"
345
- end
346
- upcase_type
347
- end
348
-
349
- def verify_mode mode
350
- upcase_mode = mode.to_s.upcase
351
- unless MODES.include? upcase_mode
352
- fail ArgumentError "Unable to determine mode for '#{mode}'"
353
- end
354
- upcase_mode
355
- end
356
- end
357
- end
358
- end
359
- end