gcloud 0.7.2 → 0.8.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.
@@ -1,4 +1,4 @@
1
- # Copyright 2015 Google Inc. All rights reserved.
1
+ # Copyright 2016 Google Inc. All rights reserved.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -16,9 +16,9 @@
16
16
  require "gcloud/errors"
17
17
 
18
18
  module Gcloud
19
- module Search
19
+ module Translate
20
20
  ##
21
- # Base Search exception class.
21
+ # Base Translate exception class.
22
22
  class Error < Gcloud::Error
23
23
  end
24
24
 
@@ -42,14 +42,11 @@ module Gcloud
42
42
 
43
43
  # @private
44
44
  def self.from_response resp
45
- data = JSON.parse resp.body
46
- if data["error"]
47
- from_response_data data["error"]
45
+ if resp.data? && resp.data["error"]
46
+ from_response_data resp.data["error"]
48
47
  else
49
48
  from_response_status resp
50
49
  end
51
- rescue JSON::ParserError
52
- from_response_status resp
53
50
  end
54
51
 
55
52
  # @private
@@ -60,10 +57,10 @@ module Gcloud
60
57
  # @private
61
58
  def self.from_response_status resp
62
59
  if resp.status == 404
63
- new "#{resp.body}: #{resp.request.uri.request_uri}",
60
+ new "#{resp.error_message}: #{resp.request.uri.request_uri}",
64
61
  resp.status
65
62
  else
66
- new resp.body, resp.status
63
+ new resp.error_message, resp.status
67
64
  end
68
65
  end
69
66
  end
@@ -0,0 +1,69 @@
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 Gcloud
17
+ module Translate
18
+ ##
19
+ # # Language
20
+ #
21
+ # Represents a supported languages query result. Returned by
22
+ # {Gcloud::Translate::Api#languages}.
23
+ #
24
+ # @see https://cloud.google.com/translate/v2/using_rest#supported-languages
25
+ # Discover Supported Languages
26
+ #
27
+ # @example
28
+ # require "gcloud"
29
+ #
30
+ # gcloud = Gcloud.new
31
+ # translate = gcloud.translate
32
+ #
33
+ # languages = translate.languages "en"
34
+ #
35
+ # languages.size #=> 104
36
+ # languages[0].code #=> "af"
37
+ # languages[0].name #=> "Afrikaans"
38
+ #
39
+ class Language
40
+ ##
41
+ # The language code. This is an [ISO
42
+ # 639-1](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes) language
43
+ # code.
44
+ #
45
+ # @return [String]
46
+ attr_reader :code
47
+
48
+ ##
49
+ # The localized name of the language, if available.
50
+ #
51
+ # @return [String]
52
+ attr_reader :name
53
+
54
+ ##
55
+ # @private Create a new object.
56
+ def initialize code, name
57
+ @code = code
58
+ @name = name
59
+ end
60
+
61
+ ##
62
+ # @private New Language from a LanguagesResource object as defined by the
63
+ # Google API Client object.
64
+ def self.from_gapi gapi
65
+ new gapi["language"], gapi["name"]
66
+ end
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,112 @@
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 Gcloud
17
+ module Translate
18
+ ##
19
+ # # Translation
20
+ #
21
+ # Represents a translation query result. Returned by
22
+ # {Gcloud::Translate::Api#translate}.
23
+ #
24
+ # @see https://cloud.google.com/translate/v2/using_rest#Translate Translate
25
+ # Text
26
+ #
27
+ # @example
28
+ # require "gcloud"
29
+ #
30
+ # gcloud = Gcloud.new
31
+ # translate = gcloud.translate
32
+ #
33
+ # translation = translate.translate "Hello world!", to: "la"
34
+ #
35
+ # puts translation #=> Salve mundi!
36
+ #
37
+ # translation.from #=> "en"
38
+ # translation.origin #=> "Hello world!"
39
+ # translation.to #=> "la"
40
+ # translation.text #=> "Salve mundi!"
41
+ #
42
+ class Translation
43
+ ##
44
+ # The translated result.
45
+ #
46
+ # @return [String]
47
+ attr_reader :text
48
+ alias_method :to_s, :text
49
+ alias_method :to_str, :text
50
+
51
+ ##
52
+ # The original query text that was translated.
53
+ #
54
+ # @return [String]
55
+ attr_reader :origin
56
+
57
+ ##
58
+ # The target language into which the text was translated.
59
+ #
60
+ # @return [String]
61
+ attr_reader :to
62
+ alias_method :language, :to
63
+ alias_method :target, :to
64
+
65
+ ##
66
+ # The source language from which the text was translated.
67
+ attr_reader :from
68
+ alias_method :source, :from
69
+
70
+ ##
71
+ # @private Create a new object.
72
+ def initialize text, to, origin, from, detected
73
+ @text = text
74
+ @to = to
75
+ @origin = origin
76
+ @from = from
77
+ @detected = detected
78
+ end
79
+
80
+ ##
81
+ # Determines if the source language was detected by the Google Cloud
82
+ # Translate API.
83
+ #
84
+ # @return [Boolean] `true` if the source language was detected by the
85
+ # Translate service, `false` if the source language was provided in the
86
+ # request
87
+ def detected?
88
+ @detected
89
+ end
90
+
91
+ ##
92
+ # @private New Translation from a TranslationsListResponse object as
93
+ # defined by the Google API Client object.
94
+ def self.from_response resp, text, to, from
95
+ res = text.zip(Array(resp.data["translations"])).map do |origin, gapi|
96
+ from_gapi gapi, to, origin, from
97
+ end
98
+ return res.first if res.size == 1
99
+ res
100
+ end
101
+
102
+ ##
103
+ # @private New Translation from a TranslationsResource object as defined
104
+ # by the Google API Client object.
105
+ def self.from_gapi gapi, to, origin, from
106
+ from ||= gapi["detectedSourceLanguage"]
107
+ detected = !gapi["detectedSourceLanguage"].nil?
108
+ new gapi["translatedText"], to, origin, from, detected
109
+ end
110
+ end
111
+ end
112
+ end
@@ -14,5 +14,5 @@
14
14
 
15
15
 
16
16
  module Gcloud
17
- VERSION = "0.7.2"
17
+ VERSION = "0.8.0"
18
18
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gcloud
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.2
4
+ version: 0.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Silvano Luciani
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2016-04-02 00:00:00.000000000 Z
13
+ date: 2016-04-28 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: grpc
@@ -354,21 +354,6 @@ files:
354
354
  - lib/gcloud/resource_manager/project.rb
355
355
  - lib/gcloud/resource_manager/project/list.rb
356
356
  - lib/gcloud/resource_manager/project/updater.rb
357
- - lib/gcloud/search.rb
358
- - lib/gcloud/search/api_client.rb
359
- - lib/gcloud/search/connection.rb
360
- - lib/gcloud/search/credentials.rb
361
- - lib/gcloud/search/document.rb
362
- - lib/gcloud/search/document/list.rb
363
- - lib/gcloud/search/errors.rb
364
- - lib/gcloud/search/field_value.rb
365
- - lib/gcloud/search/field_values.rb
366
- - lib/gcloud/search/fields.rb
367
- - lib/gcloud/search/index.rb
368
- - lib/gcloud/search/index/list.rb
369
- - lib/gcloud/search/project.rb
370
- - lib/gcloud/search/result.rb
371
- - lib/gcloud/search/result/list.rb
372
357
  - lib/gcloud/storage.rb
373
358
  - lib/gcloud/storage/bucket.rb
374
359
  - lib/gcloud/storage/bucket/acl.rb
@@ -382,6 +367,13 @@ files:
382
367
  - lib/gcloud/storage/file/list.rb
