-
Notifications
You must be signed in to change notification settings - Fork 209
Add new "quickstart" samples. #77
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 12 commits
97b97e9
beab134
318d761
bb13651
a44f362
48b30fe
4bcdf07
4f1b02c
e81e741
2590342
5e9c228
740c7cd
ca30bc2
b41be6a
252f7a0
88e99a5
c82d70c
b426429
135527c
99c4689
c389c77
019932c
87386ef
2d1367f
71a0f2f
c2994e0
73fdf64
97f366e
3249d27
e522da5
01cd1d8
183ef4b
7b31107
a450c94
4881ae7
1e97352
a06b18a
c0586e7
73b620b
0a17905
0e6e529
b7eef07
3447327
ff23358
64953d3
5796925
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -36,3 +36,4 @@ Commands: | |
| query <query> | ||
| query_job <query> | ||
| ``` | ||
|
|
||
| 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 | ||
| 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] | ||
|
|
||
| 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 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we remove all of the comments from the specs?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes! |
||
| gcloud_test_client = Google::Cloud.new ENV["GOOGLE_CLOUD_PROJECT"] | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Understood.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's change these to just I liked how explicit But now that I'm going back and reading these... I'd like to use the simple, consistent #consistency #nit
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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") | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I love this! Let's just fix these to be
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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__)
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 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.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, users pay for all Google Cloud Platform usage
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
|
||
| 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 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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] | ||
|
|
||
| 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 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
| 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 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
| # 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] | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| #!/usr/bin/ruby | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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"] | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| 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"+ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add space before |
||
| "Sentiment: 1.0, 0.6000000238418579\n" | ||
| ).to_stdout | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove trailing space in example block |
||
| end | ||
|
|
||
| end | ||
|
|
||
| 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 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove method wrappers for samples, just wrap in |
||
| # [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 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove along with wrapper methods |
||
| run_quickstart | ||
| end | ||
| 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 |
There was a problem hiding this comment.
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
bigquerynotbigquery_client:https://googlecloudplatform.github.io/google-cloud-ruby/#/docs/google-cloud/v0.20.1/google/cloud/bigquery
Also, we use
bigqueryin our code samples and never use a_clientsuffix: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:
There was a problem hiding this comment.
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/#/
And all other google-cloud-ruby snippets follow the following template: