diff --git a/bigquery/README.md b/bigquery/README.md index ef15ce784..0cc7c985d 100644 --- a/bigquery/README.md +++ b/bigquery/README.md @@ -36,3 +36,4 @@ Commands: query query_job ``` + diff --git a/bigquery/quickstart.rb b/bigquery/quickstart.rb new file mode 100644 index 000000000..d9cf1850d --- /dev/null +++ b/bigquery/quickstart.rb @@ -0,0 +1,34 @@ +# 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. + +# [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 = gcloud.bigquery + +# The name for the new dataset +dataset_name = "my_new_dataset" + +# Creates the new dataset +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 new file mode 100644 index 000000000..33cf3474f --- /dev/null +++ b/bigquery/spec/quickstart_spec.rb @@ -0,0 +1,43 @@ +# 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 "BigQuery Quickstart" do + + it "creates a new dataset" do + gcloud = Google::Cloud.new ENV["GOOGLE_CLOUD_PROJECT"] + bigquery = gcloud.bigquery + + if bigquery.dataset "my_new_dataset" + bigquery.dataset("my_new_dataset").delete + end + + expect(bigquery.dataset "my_new_dataset").to be nil + expect(Google::Cloud).to receive(:new). + with("YOUR_PROJECT_ID"). + and_return(gcloud) + + expect { + load File.expand_path("../quickstart.rb", __dir__) + }.to output( + "Dataset my_new_dataset created\.\n" + ).to_stdout + + expect(bigquery.dataset "my_new_dataset").not_to be nil + end + +end + diff --git a/datastore/quickstart.rb b/datastore/quickstart.rb new file mode 100644 index 000000000..be2c72304 --- /dev/null +++ b/datastore/quickstart.rb @@ -0,0 +1,43 @@ +# 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. + +# [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 = 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.key kind, name + +# Prepares the new entity +task = datastore.entity task_key do |t| + t["description"] = "Buy milk" +end + +# Saves the entity +datastore.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..c8f2e5b42 --- /dev/null +++ b/datastore/spec/quickstart_spec.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. + +require "rspec" +require "google/cloud" + +describe "Datastore Quickstart" do + + it "creates a new entity" do + gcloud = Google::Cloud.new ENV["GOOGLE_CLOUD_PROJECT"] + datastore = gcloud.datastore + task_key = datastore.key "Task", "sampletask1" + + if datastore.find task_key + task = datastore.find task_key + datastore.delete task + end + + expect(datastore.find task_key).to be nil + expect(Google::Cloud).to receive(:new).with("YOUR_PROJECT_ID"). + and_return(gcloud) + + expect { + load File.expand_path("../quickstart.rb", __dir__) + }.to output { + "Saved Task: Buy milk\n" + }.to_stdout + + task_key = datastore.find(task_key) + expect(task_key).not_to be nil + expect(task_key["description"]).to eq "Buy milk" + end + +end diff --git a/language/quickstart.rb b/language/quickstart.rb new file mode 100644 index 000000000..bf472c2a1 --- /dev/null +++ b/language/quickstart.rb @@ -0,0 +1,36 @@ +# 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. + +# [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 = gcloud.language + +# The text to analyze +text = "Hello, world!" +document = language.document text + +# Detects the sentiment of the text +sentiment = document.sentiment + +puts "Text: #{text}" +puts "Sentiment: #{sentiment.polarity}, #{sentiment.magnitude}" +# [END language_quickstart] + diff --git a/language/spec/quickstart_spec.rb b/language/spec/quickstart_spec.rb new file mode 100644 index 000000000..98988bb34 --- /dev/null +++ b/language/spec/quickstart_spec.rb @@ -0,0 +1,33 @@ +# 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 sentiment" do + gcloud = Google::Cloud.new ENV["GOOGLE_CLOUD_PROJECT"] + expect(Google::Cloud).to receive(:new).with("YOUR_PROJECT_ID"). + and_return(gcloud) + + expect { + load File.expand_path("../quickstart.rb", __dir__) + }.to output( + /Text: Hello, world!\nSentiment: 1.0/ + ).to_stdout + end + +end + diff --git a/logging/quickstart.rb b/logging/quickstart.rb new file mode 100644 index 000000000..eb88ff348 --- /dev/null +++ b/logging/quickstart.rb @@ -0,0 +1,40 @@ +# 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. + +# [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 = 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.write_entries entry + +puts "Logged #{entry.payload}" +# [END logging_quickstart] + diff --git a/logging/spec/quickstart_spec.rb b/logging/spec/quickstart_spec.rb new file mode 100644 index 000000000..16b6f8f69 --- /dev/null +++ b/logging/spec/quickstart_spec.rb @@ -0,0 +1,76 @@ +# 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 + + # 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 + + before do + @gcloud = Google::Cloud.new ENV["GOOGLE_CLOUD_PROJECT"] + @logging = @gcloud.logging + @entry = @logging.entry + @log_name = "projects/#{ENV["GOOGLE_CLOUD_PROJECT"]}/logs/" + + "quickstart_log_#{Time.now.to_i}" + + @entry.log_name = @log_name + end + + after do + begin + @logging.delete_log @log_name + rescue Google::Cloud::NotFoundError + end + end + + def test_log_entries + @logging.entries filter: %Q{logName="#{@log_name}"} + 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=).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 + + entries = [] + + 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 + 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 diff --git a/pubsub/quickstart.rb b/pubsub/quickstart.rb new file mode 100644 index 000000000..32899941b --- /dev/null +++ b/pubsub/quickstart.rb @@ -0,0 +1,34 @@ +# 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. + +# [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 = gcloud.pubsub + +# The name for the new topic +topic_name = "my-new-topic" + +# Creates the new topic +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 new file mode 100644 index 000000000..4f320e3d7 --- /dev/null +++ b/pubsub/spec/quickstart_spec.rb @@ -0,0 +1,43 @@ +# 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 + gcloud = Google::Cloud.new ENV["GOOGLE_CLOUD_PROJECT"] + pubsub = gcloud.pubsub + + if pubsub.topic "my-new-topic" + pubsub.topic("my-new-topic").delete + end + + expect(pubsub.topic "my-new-topic").to be nil + expect(Google::Cloud).to receive(:new). + with("YOUR_PROJECT_ID"). + and_return(gcloud) + + 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.topic "my-new-topic").not_to be nil + end + +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..5a0cef265 --- /dev/null +++ b/speech/quickstart.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. + +# [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 = gcloud.speech + +# The name of the audio file to transcribe +file_name = "./audio_files/audio.raw" + +# The audio file's encoding and sample rate +audio = speech.audio file_name, encoding: :raw, sample_rate: 16000 + +# Detects speech in the audio file +results = audio.recognize +result = results.first + +puts "Transcription: #{result.transcript}" +# [END speech_quickstart] + diff --git a/speech/spec/quickstart_spec.rb b/speech/spec/quickstart_spec.rb new file mode 100644 index 000000000..77a735e48 --- /dev/null +++ b/speech/spec/quickstart_spec.rb @@ -0,0 +1,33 @@ +# 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 + gcloud = Google::Cloud.new ENV["GOOGLE_CLOUD_PROJECT"] + + expect(Google::Cloud).to receive(:new).with("YOUR_PROJECT_ID"). + and_return(gcloud) + + expect { + load File.expand_path("../quickstart.rb", __dir__) + }.to output( + "Transcription: how old is the Brooklyn Bridge\n" + ).to_stdout + end + +end diff --git a/storage/quickstart.rb b/storage/quickstart.rb new file mode 100644 index 000000000..e75d8bd9e --- /dev/null +++ b/storage/quickstart.rb @@ -0,0 +1,34 @@ +# 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. + +# [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 = gcloud.storage + +# The name for the new bucket +bucket_name = "my-new-bucket" + +# Creates the new bucket +bucket = storage.create_bucket bucket_name + +puts "Bucket #{bucket.name} was created." +# [END storage_quickstart] + diff --git a/storage/spec/quickstart_spec.rb b/storage/spec/quickstart_spec.rb new file mode 100644 index 000000000..7037bc0fe --- /dev/null +++ b/storage/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 "Storage Quickstart" do + + it "creates a new bucket" do + gcloud = Google::Cloud.new ENV["GOOGLE_CLOUD_PROJECT"] + storage = gcloud.storage + bucket_name = ENV["STORAGE_BUCKET"] + + if storage.bucket bucket_name + 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) + + 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) + + 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 + diff --git a/translate/quickstart.rb b/translate/quickstart.rb new file mode 100644 index 000000000..8827fe3f6 --- /dev/null +++ b/translate/quickstart.rb @@ -0,0 +1,37 @@ +# 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. + +# [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 = gcloud.translate api_key + +# The text to translate +text = "Hello, world!" +# The target language +target = "ru" + +# Translates some text into Russian +translation = translate.translate text, to: target + +puts "Text: #{text}" +puts "Translation: #{translation}" +# [END translate_quickstart] + diff --git a/translate/spec/quickstart_spec.rb b/translate/spec/quickstart_spec.rb new file mode 100644 index 000000000..e06e385bc --- /dev/null +++ b/translate/spec/quickstart_spec.rb @@ -0,0 +1,36 @@ +# 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 + 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) + + expect { + load File.expand_path("../quickstart.rb", __dir__) + }.to output( + "Text: Hello, world!\n"+ + "Translation: Привет мир!\n" + ).to_stdout + end + +end + diff --git a/vision/quickstart.rb b/vision/quickstart.rb new file mode 100644 index 000000000..75fd33dda --- /dev/null +++ b/vision/quickstart.rb @@ -0,0 +1,37 @@ +# 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. + +# [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 = gcloud.vision + +# The name of the image file to annotate +file_name = "./images/cat.jpg" + +# Performs label detection on the image file +labels = vision.image(file_name).labels + +puts "Labels:" +labels.each do |label| + puts label.description +end +# [END vision_quickstart] + diff --git a/vision/spec/quickstart_spec.rb b/vision/spec/quickstart_spec.rb new file mode 100644 index 000000000..d73a995e0 --- /dev/null +++ b/vision/spec/quickstart_spec.rb @@ -0,0 +1,33 @@ +# 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 "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"). + and_return(gcloud) + + expect { + load File.expand_path("../quickstart.rb", __dir__) + }.to output( + /Labels:.*cat.*/m + ).to_stdout + end + +end