383
368
  - lib/gcloud/storage/file/verifier.rb
384
369
  - lib/gcloud/storage/project.rb
370
+ - lib/gcloud/translate.rb
371
+ - lib/gcloud/translate/api.rb
372
+ - lib/gcloud/translate/connection.rb
373
+ - lib/gcloud/translate/detection.rb
374
+ - lib/gcloud/translate/errors.rb
375
+ - lib/gcloud/translate/language.rb
376
+ - lib/gcloud/translate/translation.rb
385
377
  - lib/gcloud/upload.rb
386
378
  - lib/gcloud/version.rb
387
379
  - lib/google/api/annotations.rb
data/lib/gcloud/search.rb DELETED
@@ -1,318 +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"
17
- require "gcloud/search/project"
18
-
19
- module Gcloud
20
- ##
21
- # Creates a new `Project` instance connected to the Search service.
22
- # Each call creates a new connection.
23
- #
24
- # For more information on connecting to Google Cloud see the [Authentication
25
- # Guide](https://googlecloudplatform.github.io/gcloud-ruby/#/docs/guides/authentication).
26
- #
27
- # @param [String] project Identifier for a Search project. If not present, the
28
- # default project for the credentials is used.
29
- # @param [String, Hash] keyfile Keyfile downloaded from Google Cloud. If file
30
- # path the file must be readable.
31
- # @param [String, Array<String>] scope The OAuth 2.0 scopes controlling the
32
- # set of resources and operations that the connection can access. See [Using
33
- # OAuth 2.0 to Access Google
34
- # APIs](https://developers.google.com/identity/protocols/OAuth2).
35
- #
36
- # The default scopes are:
37
- #
38
- # * `https://www.googleapis.com/auth/cloudsearch`
39
- # * `https://www.googleapis.com/auth/userinfo.email`
40
- #
41
- # @return [Gcloud::Search::Project]
42
- #
43
- def self.search project = nil, keyfile = nil, scope: nil
44
- project ||= Gcloud::Search::Project.default_project
45
- if keyfile.nil?
46
- credentials = Gcloud::Search::Credentials.default scope: scope
47
- else
48
- credentials = Gcloud::Search::Credentials.new keyfile, scope: scope
49
- end
50
- Gcloud::Search::Project.new project, credentials
51
- end
52
-
53
- # rubocop:disable Metrics/LineLength
54
- # Disabled because there are links in the docs that are long.
55
-
56
- ##
57
- # # Google Cloud Search
58
- #
59
- # Google Cloud Search allows an application to quickly perform full-text and
60
- # geo-spatial searches without having to spin up instances and without the
61
- # hassle of managing and maintaining a search service.
62
- #
63
- # Cloud Search provides a model for indexing documents containing structured
64
- # data, with documents and indexes saved to a separate persistent store
65
- # optimized for search operations. The API supports full text matching on
66
- # string fields and allows indexing any number of documents in any number of
67
- # indexes.
68
- #
69
- # The Cloud Search API is an Alpha release, and might be changed in
70
- # backward-incompatible ways. It is not currently recommended for production
71
- # use. It is not subject to any SLA or deprecation policy.
72
- #
73
- # ## Accessing the Service
74
- #
75
- # Currently, the Cloud Search API is available only to white-listed users.
76
- # Contact your account manager or a member of the Google Cloud sales team if
77
- # you are interested in access.
78
- #
79
- # ## Authentication
80
- #
81
- # Authentication is handled by {Gcloud#search}. You can provide the project and
82
- # credential information to connect to the Cloud Search service, or if you are
83
- # running on Google Compute Engine this configuration is taken care of for
84
- # you. You can read more about the options for connecting in the
85
- # [Authentication
86
- # Guide](https://googlecloudplatform.github.io/gcloud-ruby/#/docs/guides/authentication).
87
- #
88
- # ## Managing Indexes
89
- #
90
- # An Index is a searchable collection of documents that belongs to a Project.
91
- #
92
- # You can list the indexes in your current project:
93
- #
94
- # ```ruby
95
- # require "gcloud"
96
- #
97
- # gcloud = Gcloud.new
98
- # search = gcloud.search
99
- #
100
- # indexes = search.indexes # API call
101
- # indexes.each do |index|
102
- # puts index.index_id
103
- # end
104
- # ```
105
- #
106
- # And you can use the project to create new indexes:
107
- #
108
- # ```ruby
109
- # require "gcloud"
110
- #
111
- # gcloud = Gcloud.new
112
- # search = gcloud.search
113
- #
114
- # index = search.index "products", skip_lookup: true
115
- # ```
116
- #
117
- # A new index is an unsaved value object. Indexes cannot be created,
118
- # updated, or deleted directly in the service: They are derived from the
119
- # documents which are created "within" them. A new index will exist in the
120
- # service once you save a document that references it.
121
- #
122
- # ## Managing Documents
123
- #
124
- # Using an index, create a new, unsaved Document instance, providing
125
- # your own unique document ID, as shown below, or omitting this argument to
126
- # let the service assign the ID.
127
- #
128
- # ```ruby
129
- # require "gcloud"
130
- #
131
- # gcloud = Gcloud.new
132
- # search = gcloud.search
133
- #
134
- # index = search.index "products"
135
- # document = index.document "product-sku-000001"
136
- # index.find document # API call
137
- # #=> nil
138
- # document.rank #=> nil
139
- # ```
140
- #
141
- # Add one or more fields to the document. (See [Adding document fields](#module-Gcloud::Search-label-Adding`document`fields), below.)
142
- #
143
- # ```ruby
144
- # document.add "price", 24.95
145
- # ```
146
- #
147
- # When your document is complete, save it:
148
- #
149
- # ```ruby
150
- # index.save document # API call
151
- # document.rank # set by the server
152
- # #=> 1443648166
153
- # ```
154
- #
155
- # You can list the documents in an index:
156
- #
157
- # ```ruby
158
- # require "gcloud"
159
- #
160
- # gcloud = Gcloud.new
161
- # search = gcloud.search
162
- # index = search.index "products"
163
- #
164
- # documents = index.documents # API call
165
- # documents.map &:doc_id #=> ["product-sku-000001"]
166
- # ```
167
- #
168
- # And you can delete documents:
169
- #
170
- # ```ruby
171
- # require "gcloud"
172
- #
173
- # gcloud = Gcloud.new
174
- # search = gcloud.search
175
- # index = search.index "products"
176
- #
177
- # document = index.find "product-sku-000001"
178
- #
179
- # document.delete # API call
180
- # index.find document # API call
181
- # #=> nil
182
- # ```
183
- #
184
- # To update a document after manipulating its fields or rank, just re-save it:
185
- #
186
- # ```ruby
187
- # require "gcloud"
188
- #
189
- # gcloud = Gcloud.new
190
- # search = gcloud.search
191
- # index = search.index "products"
192
- #
193
- # document = index.find "product-sku-000001"
194
- #
195
- # document.rank = 12345
196
- # document.add "price", 9.95 # replace existing number value
197
- # index.save document # API call
198
- # ```
199
- #
200
- # ## Adding document fields
201
- #
202
- # Fields belong to documents and are the data that actually gets searched.
203
- # Each field has a FieldValues collection, which facilitates access to
204
- # FieldValue objects. Each FieldValue object will be saved as one of the
205
- # [Cloud Search types](https://cloud.google.com/search/documents_indexes#document_fields_field_names_and_multi-valued_fields).
206
- # The type will be inferred from the value when possible, or you can
207
- # explicitly specify it by passing a symbol with the `type` option to
208
- # {Gcloud::Search::Document#add}.
209
- #
210
- # - String (`:atom`, `:html`, `:text`, or `:default`)
211
- # - Number (`:number`)
212
- # - Timestamp (`:datetime`)
213
- # - Geovalue (`:geo`)
214
- #
215
- # String values can be tokenized using one of three different types of
216
- # tokenization, which can be passed with the `type` option when the value is
217
- # added:
218
- #
219
- # - `:atom` means "don't tokenize this string", treat it as one
220
- # thing to compare against
221
- #
222
- # - `:html` means "treat this string as HTML", not comparing against the
223
- # tags, and treating the rest of the content like `:text`
224
- #
225
- # - `:text` means "treat this string as normal text" and split words
226
- # apart to be compared against
227
- #
228
- # Again, you can add more than one value to a field, and the values may be of
229
- # different types.
230
- #
231
- # ```ruby
232
- # require "gcloud"
233
- #
234
- # gcloud = Gcloud.new
235
- # search = gcloud.search
236
- #
237
- # index = search.index "products"
238
- # document = index.find "product-sku-000001"
239
- # document.add "description", "The best T-shirt ever.", type: :text, lang: "en"
240
- # document.add "description", "<p>The best T-shirt ever.</p>", type: :html, lang: "en"
241
- # document["description"].size #=> 2
242
- # ```
243
- #
244
- # ## Searching
245
- #
246
- # After populating an index with documents, you can request search results
247
- # with a query:
248
- #
249
- # ```ruby
250
- # require "gcloud"
251
- #
252
- # gcloud = Gcloud.new
253
- # search = gcloud.search
254
- # index = search.index "books"
255
- #
256
- # results = index.search "dark stormy"
257
- # results.each do |result|
258
- # puts result.doc_id
259
- # end
260
- # ```
261
- #
262
- # By default, Result objects are sorted by document rank. For more information
263
- # see the [REST API documentation for Document.rank](https://cloud.google.com/search/reference/rest/v1/projects/indexes/documents#resource_representation.google.cloudsearch.v1.Document.rank).
264
- #
265
- # You can specify how to sort results with the `order` option:
266
- #
267
- # ```ruby
268
- # require "gcloud"
269
- #
270
- # gcloud = Gcloud.new
271
- # search = gcloud.search
272
- # index = search.index "books"
273
- #
274
- # results = index.search "dark stormy", order: "published, avg_review desc"
275
- # documents = index.search query # API call
276
- # ```
277
- #
278
- # You can add computed fields with the `expressions` option, and specify the
279
- # fields that are returned with the `fields` option. No document data will be
280
- # returned if you omit the `fields` option, only `doc_id` references to any
281
- # matched documents.
282
- #
283
- # ```ruby
284
- # require "gcloud"
285
- #
286
- # gcloud = Gcloud.new
287
- # search = gcloud.search
288
- # index = search.index "products"
289
- #
290
- # results = index.search "cotton T-shirt",
291
- # expressions: { total_price: "(price + tax)" },
292
- # fields: ["name", "total_price", "highlight"]
293
- # ```
294
- #
295
- # Just as in documents, Result data is accessible via Fields methods:
296
- #
297
- # ```ruby
298
- # require "gcloud"
299
- #
300
- # gcloud = Gcloud.new
301
- # search = gcloud.search
302
- # index = search.index "products"
303
- # document = index.find "product-sku-000001"
304
- # results = index.search "cotton T-shirt"
305
- # values = results[0]["description"]
306
- #
307
- # values[0] #=> "100% organic cotton ruby gem T-shirt"
308
- # values[0].type #=> :text
309
- # values[0].lang #=> "en"
310
- # values[1] #=> "<p>100% organic cotton ruby gem T-shirt</p>"
311
- # values[1].type #=> :html
312
- # values[1].lang #=> "en"
313
- # ```
314
- #
315
- module Search
316
- end
317
- # rubocop:enable Metrics/LineLength
318
- end