-
Notifications
You must be signed in to change notification settings - Fork 211
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 24 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 = 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] | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 "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 | ||
|
|
||
| expect(bigquery.dataset "my_new_dataset").to be nil | ||
| 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( | ||
| "Dataset my_new_dataset created\.\n" | ||
| ).to_stdout | ||
|
|
||
| expect(bigquery.dataset "my_new_dataset").not_to be nil | ||
| 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" | ||
|
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. Do not align these. There are newlines with comments between them and it is not idiomatic to align these.
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 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,49 @@ | ||
| # 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 = 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 | ||
| end | ||
|
|
||
| expect(datastore.find task_key).to be nil | ||
| 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" | ||
| 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,37 @@ | ||
| #!/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", __dir__) | ||
| }.to output( | ||
| "Text: Hello, world!\n" + | ||
| "Sentiment: 1.0, 0.6000000238418579\n" | ||
| ).to_stdout | ||
| end | ||
|
|
||
| end | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 | ||
|
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. There is a ton of extra space characters here the need to be removed
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 |
||
| 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] | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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!"} | ||
|
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. Please do not use any Use a single quoted string
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 |
||
| 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) | ||
|
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. Do not use Why is 5 seconds chosen? Can this take longer than 5 seconds and is that acceptable? Would it work sometimes if only 1 second were waited for? Recommendation: copy/paste
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. It was a simple guess nothing exact. I changed the direct sleep call to use wait_until instead. |
||
|
|
||
| # Did we actually write a new entry? | ||
| entries = logging.entries filter: entry_filter | ||
| expect(entries).to_not be_empty | ||
| end | ||
|
|
||
| end | ||
|
|
||
| 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 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] | ||
|
|
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.
Can we remove all of the comments from the specs?
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.
Yes!