From 97b97e96c3fdc3fb6d2df442ddffad81a209016b Mon Sep 17 00:00:00 2001 From: Jason Dobry Date: Wed, 14 Sep 2016 08:48:24 -0700 Subject: [PATCH 01/44] Add "quickstart" samples. --- bigquery/quickstart.rb | 39 +++++++++++++++++++++++++++++++++++ datastore/quickstart.rb | 44 ++++++++++++++++++++++++++++++++++++++++ language/quickstart.rb | 41 +++++++++++++++++++++++++++++++++++++ logging/quickstart.rb | 45 +++++++++++++++++++++++++++++++++++++++++ pubsub/quickstart.rb | 39 +++++++++++++++++++++++++++++++++++ speech/Gemfile | 1 + speech/Gemfile.lock | 20 ++++++++++++++++++ speech/quickstart.rb | 44 ++++++++++++++++++++++++++++++++++++++++ storage/quickstart.rb | 39 +++++++++++++++++++++++++++++++++++ translate/quickstart.rb | 42 ++++++++++++++++++++++++++++++++++++++ vision/quickstart.rb | 42 ++++++++++++++++++++++++++++++++++++++ 11 files changed, 396 insertions(+) create mode 100644 bigquery/quickstart.rb create mode 100644 datastore/quickstart.rb create mode 100644 language/quickstart.rb create mode 100644 logging/quickstart.rb create mode 100644 pubsub/quickstart.rb create mode 100644 speech/quickstart.rb create mode 100644 storage/quickstart.rb create mode 100644 translate/quickstart.rb create mode 100644 vision/quickstart.rb diff --git a/bigquery/quickstart.rb b/bigquery/quickstart.rb new file mode 100644 index 000000000..1d430b131 --- /dev/null +++ b/bigquery/quickstart.rb @@ -0,0 +1,39 @@ +# Copyright 2016 Google, Inc +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +def run_quickstart + # [START bigquery_quickstart] + # Imports the Google Cloud client library + require "google/cloud" + + # Your Google Cloud Platform project ID + project_id = "YOUR_PROJECT_ID" + + # Instantiates a client + gcloud = Google::Cloud.new project_id + bigquery_client = gcloud.bigquery + + # The name for the new dataset + dataset_name = "my_new_dataset" + + # Creates the new dataset + dataset = bigquery_client.create_dataset dataset_name + + puts "Dataset #{dataset.dataset_id} created." + # [END bigquery_quickstart] +end + +if __FILE__ == $PROGRAM_NAME + run_quickstart +end diff --git a/datastore/quickstart.rb b/datastore/quickstart.rb new file mode 100644 index 000000000..ff9e26a2a --- /dev/null +++ b/datastore/quickstart.rb @@ -0,0 +1,44 @@ +# Copyright 2016 Google, Inc +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +def run_quickstart + # [START datastore_quickstart] + # Imports the Google Cloud client library + require "google/cloud" + + # Your Google Cloud Platform project ID + project_id = "YOUR_PROJECT_ID" + + # Instantiates a client + gcloud = Google::Cloud.new project_id + datastore_client = gcloud.datastore + + # The kind of the entity to retrieve + kind = "Task" + # The id of the entity to retrieve + id = 1234567890 + # The Datastore key for the entity + task_key = datastore_client.key kind, id + + # Retrieves the entity + entity = datastore_client.find task_key + + puts "Got entity: #{entity.key.id}" + # [END datastore_quickstart] +end + +if __FILE__ == $PROGRAM_NAME + run_quickstart +end + diff --git a/language/quickstart.rb b/language/quickstart.rb new file mode 100644 index 000000000..39ad20603 --- /dev/null +++ b/language/quickstart.rb @@ -0,0 +1,41 @@ +# Copyright 2016 Google, Inc +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +def run_quickstart + # [START language_quickstart] + # Imports the Google Cloud client library + require "google/cloud" + + # Your Google Cloud Platform project ID + project_id = "YOUR_PROJECT_ID" + + # Instantiates a client + gcloud = Google::Cloud.new project_id + language_client = gcloud.language + + # The text to analyze + text = "Hello, world!" + document = language_client.document text + + # Detects the sentiment of the text + sentiment = document.sentiment + + puts "Text: #{text}" + puts "Sentiment: #{sentiment.polarity}, #{sentiment.magnitude}" + # [END language_quickstart] +end + +if __FILE__ == $PROGRAM_NAME + run_quickstart +end diff --git a/logging/quickstart.rb b/logging/quickstart.rb new file mode 100644 index 000000000..a94422422 --- /dev/null +++ b/logging/quickstart.rb @@ -0,0 +1,45 @@ +# Copyright 2016 Google, Inc +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +def run_quickstart + # [START logging_quickstart] + # Imports the Google Cloud client library + require "google/cloud" + + # Your Google Cloud Platform project ID + project_id = "YOUR_PROJECT_ID" + + # Instantiates a client + gcloud = Google::Cloud.new project_id + logging_client = gcloud.logging + + # Prepares a log entry + entry = logging.entry + # The data to log + entry.payload = "Hello, world!" + # The name of the log to write to + entry.log_name = "my-log" + # The resource associated with the data + entry.resource.type = "global" + + # Writes the log entry + logging_client.write_entries entry + + puts "Logged #{entry.payload}" + # [END logging_quickstart] +end + +if __FILE__ == $PROGRAM_NAME + run_quickstart +end diff --git a/pubsub/quickstart.rb b/pubsub/quickstart.rb new file mode 100644 index 000000000..2ac1f774a --- /dev/null +++ b/pubsub/quickstart.rb @@ -0,0 +1,39 @@ +# Copyright 2016 Google, Inc +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +def run_quickstart + # [START pubsub_quickstart] + # Imports the Google Cloud client library + require "google/cloud" + + # Your Google Cloud Platform project ID + project_id = "YOUR_PROJECT_ID" + + # Instantiates a client + gcloud = Google::Cloud.new project_id + pubsub_client = gcloud.pubsub + + # The name for the new topic + topic_name = "my-new-topic" + + # Creates the new topic + topic = pubsub_client.create_topic topic_name + + puts "Topic #{topic.name} created." + # [END pubsub_quickstart] +end + +if __FILE__ == $PROGRAM_NAME + run_quickstart +end diff --git a/speech/Gemfile b/speech/Gemfile index f79259a05..f8a2b8eb2 100644 --- a/speech/Gemfile +++ b/speech/Gemfile @@ -15,6 +15,7 @@ source "https://rubygems.org" gem "google-api-client" +gem "google-cloud-speech" group :test do gem "rspec" diff --git a/speech/Gemfile.lock b/speech/Gemfile.lock index 1ade9ad53..4e9efb528 100644 --- a/speech/Gemfile.lock +++ b/speech/Gemfile.lock @@ -14,6 +14,21 @@ GEM mime-types (>= 1.6) representable (~> 2.3.0) retriable (~> 2.0) + google-cloud-core (0.20.1) + google-cloud-speech (0.20.0) + google-cloud-core (~> 0.20.0) + google-gax (~> 0.5.0) + google-protobuf (~> 3.0) + googleapis-common-protos (~> 1.3) + grpc (~> 1.0) + google-gax (0.5.1) + googleauth (~> 0.5.1) + grpc (~> 1.0) + rly (~> 0.2.3) + google-protobuf (3.1.0) + googleapis-common-protos (1.3.4) + google-protobuf (~> 3.0) + grpc (~> 1.0) googleauth (0.5.1) faraday (~> 0.9) jwt (~> 1.4) @@ -22,6 +37,9 @@ GEM multi_json (~> 1.11) os (~> 0.9) signet (~> 0.7) + grpc (1.0.0) + google-protobuf (~> 3.0) + googleauth (~> 0.5.1) httpclient (2.8.2.4) hurley (0.2) jwt (1.5.6) @@ -39,6 +57,7 @@ GEM representable (2.3.0) uber (~> 0.0.7) retriable (2.1.0) + rly (0.2.3) rspec (3.5.0) rspec-core (~> 3.5.0) rspec-expectations (~> 3.5.0) @@ -64,6 +83,7 @@ PLATFORMS DEPENDENCIES google-api-client + google-cloud-speech rspec BUNDLED WITH diff --git a/speech/quickstart.rb b/speech/quickstart.rb new file mode 100644 index 000000000..47c36691f --- /dev/null +++ b/speech/quickstart.rb @@ -0,0 +1,44 @@ +# Copyright 2016 Google, Inc +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +def run_quickstart + # [START speech_quickstart] + # Imports the Google Cloud client library + require "google/cloud" + + # Your Google Cloud Platform project ID + project_id = "YOUR_PROJECT_ID" + + # Instantiates a client + gcloud = Google::Cloud.new project_id + speech_client = gcloud.speech + + # The name of the audio file to transcribe + fileName = "./audio_files/audio.raw" + + # The audio file's encoding and sample rate + audio = speech_client.audio fileName, + encoding: :raw, sample_rate: 16000 + + # Detects speech in the audio file + results = audio.recognize + result = results.first + + puts "Transcription: #{result.transcript}" + # [END speech_quickstart] +end + +if __FILE__ == $PROGRAM_NAME + run_quickstart +end diff --git a/storage/quickstart.rb b/storage/quickstart.rb new file mode 100644 index 000000000..b528b4e1f --- /dev/null +++ b/storage/quickstart.rb @@ -0,0 +1,39 @@ +# Copyright 2016 Google, Inc +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +def run_quickstart + # [START storage_quickstart] + # Imports the Google Cloud client library + require "google/cloud" + + # Your Google Cloud Platform project ID + project_id = "YOUR_PROJECT_ID" + + # Instantiates a client + gcloud = Google::Cloud.new project_id + storage_client = gcloud.storage + + # The name for the new bucket + bucket_name = "my-new-bucket" + + # Creates the new bucket + bucket = storage_client.create_bucket bucket_name + + puts "Bucket #{bucket.name} created." + # [END storage_quickstart] +end + +if __FILE__ == $PROGRAM_NAME + run_quickstart +end diff --git a/translate/quickstart.rb b/translate/quickstart.rb new file mode 100644 index 000000000..dfdb165a0 --- /dev/null +++ b/translate/quickstart.rb @@ -0,0 +1,42 @@ +# Copyright 2016 Google, Inc +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +def run_quickstart + # [START translate_quickstart] + # Imports the Google Cloud client library + require "google/cloud" + + # Your Translate API key + api_key = "YOUR_API_KEY" + + # Instantiates a client + gcloud = Google::Cloud.new + translate_client = gcloud.translate api_key + + # The text to translate + text = "Hello, world!" + # The target language + target = "ru" + + # Translates some text into Russian + translation = translate_client.translate text, to: target + + puts "Text: #{text}" + puts "Translation: #{translation}" + # [END translate_quickstart] +end + +if __FILE__ == $PROGRAM_NAME + run_quickstart +end diff --git a/vision/quickstart.rb b/vision/quickstart.rb new file mode 100644 index 000000000..f373cf98d --- /dev/null +++ b/vision/quickstart.rb @@ -0,0 +1,42 @@ +# Copyright 2016 Google, Inc +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +def run_quickstart + # [START vision_quickstart] + # Imports the Google Cloud client library + require "google/cloud" + + # Your Google Cloud Platform project ID + project_id = "YOUR_PROJECT_ID" + + # Instantiates a client + gcloud = Google::Cloud.new project_id + vision_client = gcloud.vision + + # The name of the image file to annotate + fileName = "./images/cat.jpg" + + # Performs label detection on the image file + labels = vision_client.image(fileName).labels + + puts "Labels:" + labels.each do |label| + puts label + end + # [END vision_quickstart] +end + +if __FILE__ == $PROGRAM_NAME + run_quickstart +end From beab1348905acbb922fd41dabc04991098701899 Mon Sep 17 00:00:00 2001 From: Jason Dobry Date: Tue, 4 Oct 2016 19:41:21 -0700 Subject: [PATCH 02/44] Tweak Datastore quickstart sample. --- datastore/quickstart.rb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/datastore/quickstart.rb b/datastore/quickstart.rb index ff9e26a2a..801e77f02 100644 --- a/datastore/quickstart.rb +++ b/datastore/quickstart.rb @@ -25,16 +25,16 @@ def run_quickstart datastore_client = gcloud.datastore # The kind of the entity to retrieve - kind = "Task" - # The id of the entity to retrieve - id = 1234567890 + kind = "Person" + # The name/ID of the entity to retrieve + name = "Bob" # The Datastore key for the entity - task_key = datastore_client.key kind, id + task_key = datastore_client.key kind, name # Retrieves the entity entity = datastore_client.find task_key - puts "Got entity: #{entity.key.id}" + puts "Fetched entity: #{entity.key.name}" # [END datastore_quickstart] end From 318d761b58abd6f9924ab4063f323a7e2d005e3b Mon Sep 17 00:00:00 2001 From: Jason Dobry Date: Wed, 5 Oct 2016 15:16:27 -0700 Subject: [PATCH 03/44] Tweak Datastore quickstart sample. --- datastore/quickstart.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/datastore/quickstart.rb b/datastore/quickstart.rb index 801e77f02..d7cdbe498 100644 --- a/datastore/quickstart.rb +++ b/datastore/quickstart.rb @@ -25,16 +25,16 @@ def run_quickstart datastore_client = gcloud.datastore # The kind of the entity to retrieve - kind = "Person" + kind = "Task" # The name/ID of the entity to retrieve - name = "Bob" + name = "sampletask1" # The Datastore key for the entity task_key = datastore_client.key kind, name # Retrieves the entity - entity = datastore_client.find task_key + task = datastore_client.find task_key - puts "Fetched entity: #{entity.key.name}" + puts "Fetched task: #{task.key.name}" # [END datastore_quickstart] end From bb13651979325c933a2c348f47b41403eaf020fc Mon Sep 17 00:00:00 2001 From: Jason Dobry Date: Thu, 6 Oct 2016 15:01:44 -0700 Subject: [PATCH 04/44] Change datastore quickstart sample to create a task. --- datastore/quickstart.rb | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/datastore/quickstart.rb b/datastore/quickstart.rb index d7cdbe498..3fbe6b2bc 100644 --- a/datastore/quickstart.rb +++ b/datastore/quickstart.rb @@ -24,21 +24,25 @@ def run_quickstart gcloud = Google::Cloud.new project_id datastore_client = gcloud.datastore - # The kind of the entity to retrieve + # The kind for the new entity kind = "Task" - # The name/ID of the entity to retrieve + # The name/ID for the new entity name = "sampletask1" - # The Datastore key for the entity + # The Cloud Datastore key for the new entity task_key = datastore_client.key kind, name - # Retrieves the entity - task = datastore_client.find task_key + # Prepares the new entity + task = datastore_client.entity task_key do |t| + t["description"] = "Buy milk" + end - puts "Fetched task: #{task.key.name}" + # Saves the entity + datastore_client.save task + + puts "Saved #{task.key.name}: #{task['description']}" # [END datastore_quickstart] end if __FILE__ == $PROGRAM_NAME run_quickstart end - From a44f36269be85232d895ec1c438b7ab483a6b2a2 Mon Sep 17 00:00:00 2001 From: Frank Natividad Date: Thu, 6 Oct 2016 16:06:28 -0700 Subject: [PATCH 05/44] Changes: 1. Added test for BigQuery quickstart 2. Updated information about quickstart in README.md --- bigquery/README.md | 12 +++++++ bigquery/spec/quickstart_spec.rb | 58 ++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 bigquery/spec/quickstart_spec.rb diff --git a/bigquery/README.md b/bigquery/README.md index ef15ce784..65d561524 100644 --- a/bigquery/README.md +++ b/bigquery/README.md @@ -9,6 +9,13 @@ analytics data warehouse. ## Samples +### Quickstart + +''' + quickstart.rb provides a literal quick start using BigQuery +''' + + ### Datasets ``` @@ -36,3 +43,8 @@ Commands: query query_job ``` + +### Run all tests +``` +$ bundle exec rspec +``` diff --git a/bigquery/spec/quickstart_spec.rb b/bigquery/spec/quickstart_spec.rb new file mode 100644 index 000000000..d68c4f6cc --- /dev/null +++ b/bigquery/spec/quickstart_spec.rb @@ -0,0 +1,58 @@ +# Copyright 2016 Google, Inc +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +require_relative "../quickstart" +require "google/cloud" + +describe "Quickstart" do + # Initialize dataset_name + DATASET_NAME = 'dataset_name' + + # Setup 'before' RSpec hook + before :all do + @gcloud = Google::Cloud.new ENV['GOOGLE_PROJECT_ID'] + @bigquery = @gcloud.bigquery + @dataset = @bigquery.create_dataset DATASET_NAME + end + + # Setup 'after' RSpec hook + RSpec.configure do |config| + config.after do + cleanup! + end + end + + # Cleanup! + def cleanup! + # Delete dataset + @dataset.delete + end + + + # Test Quickstart sample + it 'creates a new dataset in BigQuery' do + # Setup expectations for Google::Cloud + expect(Google::Cloud).to receive(:new).with("YOUR_PROJECT_ID").and_return(@gcloud) + + # Setup expectations for @gcloud + expect(@gcloud).to receive(:bigquery).and_return(@bigquery) + + # Setup expectations for @bigquery + expect(@bigquery).to receive(:create_dataset).with("my_new_dataset").and_return(@dataset) + + # Check output + expect{ run_quickstart }.to output(/#{DATASET_NAME}/).to_stdout + end +end + From 48b30feeecbd4aac827706205e4c323b30721d76 Mon Sep 17 00:00:00 2001 From: Frank Natividad Date: Thu, 6 Oct 2016 16:23:17 -0700 Subject: [PATCH 06/44] Changes: 1. Removed unecessary space in bigquery quickstart spec --- bigquery/spec/quickstart_spec.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/bigquery/spec/quickstart_spec.rb b/bigquery/spec/quickstart_spec.rb index d68c4f6cc..0e7e834b2 100644 --- a/bigquery/spec/quickstart_spec.rb +++ b/bigquery/spec/quickstart_spec.rb @@ -39,7 +39,6 @@ def cleanup! @dataset.delete end - # Test Quickstart sample it 'creates a new dataset in BigQuery' do # Setup expectations for Google::Cloud From 4bcdf07b66f394c23ee29a6d11dc03aa28cf16ad Mon Sep 17 00:00:00 2001 From: Frank Natividad Date: Fri, 7 Oct 2016 16:23:36 -0700 Subject: [PATCH 07/44] Changes: 1. Updated quickstart.rb to be a global set of instructions 2. Updated spec/quickstart_spec.rb to be a minimal test. (There's only one function to test.) --- bigquery/README.md | 11 ------- bigquery/quickstart.rb | 33 ++++++++----------- bigquery/spec/quickstart_spec.rb | 55 ++++++++++++++------------------ 3 files changed, 38 insertions(+), 61 deletions(-) diff --git a/bigquery/README.md b/bigquery/README.md index 65d561524..0cc7c985d 100644 --- a/bigquery/README.md +++ b/bigquery/README.md @@ -9,13 +9,6 @@ analytics data warehouse. ## Samples -### Quickstart - -''' - quickstart.rb provides a literal quick start using BigQuery -''' - - ### Datasets ``` @@ -44,7 +37,3 @@ Commands: query_job ``` -### Run all tests -``` -$ bundle exec rspec -``` diff --git a/bigquery/quickstart.rb b/bigquery/quickstart.rb index 1d430b131..79fbdec7c 100644 --- a/bigquery/quickstart.rb +++ b/bigquery/quickstart.rb @@ -12,28 +12,23 @@ # See the License for the specific language governing permissions and # limitations under the License. -def run_quickstart - # [START bigquery_quickstart] - # Imports the Google Cloud client library - require "google/cloud" +# [START bigquery_quickstart] +# Imports the Google Cloud client library +require "google/cloud" - # Your Google Cloud Platform project ID - project_id = "YOUR_PROJECT_ID" +# Your Google Cloud Platform project ID +project_id = "YOUR_PROJECT_ID" - # Instantiates a client - gcloud = Google::Cloud.new project_id - bigquery_client = gcloud.bigquery +# Instantiates a client +gcloud = Google::Cloud.new project_id +bigquery_client = gcloud.bigquery - # The name for the new dataset - dataset_name = "my_new_dataset" +# The name for the new dataset +dataset_name = "my_new_dataset" - # Creates the new dataset - dataset = bigquery_client.create_dataset dataset_name +# Creates the new dataset +dataset = bigquery_client.create_dataset dataset_name - puts "Dataset #{dataset.dataset_id} created." - # [END bigquery_quickstart] -end +puts "Dataset #{dataset.dataset_id} created." +# [END bigquery_quickstart] -if __FILE__ == $PROGRAM_NAME - run_quickstart -end diff --git a/bigquery/spec/quickstart_spec.rb b/bigquery/spec/quickstart_spec.rb index 0e7e834b2..6ac9b0a5d 100644 --- a/bigquery/spec/quickstart_spec.rb +++ b/bigquery/spec/quickstart_spec.rb @@ -12,46 +12,39 @@ # See the License for the specific language governing permissions and # limitations under the License. -require_relative "../quickstart" +require "rspec" require "google/cloud" -describe "Quickstart" do - # Initialize dataset_name - DATASET_NAME = 'dataset_name' +describe "BigQuery Quickstart" do - # Setup 'before' RSpec hook - before :all do - @gcloud = Google::Cloud.new ENV['GOOGLE_PROJECT_ID'] - @bigquery = @gcloud.bigquery - @dataset = @bigquery.create_dataset DATASET_NAME - end + it 'creates a new dataset' do + # Initialize test objects + gcloud_test_client = Google::Cloud.new ENV['GOOGLE_CLOUD_PROJECT'] + bigquery_test_client = gcloud_test_client.bigquery - # Setup 'after' RSpec hook - RSpec.configure do |config| - config.after do - cleanup! + # Prime BigQuery for test + if bigquery_test_client.dataset "my_new_dataset" + bigquery_test_client.dataset("my_new_dataset").delete end - end - # Cleanup! - def cleanup! - # Delete dataset - @dataset.delete - end + expect(bigquery_test_client.dataset "my_new_dataset").to be nil + expect(Google::Cloud).to receive(:new). + with("YOUR_PROJECT_ID"). + and_return(gcloud_test_client) - # Test Quickstart sample - it 'creates a new dataset in BigQuery' do - # Setup expectations for Google::Cloud - expect(Google::Cloud).to receive(:new).with("YOUR_PROJECT_ID").and_return(@gcloud) + # Run quickstart + expect { + load File::expand_path("quickstart.rb") + }.to output( + "Dataset my_new_dataset created\.\n" + ).to_stdout - # Setup expectations for @gcloud - expect(@gcloud).to receive(:bigquery).and_return(@bigquery) + expect(bigquery_test_client.dataset "my_new_dataset").not_to be nil - # Setup expectations for @bigquery - expect(@bigquery).to receive(:create_dataset).with("my_new_dataset").and_return(@dataset) - - # Check output - expect{ run_quickstart }.to output(/#{DATASET_NAME}/).to_stdout + # Clean up + dataset = bigquery_test_client.dataset "my_new_dataset" + dataset.delete end + end From 4f1b02c07500e2b428aca67d7ca9c2e0111e938a Mon Sep 17 00:00:00 2001 From: Frank Natividad Date: Fri, 7 Oct 2016 16:28:26 -0700 Subject: [PATCH 08/44] Changes: 1. Substituated single quotes for double quotes. --- bigquery/spec/quickstart_spec.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bigquery/spec/quickstart_spec.rb b/bigquery/spec/quickstart_spec.rb index 6ac9b0a5d..706e45a2b 100644 --- a/bigquery/spec/quickstart_spec.rb +++ b/bigquery/spec/quickstart_spec.rb @@ -17,9 +17,9 @@ describe "BigQuery Quickstart" do - it 'creates a new dataset' do + it "creates a new dataset" do # Initialize test objects - gcloud_test_client = Google::Cloud.new ENV['GOOGLE_CLOUD_PROJECT'] + gcloud_test_client = Google::Cloud.new ENV["GOOGLE_CLOUD_PROJECT"] bigquery_test_client = gcloud_test_client.bigquery # Prime BigQuery for test From e81e7410fcdfec66cf815a1d6c3746d91ebc1543 Mon Sep 17 00:00:00 2001 From: Frank Natividad Date: Fri, 7 Oct 2016 16:58:21 -0700 Subject: [PATCH 09/44] Changes: 1. Added RSpec for quickstart.rb 2. Modified quickstart.rb to be a globally scopped. --- datastore/quickstart.rb | 59 ++++++++++++++----------------- datastore/spec/quickstart_spec.rb | 50 ++++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 32 deletions(-) create mode 100644 datastore/spec/quickstart_spec.rb diff --git a/datastore/quickstart.rb b/datastore/quickstart.rb index 3fbe6b2bc..16a6b7f33 100644 --- a/datastore/quickstart.rb +++ b/datastore/quickstart.rb @@ -12,37 +12,32 @@ # See the License for the specific language governing permissions and # limitations under the License. -def run_quickstart - # [START datastore_quickstart] - # Imports the Google Cloud client library - require "google/cloud" - - # Your Google Cloud Platform project ID - project_id = "YOUR_PROJECT_ID" - - # Instantiates a client - gcloud = Google::Cloud.new project_id - datastore_client = gcloud.datastore - - # The kind for the new entity - kind = "Task" - # The name/ID for the new entity - name = "sampletask1" - # The Cloud Datastore key for the new entity - task_key = datastore_client.key kind, name - - # Prepares the new entity - task = datastore_client.entity task_key do |t| - t["description"] = "Buy milk" - end - - # Saves the entity - datastore_client.save task - - puts "Saved #{task.key.name}: #{task['description']}" - # [END datastore_quickstart] +# [START datastore_quickstart] +# Imports the Google Cloud client library +require "google/cloud" + +# Your Google Cloud Platform project ID +project_id = "YOUR_PROJECT_ID" + +# Instantiates a client +gcloud = Google::Cloud.new project_id +datastore_client = gcloud.datastore + +# The kind for the new entity +kind = "Task" +# The name/ID for the new entity +name = "sampletask1" +# The Cloud Datastore key for the new entity +task_key = datastore_client.key kind, name + +# Prepares the new entity +task = datastore_client.entity task_key do |t| + t["description"] = "Buy milk" end -if __FILE__ == $PROGRAM_NAME - run_quickstart -end +# Saves the entity +datastore_client.save task + +puts "Saved #{task.key.name}: #{task['description']}" +# [END datastore_quickstart] + diff --git a/datastore/spec/quickstart_spec.rb b/datastore/spec/quickstart_spec.rb new file mode 100644 index 000000000..1eed344c0 --- /dev/null +++ b/datastore/spec/quickstart_spec.rb @@ -0,0 +1,50 @@ +# Copyright 2016 Google, Inc +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +require "rspec" +require "google/cloud" + +describe "Datastore Quickstart" do + + it "creates a new entity" do + # Initalize test objects + gcloud_test_client = Google::Cloud.new ENV["GOOGLE_CLOUD_PROJECT"] + datastore_test_client = gcloud_test_client.datastore + task_key_test_client = datastore_test_client.key "Task", "sampletask1" + + # Prime DataStore for test + if datastore_test_client.find task_key_test_client + task = datastore_test_client.find task_key_test_client + datastore_test_client.delete task + end + + expect(datastore_test_client.find(task_key_test_client)).to be nil + expect(Google::Cloud).to receive(:new).with("YOUR_PROJECT_ID"). + and_return(gcloud_test_client) + + # Run quickstart + expect { + load File::expand_path("quickstart.rb") + }.to output { + "Saved Task: Buy milk\n" + }.to_stdout + + expect(datastore_test_client.find(task_key_test_client)).not_to be nil + + # Clean up + task = datastore_test_client.find task_key_test_client + datastore_test_client.delete task + end + +end From 2590342d593eba860a279be94604fe19ff451f7a Mon Sep 17 00:00:00 2001 From: Frank Natividad Date: Fri, 7 Oct 2016 17:39:20 -0700 Subject: [PATCH 10/44] Changes: 1. Added spec/quickstart.rb to test quickstart.rb 2. Changed quickstart.rb to be globally scoped. --- language/quickstart.rb | 37 ++++++++++++++----------------- language/spec/quickstart_spec.rb | 38 ++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 21 deletions(-) create mode 100644 language/spec/quickstart_spec.rb diff --git a/language/quickstart.rb b/language/quickstart.rb index 39ad20603..f87a43ba0 100644 --- a/language/quickstart.rb +++ b/language/quickstart.rb @@ -12,30 +12,25 @@ # See the License for the specific language governing permissions and # limitations under the License. -def run_quickstart - # [START language_quickstart] - # Imports the Google Cloud client library - require "google/cloud" +# [START language_quickstart] +# Imports the Google Cloud client library +require "google/cloud" - # Your Google Cloud Platform project ID - project_id = "YOUR_PROJECT_ID" +# Your Google Cloud Platform project ID +project_id = "YOUR_PROJECT_ID" - # Instantiates a client - gcloud = Google::Cloud.new project_id - language_client = gcloud.language +# Instantiates a client +gcloud = Google::Cloud.new project_id +language_client = gcloud.language - # The text to analyze - text = "Hello, world!" - document = language_client.document text +# The text to analyze +text = "Hello, world!" +document = language_client.document text - # Detects the sentiment of the text - sentiment = document.sentiment +# Detects the sentiment of the text +sentiment = document.sentiment - puts "Text: #{text}" - puts "Sentiment: #{sentiment.polarity}, #{sentiment.magnitude}" - # [END language_quickstart] -end +puts "Text: #{text}" +puts "Sentiment: #{sentiment.polarity}, #{sentiment.magnitude}" +# [END language_quickstart] -if __FILE__ == $PROGRAM_NAME - run_quickstart -end diff --git a/language/spec/quickstart_spec.rb b/language/spec/quickstart_spec.rb new file mode 100644 index 000000000..a2ff46ceb --- /dev/null +++ b/language/spec/quickstart_spec.rb @@ -0,0 +1,38 @@ +#!/usr/bin/ruby +# Copyright 2016 Google, Inc +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +require "rspec" +require "google/cloud" + +describe "Language Quickstart" do + + it "detect sentimentt" do + # Initialize test objects + gcloud_test_client = Google::Cloud.new ENV["GOOGLE_CLOUD_PROJECT"] + expect(Google::Cloud).to receive(:new).with("YOUR_PROJECT_ID"). + and_return(gcloud_test_client) + + # Run quickstart + expect { + load File::expand_path("quickstart.rb") + }.to output( + "Text: Hello, world!\n"+ + "Sentiment: 1.0, 0.6000000238418579\n" + ).to_stdout + + end + +end + From 5e9c228e2d32db80558ad6d294eca243e5d8fa41 Mon Sep 17 00:00:00 2001 From: Frank Natividad Date: Fri, 7 Oct 2016 17:40:56 -0700 Subject: [PATCH 11/44] Changes: 1. Fixed typo --- language/spec/quickstart_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/language/spec/quickstart_spec.rb b/language/spec/quickstart_spec.rb index a2ff46ceb..3ea416804 100644 --- a/language/spec/quickstart_spec.rb +++ b/language/spec/quickstart_spec.rb @@ -18,7 +18,7 @@ describe "Language Quickstart" do - it "detect sentimentt" do + it "detect sentiment" do # Initialize test objects gcloud_test_client = Google::Cloud.new ENV["GOOGLE_CLOUD_PROJECT"] expect(Google::Cloud).to receive(:new).with("YOUR_PROJECT_ID"). From 740c7cd0a1a7db1b658f76b7edc3c5e44562f439 Mon Sep 17 00:00:00 2001 From: Jason Dobry Date: Mon, 10 Oct 2016 11:05:30 -0700 Subject: [PATCH 12/44] Simplify print statement in Vision quickstart. --- vision/quickstart.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vision/quickstart.rb b/vision/quickstart.rb index f373cf98d..c900eab1d 100644 --- a/vision/quickstart.rb +++ b/vision/quickstart.rb @@ -32,7 +32,7 @@ def run_quickstart puts "Labels:" labels.each do |label| - puts label + puts label.description end # [END vision_quickstart] end From ca30bc20b7334d43c4e9fdd6c0d81a22859a847c Mon Sep 17 00:00:00 2001 From: Frank Natividad Date: Mon, 10 Oct 2016 15:44:20 -0700 Subject: [PATCH 13/44] Changes: 1. Aligned group assignments 2. Removed cleanup from spec/quickstart_spec.rb 3. Fixed load call line 4. Removed "_test_client" and "_client" from variable names. --- bigquery/quickstart.rb | 6 +++--- bigquery/spec/quickstart_spec.rb | 22 +++++++++------------- 2 files changed, 12 insertions(+), 16 deletions(-) diff --git a/bigquery/quickstart.rb b/bigquery/quickstart.rb index 79fbdec7c..d9cf1850d 100644 --- a/bigquery/quickstart.rb +++ b/bigquery/quickstart.rb @@ -20,14 +20,14 @@ project_id = "YOUR_PROJECT_ID" # Instantiates a client -gcloud = Google::Cloud.new project_id -bigquery_client = gcloud.bigquery +gcloud = Google::Cloud.new project_id +bigquery = gcloud.bigquery # The name for the new dataset dataset_name = "my_new_dataset" # Creates the new dataset -dataset = bigquery_client.create_dataset dataset_name +dataset = bigquery.create_dataset dataset_name puts "Dataset #{dataset.dataset_id} created." # [END bigquery_quickstart] diff --git a/bigquery/spec/quickstart_spec.rb b/bigquery/spec/quickstart_spec.rb index 706e45a2b..57ac26204 100644 --- a/bigquery/spec/quickstart_spec.rb +++ b/bigquery/spec/quickstart_spec.rb @@ -19,32 +19,28 @@ it "creates a new dataset" do # Initialize test objects - gcloud_test_client = Google::Cloud.new ENV["GOOGLE_CLOUD_PROJECT"] - bigquery_test_client = gcloud_test_client.bigquery + gcloud = Google::Cloud.new ENV["GOOGLE_CLOUD_PROJECT"] + bigquery = gcloud.bigquery # Prime BigQuery for test - if bigquery_test_client.dataset "my_new_dataset" - bigquery_test_client.dataset("my_new_dataset").delete + if bigquery.dataset "my_new_dataset" + bigquery.dataset("my_new_dataset").delete end - expect(bigquery_test_client.dataset "my_new_dataset").to be nil + expect(bigquery.dataset "my_new_dataset").to be nil expect(Google::Cloud).to receive(:new). with("YOUR_PROJECT_ID"). - and_return(gcloud_test_client) + and_return(gcloud) # Run quickstart expect { - load File::expand_path("quickstart.rb") + load File.expand_path("../quickstart.rb", __dir__) }.to output( "Dataset my_new_dataset created\.\n" ).to_stdout - expect(bigquery_test_client.dataset "my_new_dataset").not_to be nil - - # Clean up - dataset = bigquery_test_client.dataset "my_new_dataset" - dataset.delete - end + expect(bigquery.dataset "my_new_dataset").not_to be nil + end end From b41be6aa9e7f85b7d546f1f1ffc7841fa9df443a Mon Sep 17 00:00:00 2001 From: Frank Natividad Date: Mon, 10 Oct 2016 15:57:46 -0700 Subject: [PATCH 14/44] Changes: 1. Aligned assignments in groups 2. Added an additional expectation in spec/quickstart_spec.rb --- datastore/quickstart.rb | 6 +++--- datastore/spec/quickstart_spec.rb | 27 +++++++++++++-------------- 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/datastore/quickstart.rb b/datastore/quickstart.rb index 16a6b7f33..41d00c02d 100644 --- a/datastore/quickstart.rb +++ b/datastore/quickstart.rb @@ -20,13 +20,13 @@ project_id = "YOUR_PROJECT_ID" # Instantiates a client -gcloud = Google::Cloud.new project_id +gcloud = Google::Cloud.new project_id datastore_client = gcloud.datastore # The kind for the new entity -kind = "Task" +kind = "Task" # The name/ID for the new entity -name = "sampletask1" +name = "sampletask1" # The Cloud Datastore key for the new entity task_key = datastore_client.key kind, name diff --git a/datastore/spec/quickstart_spec.rb b/datastore/spec/quickstart_spec.rb index 1eed344c0..fd6ca9a94 100644 --- a/datastore/spec/quickstart_spec.rb +++ b/datastore/spec/quickstart_spec.rb @@ -19,32 +19,31 @@ it "creates a new entity" do # Initalize test objects - gcloud_test_client = Google::Cloud.new ENV["GOOGLE_CLOUD_PROJECT"] - datastore_test_client = gcloud_test_client.datastore - task_key_test_client = datastore_test_client.key "Task", "sampletask1" + gcloud = Google::Cloud.new ENV["GOOGLE_CLOUD_PROJECT"] + datastore = gcloud.datastore + task_key = datastore.key "Task", "sampletask1" # Prime DataStore for test - if datastore_test_client.find task_key_test_client - task = datastore_test_client.find task_key_test_client - datastore_test_client.delete task + if datastore.find task_key + task = datastore.find task_key + datastore.delete task end - expect(datastore_test_client.find(task_key_test_client)).to be nil + expect(datastore.find task_key).to be nil expect(Google::Cloud).to receive(:new).with("YOUR_PROJECT_ID"). - and_return(gcloud_test_client) + and_return(gcloud) # Run quickstart expect { - load File::expand_path("quickstart.rb") + load File.expand_path("../quickstart.rb", __dir__) }.to output { "Saved Task: Buy milk\n" }.to_stdout - expect(datastore_test_client.find(task_key_test_client)).not_to be nil - - # Clean up - task = datastore_test_client.find task_key_test_client - datastore_test_client.delete task + # Check entity was saved + task_key = datastore.find(task_key) + expect(task_key).not_to be nil + expect(task_key["description"]).to eq "Buy milk" end end From 252f7a03677e8c5301d89ebc10676b2294931caa Mon Sep 17 00:00:00 2001 From: Frank Natividad Date: Mon, 10 Oct 2016 16:01:28 -0700 Subject: [PATCH 15/44] Changes: 1. Added a space before the concatenation 'plus' sign. 2. Removed trailing space 3. Aligned assignments in a group --- language/quickstart.rb | 4 ++-- language/spec/quickstart_spec.rb | 5 ++--- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/language/quickstart.rb b/language/quickstart.rb index f87a43ba0..822b74c71 100644 --- a/language/quickstart.rb +++ b/language/quickstart.rb @@ -20,11 +20,11 @@ project_id = "YOUR_PROJECT_ID" # Instantiates a client -gcloud = Google::Cloud.new project_id +gcloud = Google::Cloud.new project_id language_client = gcloud.language # The text to analyze -text = "Hello, world!" +text = "Hello, world!" document = language_client.document text # Detects the sentiment of the text diff --git a/language/spec/quickstart_spec.rb b/language/spec/quickstart_spec.rb index 3ea416804..7f0cbab73 100644 --- a/language/spec/quickstart_spec.rb +++ b/language/spec/quickstart_spec.rb @@ -26,12 +26,11 @@ # Run quickstart expect { - load File::expand_path("quickstart.rb") + load File.expand_path("../quickstart.rb", __dir__) }.to output( - "Text: Hello, world!\n"+ + "Text: Hello, world!\n" + "Sentiment: 1.0, 0.6000000238418579\n" ).to_stdout - end end From 88e99a5383089474d423e01636506dbfd4a811d7 Mon Sep 17 00:00:00 2001 From: Frank Natividad Date: Mon, 10 Oct 2016 16:05:00 -0700 Subject: [PATCH 16/44] Changes: 1. Aligned assignments in groups 2. Added space before concatenation '+' 3. Removed clean-up. --- pubsub/quickstart.rb | 33 +++++++++++------------- pubsub/spec/quickstart_spec.rb | 46 ++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 19 deletions(-) create mode 100644 pubsub/spec/quickstart_spec.rb diff --git a/pubsub/quickstart.rb b/pubsub/quickstart.rb index 2ac1f774a..044790b2a 100644 --- a/pubsub/quickstart.rb +++ b/pubsub/quickstart.rb @@ -12,28 +12,23 @@ # See the License for the specific language governing permissions and # limitations under the License. -def run_quickstart - # [START pubsub_quickstart] - # Imports the Google Cloud client library - require "google/cloud" +# [START pubsub_quickstart] +# Imports the Google Cloud client library +require "google/cloud" - # Your Google Cloud Platform project ID - project_id = "YOUR_PROJECT_ID" +# Your Google Cloud Platform project ID +project_id = "YOUR_PROJECT_ID" - # Instantiates a client - gcloud = Google::Cloud.new project_id - pubsub_client = gcloud.pubsub +# Instantiates a client +gcloud = Google::Cloud.new project_id +pubsub_client = gcloud.pubsub - # The name for the new topic - topic_name = "my-new-topic" +# The name for the new topic +topic_name = "my-new-topic" - # Creates the new topic - topic = pubsub_client.create_topic topic_name +# Creates the new topic +topic = pubsub_client.create_topic topic_name - puts "Topic #{topic.name} created." - # [END pubsub_quickstart] -end +puts "Topic #{topic.name} created." +# [END pubsub_quickstart] -if __FILE__ == $PROGRAM_NAME - run_quickstart -end diff --git a/pubsub/spec/quickstart_spec.rb b/pubsub/spec/quickstart_spec.rb new file mode 100644 index 000000000..b68cfc3eb --- /dev/null +++ b/pubsub/spec/quickstart_spec.rb @@ -0,0 +1,46 @@ +# Copyright 2016 Google, Inc +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +require "rspec" +require "google/cloud" + +describe "PubSub Quickstart" do + + it "creates a new topic" do + # Initialize test objects + gcloud_test_client = Google::Cloud.new ENV["GOOGLE_CLOUD_PROJECT"] + pubsub_test_client = gcloud_test_client.pubsub + + # Prime PubSub for test + if pubsub_test_client.topic "my-new-topic" + pubsub_test_client.topic("my-new-topic").delete + end + + expect(pubsub_test_client.topic "my-new-topic").to be nil + expect(Google::Cloud).to receive(:new). + with("YOUR_PROJECT_ID"). + and_return(gcloud_test_client) + + # Run quickstart + expect { + load File.expand_path("../quickstart.rb", __dir__) + }.to output( + "Topic projects/#{ENV['GOOGLE_CLOUD_PROJECT']}/" + + "topics/my-new-topic created.\n" + ).to_stdout + + expect(pubsub_test_client.topic "my-new-topic").not_to be nil + end + +end From c82d70c2d75a1450bb85155703c639c6cb943b45 Mon Sep 17 00:00:00 2001 From: Frank Natividad Date: Mon, 10 Oct 2016 16:11:55 -0700 Subject: [PATCH 17/44] Changes: 1. Removed _test_client and _client from quickstart_spec.rb and quickstart.rb respectively. 2. Changed single-quote to double-quotes. --- pubsub/quickstart.rb | 6 +++--- pubsub/spec/quickstart_spec.rb | 16 ++++++++-------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/pubsub/quickstart.rb b/pubsub/quickstart.rb index 044790b2a..32899941b 100644 --- a/pubsub/quickstart.rb +++ b/pubsub/quickstart.rb @@ -20,14 +20,14 @@ project_id = "YOUR_PROJECT_ID" # Instantiates a client -gcloud = Google::Cloud.new project_id -pubsub_client = gcloud.pubsub +gcloud = Google::Cloud.new project_id +pubsub = gcloud.pubsub # The name for the new topic topic_name = "my-new-topic" # Creates the new topic -topic = pubsub_client.create_topic topic_name +topic = pubsub.create_topic topic_name puts "Topic #{topic.name} created." # [END pubsub_quickstart] diff --git a/pubsub/spec/quickstart_spec.rb b/pubsub/spec/quickstart_spec.rb index b68cfc3eb..2743bc217 100644 --- a/pubsub/spec/quickstart_spec.rb +++ b/pubsub/spec/quickstart_spec.rb @@ -19,28 +19,28 @@ it "creates a new topic" do # Initialize test objects - gcloud_test_client = Google::Cloud.new ENV["GOOGLE_CLOUD_PROJECT"] - pubsub_test_client = gcloud_test_client.pubsub + gcloud = Google::Cloud.new ENV["GOOGLE_CLOUD_PROJECT"] + pubsub = gcloud.pubsub # Prime PubSub for test - if pubsub_test_client.topic "my-new-topic" - pubsub_test_client.topic("my-new-topic").delete + if pubsub.topic "my-new-topic" + pubsub.topic("my-new-topic").delete end - expect(pubsub_test_client.topic "my-new-topic").to be nil + expect(pubsub.topic "my-new-topic").to be nil expect(Google::Cloud).to receive(:new). with("YOUR_PROJECT_ID"). - and_return(gcloud_test_client) + and_return(gcloud) # Run quickstart expect { load File.expand_path("../quickstart.rb", __dir__) }.to output( - "Topic projects/#{ENV['GOOGLE_CLOUD_PROJECT']}/" + + "Topic projects/#{ENV["GOOGLE_CLOUD_PROJECT"]}/" + "topics/my-new-topic created.\n" ).to_stdout - expect(pubsub_test_client.topic "my-new-topic").not_to be nil + expect(pubsub.topic "my-new-topic").not_to be nil end end From b426429ab903982344ddc8d1b4f6a46e471967bd Mon Sep 17 00:00:00 2001 From: Frank Natividad Date: Mon, 10 Oct 2016 16:17:56 -0700 Subject: [PATCH 18/44] Changes: 1. Added spec/quickstart_spec.rb for quickstart.rb 2. Removed '_client' 3. Aligned assignments in groups --- speech/quickstart.rb | 40 +++++++++++++++------------------- speech/spec/quickstart_spec.rb | 35 +++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 23 deletions(-) create mode 100644 speech/spec/quickstart_spec.rb diff --git a/speech/quickstart.rb b/speech/quickstart.rb index 47c36691f..d11d4c688 100644 --- a/speech/quickstart.rb +++ b/speech/quickstart.rb @@ -12,33 +12,27 @@ # See the License for the specific language governing permissions and # limitations under the License. -def run_quickstart - # [START speech_quickstart] - # Imports the Google Cloud client library - require "google/cloud" +# [START speech_quickstart] +# Imports the Google Cloud client library +require "google/cloud" - # Your Google Cloud Platform project ID - project_id = "YOUR_PROJECT_ID" +# Your Google Cloud Platform project ID +project_id = "YOUR_PROJECT_ID" - # Instantiates a client - gcloud = Google::Cloud.new project_id - speech_client = gcloud.speech +# Instantiates a client +gcloud = Google::Cloud.new project_id +speech = gcloud.speech - # The name of the audio file to transcribe - fileName = "./audio_files/audio.raw" +# The name of the audio file to transcribe +fileName = "./audio_files/audio.raw" - # The audio file's encoding and sample rate - audio = speech_client.audio fileName, - encoding: :raw, sample_rate: 16000 +# The audio file's encoding and sample rate +audio = speech.audio fileName, encoding: :raw, sample_rate: 16000 - # Detects speech in the audio file - results = audio.recognize - result = results.first +# Detects speech in the audio file +results = audio.recognize +result = results.first - puts "Transcription: #{result.transcript}" - # [END speech_quickstart] -end +puts "Transcription: #{result.transcript}" +# [END speech_quickstart] -if __FILE__ == $PROGRAM_NAME - run_quickstart -end diff --git a/speech/spec/quickstart_spec.rb b/speech/spec/quickstart_spec.rb new file mode 100644 index 000000000..6f065c2a4 --- /dev/null +++ b/speech/spec/quickstart_spec.rb @@ -0,0 +1,35 @@ +# Copyright 2016 Google, Inc +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +require "rspec" +require "google/cloud" + +describe "Speech Quickstart" do + + it "transcribes a sample audio.raw file" do + # Initiliaze and setup necessary objects + gcloud = Google::Cloud.new ENV["GOOGLE_CLOUD_PROJECT"] + + expect(Google::Cloud).to receive(:new).with("YOUR_PROJECT_ID"). + and_return(gcloud) + + # Transcribe + expect { + load File.expand_path("../quickstart.rb", __dir__) + }.to output( + "Transcription: how old is the Brooklyn Bridge\n" + ).to_stdout + end + +end From 135527cc65ff1181e9e61a3797d3b5d2d6cb948c Mon Sep 17 00:00:00 2001 From: Frank Natividad Date: Mon, 10 Oct 2016 16:25:12 -0700 Subject: [PATCH 19/44] Changes: 1. Added spec/quickstart_spec.rb for quickstart.rb 2. Aligned assignments in quickstart.rb and spec/quickstart_spec.rb 3. remove _client from quickstart.rb --- vision/quickstart.rb | 37 +++++++++++-------------- vision/spec/quickstart_spec.rb | 50 ++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 21 deletions(-) create mode 100644 vision/spec/quickstart_spec.rb diff --git a/vision/quickstart.rb b/vision/quickstart.rb index c900eab1d..c17e3a4e6 100644 --- a/vision/quickstart.rb +++ b/vision/quickstart.rb @@ -12,31 +12,26 @@ # See the License for the specific language governing permissions and # limitations under the License. -def run_quickstart - # [START vision_quickstart] - # Imports the Google Cloud client library - require "google/cloud" +# [START vision_quickstart] +# Imports the Google Cloud client library +require "google/cloud" - # Your Google Cloud Platform project ID - project_id = "YOUR_PROJECT_ID" +# Your Google Cloud Platform project ID +project_id = "YOUR_PROJECT_ID" - # Instantiates a client - gcloud = Google::Cloud.new project_id - vision_client = gcloud.vision +# Instantiates a client +gcloud = Google::Cloud.new project_id +vision = gcloud.vision - # The name of the image file to annotate - fileName = "./images/cat.jpg" +# The name of the image file to annotate +fileName = "./images/cat.jpg" - # Performs label detection on the image file - labels = vision_client.image(fileName).labels +# Performs label detection on the image file +labels = vision.image(fileName).labels - puts "Labels:" - labels.each do |label| - puts label.description - end - # [END vision_quickstart] +puts "Labels:" +labels.each do |label| + puts label.description end +# [END vision_quickstart] -if __FILE__ == $PROGRAM_NAME - run_quickstart -end diff --git a/vision/spec/quickstart_spec.rb b/vision/spec/quickstart_spec.rb new file mode 100644 index 000000000..8bae31438 --- /dev/null +++ b/vision/spec/quickstart_spec.rb @@ -0,0 +1,50 @@ +# Copyright 2016 Google, Inc +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +require "rspec" +require "google/cloud" + +describe "Vision Quickstart" do + + it "label a cat image" do + # Initialize and setup test objects + gcloud = Google::Cloud.new ENV["GOOGLE_CLOUD_PROJECT"] + + expect(Google::Cloud).to receive(:new).with("YOUR_PROJECT_ID"). + and_return(gcloud) + + # Label a cat image + expect { + load File.expand_path("../quickstart.rb", __dir__) + }.to output( + "Labels:\n" + + "cat\n" + + "mammal\n" + + "vertebrate\n" + + "whiskers\n" + + "fauna\n" + + "cat like mammal\n" + + "small to medium sized cats\n" + + "grass\n" + + "tabby cat\n" + + "european shorthair\n" + + "wild cat\n" + + "domestic short haired cat\n" + + "rusty spotted cat\n" + + "abyssinian\n" + + "carnivoran\n" + ).to_stdout + end + +end From 99c4689940ff7b17e4ddcec7e637be70d6339edb Mon Sep 17 00:00:00 2001 From: Frank Natividad Date: Mon, 10 Oct 2016 16:28:05 -0700 Subject: [PATCH 20/44] Changes: 1. Added spec quickstart_spec.rb for quickstart.rb 2. Aligned assignments in both quickstart.rb and spec/quickstart_spec.rb 3. removed _client from quickstart.rb --- translate/quickstart.rb | 39 ++++++++++++++----------------- translate/spec/quickstart_spec.rb | 38 ++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 22 deletions(-) create mode 100644 translate/spec/quickstart_spec.rb diff --git a/translate/quickstart.rb b/translate/quickstart.rb index dfdb165a0..8827fe3f6 100644 --- a/translate/quickstart.rb +++ b/translate/quickstart.rb @@ -12,31 +12,26 @@ # See the License for the specific language governing permissions and # limitations under the License. -def run_quickstart - # [START translate_quickstart] - # Imports the Google Cloud client library - require "google/cloud" +# [START translate_quickstart] +# Imports the Google Cloud client library +require "google/cloud" - # Your Translate API key - api_key = "YOUR_API_KEY" +# Your Translate API key +api_key = "YOUR_API_KEY" - # Instantiates a client - gcloud = Google::Cloud.new - translate_client = gcloud.translate api_key +# Instantiates a client +gcloud = Google::Cloud.new +translate = gcloud.translate api_key - # The text to translate - text = "Hello, world!" - # The target language - target = "ru" +# The text to translate +text = "Hello, world!" +# The target language +target = "ru" - # Translates some text into Russian - translation = translate_client.translate text, to: target +# Translates some text into Russian +translation = translate.translate text, to: target - puts "Text: #{text}" - puts "Translation: #{translation}" - # [END translate_quickstart] -end +puts "Text: #{text}" +puts "Translation: #{translation}" +# [END translate_quickstart] -if __FILE__ == $PROGRAM_NAME - run_quickstart -end diff --git a/translate/spec/quickstart_spec.rb b/translate/spec/quickstart_spec.rb new file mode 100644 index 000000000..a02e9ce81 --- /dev/null +++ b/translate/spec/quickstart_spec.rb @@ -0,0 +1,38 @@ +# Copyright 2016 Google, Inc +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +require "rspec" +require "google/cloud" + +describe "Translate Quickstart" do + + it "translates Hello, world! to Russian" do + # Initialize and setup test objects + gcloud = Google::Cloud.new + translate = gcloud.translate ENV["TRANSLATE_KEY"] + expect(Google::Cloud).to receive(:new).and_return(gcloud) + expect(gcloud).to receive(:translate).with("YOUR_API_KEY"). + and_return(translate) + + # Translate + expect { + load File.expand_path("../quickstart.rb", __dir__) + }.to output( + "Text: Hello, world!\n"+ + "Translation: Привет мир!\n" + ).to_stdout + end + +end + From c389c770abc085555673eafa5be45730c7735aea Mon Sep 17 00:00:00 2001 From: Frank Natividad Date: Tue, 11 Oct 2016 09:34:10 -0700 Subject: [PATCH 21/44] Changes: 1. Added spec/quickstart_spec.rb for Storage Quickstart 2. Uses ENV GOOGLE_CLOUD_BUCKET 3. Fixed assignment alignment. --- storage/quickstart.rb | 33 +++++++++------------ storage/spec/quickstart_spec.rb | 52 +++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 19 deletions(-) create mode 100644 storage/spec/quickstart_spec.rb diff --git a/storage/quickstart.rb b/storage/quickstart.rb index b528b4e1f..e75d8bd9e 100644 --- a/storage/quickstart.rb +++ b/storage/quickstart.rb @@ -12,28 +12,23 @@ # See the License for the specific language governing permissions and # limitations under the License. -def run_quickstart - # [START storage_quickstart] - # Imports the Google Cloud client library - require "google/cloud" +# [START storage_quickstart] +# Imports the Google Cloud client library +require "google/cloud" - # Your Google Cloud Platform project ID - project_id = "YOUR_PROJECT_ID" +# Your Google Cloud Platform project ID +project_id = "YOUR_PROJECT_ID" - # Instantiates a client - gcloud = Google::Cloud.new project_id - storage_client = gcloud.storage +# Instantiates a client +gcloud = Google::Cloud.new project_id +storage = gcloud.storage - # The name for the new bucket - bucket_name = "my-new-bucket" +# The name for the new bucket +bucket_name = "my-new-bucket" - # Creates the new bucket - bucket = storage_client.create_bucket bucket_name +# Creates the new bucket +bucket = storage.create_bucket bucket_name - puts "Bucket #{bucket.name} created." - # [END storage_quickstart] -end +puts "Bucket #{bucket.name} was created." +# [END storage_quickstart] -if __FILE__ == $PROGRAM_NAME - run_quickstart -end diff --git a/storage/spec/quickstart_spec.rb b/storage/spec/quickstart_spec.rb new file mode 100644 index 000000000..73da549fb --- /dev/null +++ b/storage/spec/quickstart_spec.rb @@ -0,0 +1,52 @@ +# Copyright 2016 Google, Inc +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +require "rspec" +require "google/cloud" + +describe "Storage Quickstart" do + + it "creates a new bucket" do + # Initialize test objects + gcloud = Google::Cloud.new ENV["GOOGLE_CLOUD_PROJECT"] + storage = gcloud.storage + bucket_name = ENV["GOOGLE_CLOUD_BUCKET"] + + # Check that bucket_name doesn't already exist + if storage.bucket bucket_name + storage.bucket(bucket_name).delete + end + + expect(storage.bucket bucket_name).to be nil + expect(Google::Cloud).to receive(:new).with("YOUR_PROJECT_ID"). + and_return(gcloud) + + # Create storage bucket with bucket_name and swap with quickstart bucket + bucket = storage.create_bucket bucket_name + expect(gcloud).to receive(:storage).and_return(storage) + expect(storage).to receive(:create_bucket).with("my-new-bucket"). + and_return(bucket) + + # Run Storage Quickstart + expect { + load File.expand_path("../quickstart.rb", __dir__) + }.to output( + "Bucket #{bucket_name} was created.\n" + ).to_stdout + + expect(storage.bucket bucket_name).not_to be nil + end + +end + From 019932c9af05520841d0d3798c1e25f0acf8c760 Mon Sep 17 00:00:00 2001 From: Frank Natividad Date: Tue, 11 Oct 2016 14:17:22 -0700 Subject: [PATCH 22/44] Changes: 1. Fixed assignment alignments --- translate/quickstart.rb | 2 +- translate/spec/quickstart_spec.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/translate/quickstart.rb b/translate/quickstart.rb index 8827fe3f6..24513f397 100644 --- a/translate/quickstart.rb +++ b/translate/quickstart.rb @@ -24,7 +24,7 @@ translate = gcloud.translate api_key # The text to translate -text = "Hello, world!" +text = "Hello, world!" # The target language target = "ru" diff --git a/translate/spec/quickstart_spec.rb b/translate/spec/quickstart_spec.rb index a02e9ce81..98e51a2cb 100644 --- a/translate/spec/quickstart_spec.rb +++ b/translate/spec/quickstart_spec.rb @@ -19,7 +19,7 @@ it "translates Hello, world! to Russian" do # Initialize and setup test objects - gcloud = Google::Cloud.new + gcloud = Google::Cloud.new translate = gcloud.translate ENV["TRANSLATE_KEY"] expect(Google::Cloud).to receive(:new).and_return(gcloud) expect(gcloud).to receive(:translate).with("YOUR_API_KEY"). From 87386ef081fcb5fcec9c80e898cf18807f971ea1 Mon Sep 17 00:00:00 2001 From: Frank Natividad Date: Tue, 11 Oct 2016 14:18:08 -0700 Subject: [PATCH 23/44] Changes: 1. Aligned assignments 2. Added spec/quickstart_spec.rb for quickstart.rb --- logging/quickstart.rb | 45 +++++++++++++--------------- logging/spec/quickstart_spec.rb | 53 +++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 25 deletions(-) create mode 100644 logging/spec/quickstart_spec.rb diff --git a/logging/quickstart.rb b/logging/quickstart.rb index a94422422..d1be53a08 100644 --- a/logging/quickstart.rb +++ b/logging/quickstart.rb @@ -12,34 +12,29 @@ # See the License for the specific language governing permissions and # limitations under the License. -def run_quickstart - # [START logging_quickstart] - # Imports the Google Cloud client library - require "google/cloud" +# [START logging_quickstart] +# Imports the Google Cloud client library +require "google/cloud" - # Your Google Cloud Platform project ID - project_id = "YOUR_PROJECT_ID" +# Your Google Cloud Platform project ID +project_id = "YOUR_PROJECT_ID" - # Instantiates a client - gcloud = Google::Cloud.new project_id - logging_client = gcloud.logging +# Instantiates a client +gcloud = Google::Cloud.new project_id +logging = gcloud.logging - # Prepares a log entry - entry = logging.entry - # The data to log - entry.payload = "Hello, world!" - # The name of the log to write to - entry.log_name = "my-log" - # The resource associated with the data - entry.resource.type = "global" +# Prepares a log entry +entry = logging.entry +# The data to log +entry.payload = "Hello, world!" +# The name of the log to write to +entry.log_name = "my-log" +# The resource associated with the data +entry.resource.type = "global" - # Writes the log entry - logging_client.write_entries entry +# Writes the log entry +logging.write_entries entry - puts "Logged #{entry.payload}" - # [END logging_quickstart] -end +puts "Logged #{entry.payload}" +# [END logging_quickstart] -if __FILE__ == $PROGRAM_NAME - run_quickstart -end diff --git a/logging/spec/quickstart_spec.rb b/logging/spec/quickstart_spec.rb new file mode 100644 index 000000000..90ca028da --- /dev/null +++ b/logging/spec/quickstart_spec.rb @@ -0,0 +1,53 @@ +# Copyright 2016 Google, Inc +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +require "rspec" +require "google/cloud" + +describe "Logging Quickstart" do + + it "logs a new entry" do + # Initialize test objects + entry_filter = %Q{logName:"my-log" textPayload:"Hello, world!"} + gcloud = Google::Cloud.new ENV["GOOGLE_CLOUD_PROJECT"] + logging = gcloud.logging + + expect(Google::Cloud).to receive(:new).with("YOUR_PROJECT_ID"). + and_return(gcloud) + + # Prepare for tests + entries = logging.entries filter: entry_filter + unless entries.empty? + logging.delete_log "my-log" + end + + expect(logging.entries(filter: entry_filter)).to be_empty + + # Log a new entry + expect { + load File.expand_path("../quickstart.rb", __dir__) + }.to output( + "Logged Hello, world!\n" + ).to_stdout + + # Allow the entry some time to propagate + sleep(5) + + # Did we actually write a new entry? + entries = logging.entries filter: entry_filter + expect(entries).to_not be_empty + end + +end + From 2d1367f49dd8bdf72f00e1ff2da672c199af34b5 Mon Sep 17 00:00:00 2001 From: Frank Natividad Date: Tue, 11 Oct 2016 15:55:29 -0700 Subject: [PATCH 24/44] Changes: 1. Changed bucket environmental variable to use STORAGE_BUCKET --- storage/spec/quickstart_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/storage/spec/quickstart_spec.rb b/storage/spec/quickstart_spec.rb index 73da549fb..c8f17e961 100644 --- a/storage/spec/quickstart_spec.rb +++ b/storage/spec/quickstart_spec.rb @@ -21,7 +21,7 @@ # Initialize test objects gcloud = Google::Cloud.new ENV["GOOGLE_CLOUD_PROJECT"] storage = gcloud.storage - bucket_name = ENV["GOOGLE_CLOUD_BUCKET"] + bucket_name = ENV["STORAGE_BUCKET"] # Check that bucket_name doesn't already exist if storage.bucket bucket_name From 71a0f2f89ca89dfc9a6839b58ef7203c759adeea Mon Sep 17 00:00:00 2001 From: Frank Natividad Date: Tue, 11 Oct 2016 16:42:51 -0700 Subject: [PATCH 25/44] Changes: 1. Removed comments from quickstart's RSpecs --- bigquery/spec/quickstart_spec.rb | 3 --- datastore/spec/quickstart_spec.rb | 4 ---- language/spec/quickstart_spec.rb | 2 -- logging/spec/quickstart_spec.rb | 5 ----- pubsub/spec/quickstart_spec.rb | 3 --- speech/spec/quickstart_spec.rb | 2 -- storage/spec/quickstart_spec.rb | 8 +++----- translate/spec/quickstart_spec.rb | 2 -- vision/spec/quickstart_spec.rb | 2 -- 9 files changed, 3 insertions(+), 28 deletions(-) diff --git a/bigquery/spec/quickstart_spec.rb b/bigquery/spec/quickstart_spec.rb index 57ac26204..33cf3474f 100644 --- a/bigquery/spec/quickstart_spec.rb +++ b/bigquery/spec/quickstart_spec.rb @@ -18,11 +18,9 @@ describe "BigQuery Quickstart" do it "creates a new dataset" do - # Initialize test objects gcloud = Google::Cloud.new ENV["GOOGLE_CLOUD_PROJECT"] bigquery = gcloud.bigquery - # Prime BigQuery for test if bigquery.dataset "my_new_dataset" bigquery.dataset("my_new_dataset").delete end @@ -32,7 +30,6 @@ with("YOUR_PROJECT_ID"). and_return(gcloud) - # Run quickstart expect { load File.expand_path("../quickstart.rb", __dir__) }.to output( diff --git a/datastore/spec/quickstart_spec.rb b/datastore/spec/quickstart_spec.rb index fd6ca9a94..c8f2e5b42 100644 --- a/datastore/spec/quickstart_spec.rb +++ b/datastore/spec/quickstart_spec.rb @@ -18,12 +18,10 @@ describe "Datastore Quickstart" do it "creates a new entity" do - # Initalize test objects gcloud = Google::Cloud.new ENV["GOOGLE_CLOUD_PROJECT"] datastore = gcloud.datastore task_key = datastore.key "Task", "sampletask1" - # Prime DataStore for test if datastore.find task_key task = datastore.find task_key datastore.delete task @@ -33,14 +31,12 @@ expect(Google::Cloud).to receive(:new).with("YOUR_PROJECT_ID"). and_return(gcloud) - # Run quickstart expect { load File.expand_path("../quickstart.rb", __dir__) }.to output { "Saved Task: Buy milk\n" }.to_stdout - # Check entity was saved task_key = datastore.find(task_key) expect(task_key).not_to be nil expect(task_key["description"]).to eq "Buy milk" diff --git a/language/spec/quickstart_spec.rb b/language/spec/quickstart_spec.rb index 7f0cbab73..c1587a17b 100644 --- a/language/spec/quickstart_spec.rb +++ b/language/spec/quickstart_spec.rb @@ -19,12 +19,10 @@ describe "Language Quickstart" do it "detect sentiment" do - # Initialize test objects gcloud_test_client = Google::Cloud.new ENV["GOOGLE_CLOUD_PROJECT"] expect(Google::Cloud).to receive(:new).with("YOUR_PROJECT_ID"). and_return(gcloud_test_client) - # Run quickstart expect { load File.expand_path("../quickstart.rb", __dir__) }.to output( diff --git a/logging/spec/quickstart_spec.rb b/logging/spec/quickstart_spec.rb index 90ca028da..e1642bd1f 100644 --- a/logging/spec/quickstart_spec.rb +++ b/logging/spec/quickstart_spec.rb @@ -18,7 +18,6 @@ describe "Logging Quickstart" do it "logs a new entry" do - # Initialize test objects entry_filter = %Q{logName:"my-log" textPayload:"Hello, world!"} gcloud = Google::Cloud.new ENV["GOOGLE_CLOUD_PROJECT"] logging = gcloud.logging @@ -26,7 +25,6 @@ expect(Google::Cloud).to receive(:new).with("YOUR_PROJECT_ID"). and_return(gcloud) - # Prepare for tests entries = logging.entries filter: entry_filter unless entries.empty? logging.delete_log "my-log" @@ -34,17 +32,14 @@ expect(logging.entries(filter: entry_filter)).to be_empty - # Log a new entry expect { load File.expand_path("../quickstart.rb", __dir__) }.to output( "Logged Hello, world!\n" ).to_stdout - # Allow the entry some time to propagate sleep(5) - # Did we actually write a new entry? entries = logging.entries filter: entry_filter expect(entries).to_not be_empty end diff --git a/pubsub/spec/quickstart_spec.rb b/pubsub/spec/quickstart_spec.rb index 2743bc217..4f320e3d7 100644 --- a/pubsub/spec/quickstart_spec.rb +++ b/pubsub/spec/quickstart_spec.rb @@ -18,11 +18,9 @@ describe "PubSub Quickstart" do it "creates a new topic" do - # Initialize test objects gcloud = Google::Cloud.new ENV["GOOGLE_CLOUD_PROJECT"] pubsub = gcloud.pubsub - # Prime PubSub for test if pubsub.topic "my-new-topic" pubsub.topic("my-new-topic").delete end @@ -32,7 +30,6 @@ with("YOUR_PROJECT_ID"). and_return(gcloud) - # Run quickstart expect { load File.expand_path("../quickstart.rb", __dir__) }.to output( diff --git a/speech/spec/quickstart_spec.rb b/speech/spec/quickstart_spec.rb index 6f065c2a4..77a735e48 100644 --- a/speech/spec/quickstart_spec.rb +++ b/speech/spec/quickstart_spec.rb @@ -18,13 +18,11 @@ describe "Speech Quickstart" do it "transcribes a sample audio.raw file" do - # Initiliaze and setup necessary objects gcloud = Google::Cloud.new ENV["GOOGLE_CLOUD_PROJECT"] expect(Google::Cloud).to receive(:new).with("YOUR_PROJECT_ID"). and_return(gcloud) - # Transcribe expect { load File.expand_path("../quickstart.rb", __dir__) }.to output( diff --git a/storage/spec/quickstart_spec.rb b/storage/spec/quickstart_spec.rb index c8f17e961..7037bc0fe 100644 --- a/storage/spec/quickstart_spec.rb +++ b/storage/spec/quickstart_spec.rb @@ -18,27 +18,25 @@ describe "Storage Quickstart" do it "creates a new bucket" do - # Initialize test objects gcloud = Google::Cloud.new ENV["GOOGLE_CLOUD_PROJECT"] storage = gcloud.storage bucket_name = ENV["STORAGE_BUCKET"] - # Check that bucket_name doesn't already exist if storage.bucket bucket_name - storage.bucket(bucket_name).delete + bucket = storage.bucket bucket_name + bucket.files.each &:delete until bucket.files.empty? + bucket.delete end expect(storage.bucket bucket_name).to be nil expect(Google::Cloud).to receive(:new).with("YOUR_PROJECT_ID"). and_return(gcloud) - # Create storage bucket with bucket_name and swap with quickstart bucket bucket = storage.create_bucket bucket_name expect(gcloud).to receive(:storage).and_return(storage) expect(storage).to receive(:create_bucket).with("my-new-bucket"). and_return(bucket) - # Run Storage Quickstart expect { load File.expand_path("../quickstart.rb", __dir__) }.to output( diff --git a/translate/spec/quickstart_spec.rb b/translate/spec/quickstart_spec.rb index 98e51a2cb..e06e385bc 100644 --- a/translate/spec/quickstart_spec.rb +++ b/translate/spec/quickstart_spec.rb @@ -18,14 +18,12 @@ describe "Translate Quickstart" do it "translates Hello, world! to Russian" do - # Initialize and setup test objects gcloud = Google::Cloud.new translate = gcloud.translate ENV["TRANSLATE_KEY"] expect(Google::Cloud).to receive(:new).and_return(gcloud) expect(gcloud).to receive(:translate).with("YOUR_API_KEY"). and_return(translate) - # Translate expect { load File.expand_path("../quickstart.rb", __dir__) }.to output( diff --git a/vision/spec/quickstart_spec.rb b/vision/spec/quickstart_spec.rb index 8bae31438..af204d008 100644 --- a/vision/spec/quickstart_spec.rb +++ b/vision/spec/quickstart_spec.rb @@ -18,13 +18,11 @@ describe "Vision Quickstart" do it "label a cat image" do - # Initialize and setup test objects gcloud = Google::Cloud.new ENV["GOOGLE_CLOUD_PROJECT"] expect(Google::Cloud).to receive(:new).with("YOUR_PROJECT_ID"). and_return(gcloud) - # Label a cat image expect { load File.expand_path("../quickstart.rb", __dir__) }.to output( From c2994e06d5a12e38f7ba87598419d77581da1517 Mon Sep 17 00:00:00 2001 From: Frank Natividad Date: Tue, 11 Oct 2016 16:46:04 -0700 Subject: [PATCH 26/44] Changes: 1. removed "_client" from datastore quickstart.rb --- datastore/quickstart.rb | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/datastore/quickstart.rb b/datastore/quickstart.rb index 41d00c02d..be2c72304 100644 --- a/datastore/quickstart.rb +++ b/datastore/quickstart.rb @@ -20,23 +20,23 @@ project_id = "YOUR_PROJECT_ID" # Instantiates a client -gcloud = Google::Cloud.new project_id -datastore_client = gcloud.datastore +gcloud = Google::Cloud.new project_id +datastore = gcloud.datastore # The kind for the new entity -kind = "Task" +kind = "Task" # The name/ID for the new entity -name = "sampletask1" +name = "sampletask1" # The Cloud Datastore key for the new entity -task_key = datastore_client.key kind, name +task_key = datastore.key kind, name # Prepares the new entity -task = datastore_client.entity task_key do |t| +task = datastore.entity task_key do |t| t["description"] = "Buy milk" end # Saves the entity -datastore_client.save task +datastore.save task puts "Saved #{task.key.name}: #{task['description']}" # [END datastore_quickstart] From 73fdf644073f7b49b74b269f96fd6cad0367593b Mon Sep 17 00:00:00 2001 From: Frank Natividad Date: Tue, 11 Oct 2016 16:48:04 -0700 Subject: [PATCH 27/44] Removed extra spaces in logging/quickstart.rb --- logging/quickstart.rb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/logging/quickstart.rb b/logging/quickstart.rb index d1be53a08..eb88ff348 100644 --- a/logging/quickstart.rb +++ b/logging/quickstart.rb @@ -20,15 +20,15 @@ project_id = "YOUR_PROJECT_ID" # Instantiates a client -gcloud = Google::Cloud.new project_id -logging = gcloud.logging +gcloud = Google::Cloud.new project_id +logging = gcloud.logging # Prepares a log entry -entry = logging.entry +entry = logging.entry # The data to log -entry.payload = "Hello, world!" +entry.payload = "Hello, world!" # The name of the log to write to -entry.log_name = "my-log" +entry.log_name = "my-log" # The resource associated with the data entry.resource.type = "global" From 97f366ea5fa93cdcc4cbef633f849f4143f1665a Mon Sep 17 00:00:00 2001 From: Frank Natividad Date: Tue, 11 Oct 2016 16:50:15 -0700 Subject: [PATCH 28/44] Removed %Q{} and used single-quotes instead --- logging/spec/quickstart_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/logging/spec/quickstart_spec.rb b/logging/spec/quickstart_spec.rb index e1642bd1f..713864d18 100644 --- a/logging/spec/quickstart_spec.rb +++ b/logging/spec/quickstart_spec.rb @@ -18,7 +18,7 @@ describe "Logging Quickstart" do it "logs a new entry" do - entry_filter = %Q{logName:"my-log" textPayload:"Hello, world!"} + entry_filter = 'logName:"my-log" textPayload:"Hello, world!"' gcloud = Google::Cloud.new ENV["GOOGLE_CLOUD_PROJECT"] logging = gcloud.logging From 3249d2712401d6cf3a11e324b4341a813193343e Mon Sep 17 00:00:00 2001 From: Frank Natividad Date: Tue, 11 Oct 2016 16:52:31 -0700 Subject: [PATCH 29/44] Removed spaces from assignment in trasnlate --- translate/quickstart.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/translate/quickstart.rb b/translate/quickstart.rb index 24513f397..8827fe3f6 100644 --- a/translate/quickstart.rb +++ b/translate/quickstart.rb @@ -24,7 +24,7 @@ translate = gcloud.translate api_key # The text to translate -text = "Hello, world!" +text = "Hello, world!" # The target language target = "ru" From e522da5bcd0c8fc4a4f61e4863e8c9d009424df5 Mon Sep 17 00:00:00 2001 From: Frank Natividad Date: Tue, 11 Oct 2016 17:03:19 -0700 Subject: [PATCH 30/44] Removed multiple label lookup in Vision quickstart_spec.rb (using cat)! --- vision/spec/quickstart_spec.rb | 17 +---------------- 1 file changed, 1 insertion(+), 16 deletions(-) diff --git a/vision/spec/quickstart_spec.rb b/vision/spec/quickstart_spec.rb index af204d008..a13afa4c9 100644 --- a/vision/spec/quickstart_spec.rb +++ b/vision/spec/quickstart_spec.rb @@ -26,22 +26,7 @@ expect { load File.expand_path("../quickstart.rb", __dir__) }.to output( - "Labels:\n" + - "cat\n" + - "mammal\n" + - "vertebrate\n" + - "whiskers\n" + - "fauna\n" + - "cat like mammal\n" + - "small to medium sized cats\n" + - "grass\n" + - "tabby cat\n" + - "european shorthair\n" + - "wild cat\n" + - "domestic short haired cat\n" + - "rusty spotted cat\n" + - "abyssinian\n" + - "carnivoran\n" + /Labels:.*cat.*/m ).to_stdout end From 01cd1d855c202abf88e0a8ed00c8bbc9099864ba Mon Sep 17 00:00:00 2001 From: Frank Natividad Date: Tue, 11 Oct 2016 17:32:20 -0700 Subject: [PATCH 31/44] Added wait_until helper function and removed direct call to sleep --- logging/spec/quickstart_spec.rb | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/logging/spec/quickstart_spec.rb b/logging/spec/quickstart_spec.rb index 713864d18..ef64544b6 100644 --- a/logging/spec/quickstart_spec.rb +++ b/logging/spec/quickstart_spec.rb @@ -17,6 +17,15 @@ describe "Logging Quickstart" do + # Simple wait method. Test for condition 5 times, delaying 1 second each time + def wait_until times: 5, delay: 1, &condition + times.times do + return if condition.call + sleep delay + end + raise "Condition not met. Waited #{times} times with #{delay} sec delay" + end + it "logs a new entry" do entry_filter = 'logName:"my-log" textPayload:"Hello, world!"' gcloud = Google::Cloud.new ENV["GOOGLE_CLOUD_PROJECT"] @@ -38,7 +47,9 @@ "Logged Hello, world!\n" ).to_stdout - sleep(5) + wait_until(delay: 2) do + logging.entries(filter: entry_filter).any? + end entries = logging.entries filter: entry_filter expect(entries).to_not be_empty From 183ef4b22e0b2ffaf2cdd39210e5c210d16d8880 Mon Sep 17 00:00:00 2001 From: Frank Natividad Date: Wed, 12 Oct 2016 10:15:15 -0700 Subject: [PATCH 32/44] Changes: 1. Expanded spec to create and delete a new log_name for entries created during tests. 2. Removed unnecessary comments. --- logging/spec/quickstart_spec.rb | 36 +++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/logging/spec/quickstart_spec.rb b/logging/spec/quickstart_spec.rb index ef64544b6..a5faf987f 100644 --- a/logging/spec/quickstart_spec.rb +++ b/logging/spec/quickstart_spec.rb @@ -26,20 +26,30 @@ def wait_until times: 5, delay: 1, &condition raise "Condition not met. Waited #{times} times with #{delay} sec delay" end - it "logs a new entry" do - entry_filter = 'logName:"my-log" textPayload:"Hello, world!"' - gcloud = Google::Cloud.new ENV["GOOGLE_CLOUD_PROJECT"] - logging = gcloud.logging + before(:all) do + @gcloud = Google::Cloud.new ENV["GOOGLE_CLOUD_PROJECT"] + @logging = @gcloud.logging + @entry = @logging.entry - expect(Google::Cloud).to receive(:new).with("YOUR_PROJECT_ID"). - and_return(gcloud) + time_now = Time.now.to_f.to_s.split('.').first + @log_name = "sub-name-#{time_now}" + end - entries = logging.entries filter: entry_filter - unless entries.empty? - logging.delete_log "my-log" + after(:all) do + if @logging.entries(filter: "logName:\"#{@log_name}\"").any? + @logging.delete_log @log_name end + end - expect(logging.entries(filter: entry_filter)).to be_empty + it "logs a new entry" do + entry_filter = "logName:\"#{@log_name}\" textPayload:\"Hello, world!\"" + + expect(Google::Cloud).to receive(:new).with("YOUR_PROJECT_ID"). + and_return(@gcloud) + expect(@gcloud).to receive(:logging).and_return(@logging) + expect(@logging).to receive(:entry).and_return(@entry) + expect(@entry).to receive(:log_name) { @log_name } + expect(@logging.entries(filter: entry_filter)).to be_empty expect { load File.expand_path("../quickstart.rb", __dir__) @@ -47,11 +57,11 @@ def wait_until times: 5, delay: 1, &condition "Logged Hello, world!\n" ).to_stdout - wait_until(delay: 2) do - logging.entries(filter: entry_filter).any? + wait_until do + @logging.entries(filter: entry_filter).any? end - entries = logging.entries filter: entry_filter + entries = @logging.entries filter: entry_filter expect(entries).to_not be_empty end From a450c94a21977dbbe085dc9e8895e8f8d647315c Mon Sep 17 00:00:00 2001 From: Frank Natividad Date: Wed, 12 Oct 2016 14:17:43 -0700 Subject: [PATCH 33/44] Changes: 1. Updated sample_spec.rb to not expect a double-quote after Log message: 2. Increased timed delay in quickstart_spec.rb --- logging/spec/quickstart_spec.rb | 2 +- logging/spec/sample_spec.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/logging/spec/quickstart_spec.rb b/logging/spec/quickstart_spec.rb index a5faf987f..8cfd2164b 100644 --- a/logging/spec/quickstart_spec.rb +++ b/logging/spec/quickstart_spec.rb @@ -57,7 +57,7 @@ def wait_until times: 5, delay: 1, &condition "Logged Hello, world!\n" ).to_stdout - wait_until do + wait_until(delay: 5) do @logging.entries(filter: entry_filter).any? end diff --git a/logging/spec/sample_spec.rb b/logging/spec/sample_spec.rb index ee8dfc5d0..c94bb0735 100644 --- a/logging/spec/sample_spec.rb +++ b/logging/spec/sample_spec.rb @@ -144,7 +144,7 @@ def cleanup! timestamp = "\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2} [^\\\\]+" expect { list_log_entries }.to output( - %r{\[#{timestamp}\] #{my_application_log_name} "Log message"} + %r{\[#{timestamp}\] #{my_application_log_name} \"Log message} ).to_stdout end From 4881ae7636ad90ba3c81cc3d95fcec1e48aa3fc4 Mon Sep 17 00:00:00 2001 From: Frank Natividad Date: Wed, 12 Oct 2016 17:23:37 -0700 Subject: [PATCH 34/44] Modified expectation to attempt to fix Travis CI --- logging/spec/quickstart_spec.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/logging/spec/quickstart_spec.rb b/logging/spec/quickstart_spec.rb index 8cfd2164b..225ee0964 100644 --- a/logging/spec/quickstart_spec.rb +++ b/logging/spec/quickstart_spec.rb @@ -48,7 +48,9 @@ def wait_until times: 5, delay: 1, &condition and_return(@gcloud) expect(@gcloud).to receive(:logging).and_return(@logging) expect(@logging).to receive(:entry).and_return(@entry) - expect(@entry).to receive(:log_name) { @log_name } + allow(@entry).to receive(:log_name).and_wrap_original do |entry| + @log_name + end expect(@logging.entries(filter: entry_filter)).to be_empty expect { @@ -57,6 +59,7 @@ def wait_until times: 5, delay: 1, &condition "Logged Hello, world!\n" ).to_stdout + expect(@entry.log_name).to eq @log_name wait_until(delay: 5) do @logging.entries(filter: entry_filter).any? end From 1e97352f8849bbe94414379cfbba2fcadc543439 Mon Sep 17 00:00:00 2001 From: remi Taylor Date: Thu, 13 Oct 2016 14:04:33 -0700 Subject: [PATCH 35/44] Refactor Logging Quickstart spec and fix by adding order clause to entry fetch - logging.entries filter: was not working without additional order: option - simplify log name to "quickstart_log_#{Time.now.to_i}" - always try to delete log after spec runs, allowing for NotFound exceptions - refactor entry lookup into a helper method --- logging/spec/quickstart_spec.rb | 45 ++++++++++++++++++--------------- 1 file changed, 24 insertions(+), 21 deletions(-) diff --git a/logging/spec/quickstart_spec.rb b/logging/spec/quickstart_spec.rb index 225ee0964..a700ebd67 100644 --- a/logging/spec/quickstart_spec.rb +++ b/logging/spec/quickstart_spec.rb @@ -26,47 +26,50 @@ def wait_until times: 5, delay: 1, &condition raise "Condition not met. Waited #{times} times with #{delay} sec delay" end - before(:all) do - @gcloud = Google::Cloud.new ENV["GOOGLE_CLOUD_PROJECT"] - @logging = @gcloud.logging - @entry = @logging.entry + before do + @gcloud = Google::Cloud.new ENV["GOOGLE_CLOUD_PROJECT"] + @logging = @gcloud.logging + @entry = @logging.entry + @log_name = "quickstart_log_#{Time.now.to_i}" - time_now = Time.now.to_f.to_s.split('.').first - @log_name = "sub-name-#{time_now}" + @entry.log_name = @log_name end - after(:all) do - if @logging.entries(filter: "logName:\"#{@log_name}\"").any? + after do + begin @logging.delete_log @log_name + rescue Google::Cloud::NotFoundError end end - it "logs a new entry" do - entry_filter = "logName:\"#{@log_name}\" textPayload:\"Hello, world!\"" + def test_log_entries + @logging.entries filter: %{logName:"#{@log_name}"}, order: "timestamp desc" + end + it "logs a new entry" do expect(Google::Cloud).to receive(:new).with("YOUR_PROJECT_ID"). and_return(@gcloud) expect(@gcloud).to receive(:logging).and_return(@logging) expect(@logging).to receive(:entry).and_return(@entry) - allow(@entry).to receive(:log_name).and_wrap_original do |entry| - @log_name - end - expect(@logging.entries(filter: entry_filter)).to be_empty + allow(@entry).to receive(:log_name=).with("my-log") + expect(test_log_entries).to be_empty + expect { load File.expand_path("../quickstart.rb", __dir__) }.to output( "Logged Hello, world!\n" ).to_stdout - expect(@entry.log_name).to eq @log_name - wait_until(delay: 5) do - @logging.entries(filter: entry_filter).any? - end + entries = [] - entries = @logging.entries filter: entry_filter - expect(entries).to_not be_empty - end + wait_until { entries = test_log_entries; entries.any? } + expect(entries).not_to be_empty + expect(entries.length).to eq 1 + + entry = entries.first + expect(entry.payload).to eq "Hello, world!" + end end From c0586e70d0cafaf99af626c060fdc1cfe06efbbc Mon Sep 17 00:00:00 2001 From: Frank Natividad Date: Thu, 13 Oct 2016 16:57:10 -0700 Subject: [PATCH 36/44] Removed some spacing found by Vim --- logging/spec/quickstart_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/logging/spec/quickstart_spec.rb b/logging/spec/quickstart_spec.rb index a700ebd67..ab9f320d0 100644 --- a/logging/spec/quickstart_spec.rb +++ b/logging/spec/quickstart_spec.rb @@ -54,7 +54,7 @@ def test_log_entries allow(@entry).to receive(:log_name=).with("my-log") expect(test_log_entries).to be_empty - + expect { load File.expand_path("../quickstart.rb", __dir__) }.to output( From 73b620be846cedda39f6a6771556377b561ce648 Mon Sep 17 00:00:00 2001 From: Frank Natividad Date: Thu, 13 Oct 2016 17:36:49 -0700 Subject: [PATCH 37/44] Changed %{} to %Q{} to be more explicit. --- logging/spec/quickstart_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/logging/spec/quickstart_spec.rb b/logging/spec/quickstart_spec.rb index ab9f320d0..dbceae489 100644 --- a/logging/spec/quickstart_spec.rb +++ b/logging/spec/quickstart_spec.rb @@ -43,7 +43,7 @@ def wait_until times: 5, delay: 1, &condition end def test_log_entries - @logging.entries filter: %{logName:"#{@log_name}"}, order: "timestamp desc" + @logging.entries filter: %Q{logName:"#{@log_name}"}, order: "timestamp desc" end it "logs a new entry" do From 0a17905718adf1d8e89c15558ea0b4547c32366d Mon Sep 17 00:00:00 2001 From: Frank Natividad Date: Tue, 18 Oct 2016 11:02:35 -0700 Subject: [PATCH 38/44] Removed order from filter. Using full project path --- logging/spec/quickstart_spec.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/logging/spec/quickstart_spec.rb b/logging/spec/quickstart_spec.rb index dbceae489..16b6f8f69 100644 --- a/logging/spec/quickstart_spec.rb +++ b/logging/spec/quickstart_spec.rb @@ -30,7 +30,8 @@ def wait_until times: 5, delay: 1, &condition @gcloud = Google::Cloud.new ENV["GOOGLE_CLOUD_PROJECT"] @logging = @gcloud.logging @entry = @logging.entry - @log_name = "quickstart_log_#{Time.now.to_i}" + @log_name = "projects/#{ENV["GOOGLE_CLOUD_PROJECT"]}/logs/" + + "quickstart_log_#{Time.now.to_i}" @entry.log_name = @log_name end @@ -43,7 +44,7 @@ def wait_until times: 5, delay: 1, &condition end def test_log_entries - @logging.entries filter: %Q{logName:"#{@log_name}"}, order: "timestamp desc" + @logging.entries filter: %Q{logName="#{@log_name}"} end it "logs a new entry" do From 0e6e529e47450a1d7e3c830bff3b33416518eea5 Mon Sep 17 00:00:00 2001 From: Frank Natividad Date: Wed, 19 Oct 2016 11:34:15 -0700 Subject: [PATCH 39/44] Modified quickstart spec test to check poliarity --- language/spec/quickstart_spec.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/language/spec/quickstart_spec.rb b/language/spec/quickstart_spec.rb index c1587a17b..795e5d6d8 100644 --- a/language/spec/quickstart_spec.rb +++ b/language/spec/quickstart_spec.rb @@ -26,8 +26,7 @@ expect { load File.expand_path("../quickstart.rb", __dir__) }.to output( - "Text: Hello, world!\n" + - "Sentiment: 1.0, 0.6000000238418579\n" + /Text: Hello, world!\nSentiment: 1.0/ ).to_stdout end From b7eef07522660219ee1238b81cebe7b11da5c770 Mon Sep 17 00:00:00 2001 From: Frank Natividad Date: Thu, 20 Oct 2016 12:39:03 -0700 Subject: [PATCH 40/44] Language Removed _client, _test_client, and shabang line --- language/quickstart.rb | 4 ++-- language/spec/quickstart_spec.rb | 5 ++--- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/language/quickstart.rb b/language/quickstart.rb index 822b74c71..168245e46 100644 --- a/language/quickstart.rb +++ b/language/quickstart.rb @@ -21,11 +21,11 @@ # Instantiates a client gcloud = Google::Cloud.new project_id -language_client = gcloud.language +language = gcloud.language # The text to analyze text = "Hello, world!" -document = language_client.document text +document = language.document text # Detects the sentiment of the text sentiment = document.sentiment diff --git a/language/spec/quickstart_spec.rb b/language/spec/quickstart_spec.rb index 795e5d6d8..98988bb34 100644 --- a/language/spec/quickstart_spec.rb +++ b/language/spec/quickstart_spec.rb @@ -1,4 +1,3 @@ -#!/usr/bin/ruby # Copyright 2016 Google, Inc # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -19,9 +18,9 @@ describe "Language Quickstart" do it "detect sentiment" do - gcloud_test_client = Google::Cloud.new ENV["GOOGLE_CLOUD_PROJECT"] + gcloud = Google::Cloud.new ENV["GOOGLE_CLOUD_PROJECT"] expect(Google::Cloud).to receive(:new).with("YOUR_PROJECT_ID"). - and_return(gcloud_test_client) + and_return(gcloud) expect { load File.expand_path("../quickstart.rb", __dir__) From 344732757ec8036d8a544a96a9366ce511c683f9 Mon Sep 17 00:00:00 2001 From: Frank Natividad Date: Thu, 20 Oct 2016 12:41:23 -0700 Subject: [PATCH 41/44] Speech quickstart changed fileName -> file_name --- speech/quickstart.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/speech/quickstart.rb b/speech/quickstart.rb index d11d4c688..5a0cef265 100644 --- a/speech/quickstart.rb +++ b/speech/quickstart.rb @@ -24,10 +24,10 @@ speech = gcloud.speech # The name of the audio file to transcribe -fileName = "./audio_files/audio.raw" +file_name = "./audio_files/audio.raw" # The audio file's encoding and sample rate -audio = speech.audio fileName, encoding: :raw, sample_rate: 16000 +audio = speech.audio file_name, encoding: :raw, sample_rate: 16000 # Detects speech in the audio file results = audio.recognize From ff23358c00ba564ded36368e2f94a11efe0d37cf Mon Sep 17 00:00:00 2001 From: Frank Natividad Date: Thu, 20 Oct 2016 12:43:04 -0700 Subject: [PATCH 42/44] Vision quickstart: changed fileName -> file_name --- vision/quickstart.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/vision/quickstart.rb b/vision/quickstart.rb index c17e3a4e6..75fd33dda 100644 --- a/vision/quickstart.rb +++ b/vision/quickstart.rb @@ -24,10 +24,10 @@ vision = gcloud.vision # The name of the image file to annotate -fileName = "./images/cat.jpg" +file_name = "./images/cat.jpg" # Performs label detection on the image file -labels = vision.image(fileName).labels +labels = vision.image(file_name).labels puts "Labels:" labels.each do |label| From 64953d37c9dc715cc3645f2a393d894f22cf0a53 Mon Sep 17 00:00:00 2001 From: Frank Natividad Date: Thu, 20 Oct 2016 12:43:27 -0700 Subject: [PATCH 43/44] Vision quickstart spec: added better description --- vision/spec/quickstart_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vision/spec/quickstart_spec.rb b/vision/spec/quickstart_spec.rb index a13afa4c9..d73a995e0 100644 --- a/vision/spec/quickstart_spec.rb +++ b/vision/spec/quickstart_spec.rb @@ -17,7 +17,7 @@ describe "Vision Quickstart" do - it "label a cat image" do + it "performs label detection on a sample image file" do gcloud = Google::Cloud.new ENV["GOOGLE_CLOUD_PROJECT"] expect(Google::Cloud).to receive(:new).with("YOUR_PROJECT_ID"). From 579692507c9aedc70ede264473dc4208e2c702cd Mon Sep 17 00:00:00 2001 From: Frank Natividad Date: Thu, 20 Oct 2016 15:45:48 -0700 Subject: [PATCH 44/44] Language/quickstart.rb Remove unwanted spacing. --- language/quickstart.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/language/quickstart.rb b/language/quickstart.rb index 168245e46..bf472c2a1 100644 --- a/language/quickstart.rb +++ b/language/quickstart.rb @@ -20,7 +20,7 @@ project_id = "YOUR_PROJECT_ID" # Instantiates a client -gcloud = Google::Cloud.new project_id +gcloud = Google::Cloud.new project_id language = gcloud.language # The text to analyze