Skip to content
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
97b97e9
Add "quickstart" samples.
jmdobry Sep 14, 2016
beab134
Tweak Datastore quickstart sample.
jmdobry Oct 5, 2016
318d761
Tweak Datastore quickstart sample.
jmdobry Oct 5, 2016
bb13651
Change datastore quickstart sample to create a task.
jmdobry Oct 6, 2016
a44f362
Changes:
Oct 6, 2016
48b30fe
Changes:
Oct 6, 2016
4bcdf07
Changes:
Oct 7, 2016
4f1b02c
Changes:
Oct 7, 2016
e81e741
Changes:
Oct 7, 2016
2590342
Changes:
Oct 8, 2016
5e9c228
Changes:
Oct 8, 2016
740c7cd
Simplify print statement in Vision quickstart.
jmdobry Oct 10, 2016
ca30bc2
Changes:
Oct 10, 2016
b41be6a
Changes:
Oct 10, 2016
252f7a0
Changes:
Oct 10, 2016
88e99a5
Changes:
Oct 10, 2016
c82d70c
Changes:
Oct 10, 2016
b426429
Changes:
Oct 10, 2016
135527c
Changes:
Oct 10, 2016
99c4689
Changes:
Oct 10, 2016
c389c77
Changes:
Oct 11, 2016
019932c
Changes:
Oct 11, 2016
87386ef
Changes:
Oct 11, 2016
2d1367f
Changes:
Oct 11, 2016
71a0f2f
Changes:
Oct 11, 2016
c2994e0
Changes:
Oct 11, 2016
73fdf64
Removed extra spaces in logging/quickstart.rb
Oct 11, 2016
97f366e
Removed %Q{} and used single-quotes instead
Oct 11, 2016
3249d27
Removed spaces from assignment in trasnlate
Oct 11, 2016
e522da5
Removed multiple label lookup in Vision quickstart_spec.rb (using cat)!
Oct 12, 2016
01cd1d8
Added wait_until helper function and removed direct call to sleep
Oct 12, 2016
183ef4b
Changes:
Oct 12, 2016
7b31107
Merge branch 'master' of github.com:GoogleCloudPlatform/ruby-docs-sam…
Oct 12, 2016
a450c94
Changes:
Oct 12, 2016
4881ae7
Modified expectation to attempt to fix Travis CI
Oct 13, 2016
1e97352
Refactor Logging Quickstart spec and fix by adding order clause to en…
Oct 13, 2016
a06b18a
Merge pull request #90 from GoogleCloudPlatform/logging-quickstart-spec
frankyn Oct 13, 2016
c0586e7
Removed some spacing found by Vim
Oct 13, 2016
73b620b
Changed %{} to %Q{} to be more explicit.
Oct 14, 2016
0a17905
Removed order from filter. Using full project path
Oct 18, 2016
0e6e529
Modified quickstart spec test to check poliarity
Oct 19, 2016
b7eef07
Language Removed _client, _test_client, and shabang line
Oct 20, 2016
3447327
Speech quickstart changed fileName -> file_name
Oct 20, 2016
ff23358
Vision quickstart: changed fileName -> file_name
Oct 20, 2016
64953d3
Vision quickstart spec: added better description
Oct 20, 2016
5796925
Language/quickstart.rb Remove unwanted spacing.
Oct 20, 2016
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions bigquery/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,4 @@ Commands:
query <query>
query_job <query>
```

34 changes: 34 additions & 0 deletions bigquery/quickstart.rb
Original file line number Diff line number Diff line change
@@ -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
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because we already have a comment with the wording "Instantiates a client", I don't think we need to call the BigQuery client bigquery_client. I really like the clarity this adds but no other samples use this working.

All samples on the client library website use bigquery not bigquery_client:
https://googlecloudplatform.github.io/google-cloud-ruby/#/docs/google-cloud/v0.20.1/google/cloud/bigquery

Also, we use bigquery in our code samples and never use a _client suffix:
https://github.com/GoogleCloudPlatform/ruby-docs-samples/blob/2016.09.29/bigquery/tables.rb#L24

Let's update the syntax of the quickstarts to look more idiomatic, like so:

# Instantiates a client
gcloud   = Google::Cloud.new project_id
bigquery = gcloud.bigquery

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jmdobry Is it critically important for every quickstart to have variables suffixed with _client?

No other Ruby samples use this, including google-cloud-ruby quickstarts. I would like the quickstart code to be conventional with code seen everywhere else.

For example, google-cloud-ruby has a quickstart on its homepage at: https://googlecloudplatform.github.io/google-cloud-ruby/#/

require "google/cloud"

gcloud = Google::Cloud.new "my-todo-project-id",
                           "/path/to/keyfile.json"
datastore = gcloud.datastore

product = datastore.find "Product", 123

And all other google-cloud-ruby snippets follow the following template:

require "google/cloud"

gcloud = Google::Cloud.new
storage = gcloud.storage

all_buckets = storage.buckets

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]

50 changes: 50 additions & 0 deletions bigquery/spec/quickstart_spec.rb
Original file line number Diff line number Diff line change
@@ -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 "BigQuery Quickstart" do

it "creates a new dataset" do
# Initialize test objects
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we remove all of the comments from the specs?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes!

gcloud_test_client = Google::Cloud.new ENV["GOOGLE_CLOUD_PROJECT"]
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small #nit - Align variable assignments

Alignment of variable assignments is a common Ruby idiom and it is particularly useful for improving the readability of our code samples. So let's try to be consistent and always align (when it makes sense to do so)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Understood.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's change these to just gcloud and bigquery like is used everywhere else.

I liked how explicit gcloud_test_client is when initially drafting these specs to be super clear and self-document how these specs work:
https://gist.github.com/remi/f3abdee163a20e56c4d678c10e088d09

But now that I'm going back and reading these... I'd like to use the simple, consistent gcloud and project_name variables for these)

#consistency #nit

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it!

bigquery_test_client = gcloud_test_client.bigquery

# Prime BigQuery for test
if bigquery_test_client.dataset "my_new_dataset"
bigquery_test_client.dataset("my_new_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)

# Run quickstart
expect {
load File::expand_path("quickstart.rb")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I love this! Let's just fix these to be File.expand_path sans the ::

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should also specify the full path to the quickstart relative to the spec directory (otherwise this uses the current working directory). So:

load File.expand_path("../quickstart.rb", __dir__)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Understood!

}.to output(
"Dataset my_new_dataset created\.\n"
).to_stdout

expect(bigquery_test_client.dataset "my_new_dataset").not_to be nil

# Clean up
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This won't run if any of the above expectations fail.

Any cleanup should be placed in an after block to ensure that it runs.

Because this example ensures that it runs no existing dataset (does the deletion before it runs the snippet code), I vote to remove this cleanup. Dataset will be left behind, but it will always be deleted before the example runs to ensure that the test setup is correct.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If someone outside of Google runs the test do they have to pay for the Bigquery dataset?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, users pay for all Google Cloud Platform usage

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it.

dataset = bigquery_test_client.dataset "my_new_dataset"
dataset.delete
end

end

43 changes: 43 additions & 0 deletions datastore/quickstart.rb
Original file line number Diff line number Diff line change
@@ -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_client = gcloud.datastore
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_client

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Understood.


# 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]

50 changes: 50 additions & 0 deletions datastore/spec/quickstart_spec.rb
Original file line number Diff line number Diff line change
@@ -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
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's check that the sampletask's description is "Buy milk" in addition to verifying that the entity now exists

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thoughts...

it "creates a new entity" do
  gcloud     = Google::Cloud.new ENV["GOOGLE_CLOUD_PROJECT"]
  datastore  = gcloud.datastore
  sampletask = datastore.find "Task", "sampletask1"

  datastore.delete sampletask if sampletask

  expect(datastore.find "Task", "sampletask1").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

  sampletask = datastore.find "Task", "sampletask1"

  expect(sampletask).not_to be nil
  expect(sampletask["description"]).to eq "Buy milk"
end


# Clean up
task = datastore_test_client.find task_key_test_client
datastore_test_client.delete task
end

end
36 changes: 36 additions & 0 deletions language/quickstart.rb
Original file line number Diff line number Diff line change
@@ -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_client = gcloud.language
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

language_client --> 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]

38 changes: 38 additions & 0 deletions language/spec/quickstart_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/usr/bin/ruby
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove shabang line

# 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
# Initialize test objects
gcloud_test_client = Google::Cloud.new ENV["GOOGLE_CLOUD_PROJECT"]
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gcloud_test_client --> gloud

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"+
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add space before +

"Sentiment: 1.0, 0.6000000238418579\n"
).to_stdout

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove trailing space in example block

end

end

45 changes: 45 additions & 0 deletions logging/quickstart.rb
Original file line number Diff line number Diff line change
@@ -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
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove method wrappers for samples, just wrap in [START... and [END... region comments

# [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
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove along with wrapper methods

run_quickstart
end
39 changes: 39 additions & 0 deletions pubsub/quickstart.rb
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions speech/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
source "https://rubygems.org"

gem "google-api-client"
gem "google-cloud-speech"

group :test do
gem "rspec"
Expand Down
Loading