Skip to content

Commit ca91692

Browse files
author
remi Taylor
authored
Merge pull request #77 from GoogleCloudPlatform/quickstarts
Add new "quickstart" samples.
2 parents 03740bd + 5796925 commit ca91692

22 files changed

+748
-1
lines changed

bigquery/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,4 @@ Commands:
3636
query <query>
3737
query_job <query>
3838
```
39+

bigquery/quickstart.rb

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Copyright 2016 Google, Inc
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# [START bigquery_quickstart]
16+
# Imports the Google Cloud client library
17+
require "google/cloud"
18+
19+
# Your Google Cloud Platform project ID
20+
project_id = "YOUR_PROJECT_ID"
21+
22+
# Instantiates a client
23+
gcloud = Google::Cloud.new project_id
24+
bigquery = gcloud.bigquery
25+
26+
# The name for the new dataset
27+
dataset_name = "my_new_dataset"
28+
29+
# Creates the new dataset
30+
dataset = bigquery.create_dataset dataset_name
31+
32+
puts "Dataset #{dataset.dataset_id} created."
33+
# [END bigquery_quickstart]
34+

bigquery/spec/quickstart_spec.rb

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Copyright 2016 Google, Inc
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
require "rspec"
16+
require "google/cloud"
17+
18+
describe "BigQuery Quickstart" do
19+
20+
it "creates a new dataset" do
21+
gcloud = Google::Cloud.new ENV["GOOGLE_CLOUD_PROJECT"]
22+
bigquery = gcloud.bigquery
23+
24+
if bigquery.dataset "my_new_dataset"
25+
bigquery.dataset("my_new_dataset").delete
26+
end
27+
28+
expect(bigquery.dataset "my_new_dataset").to be nil
29+
expect(Google::Cloud).to receive(:new).
30+
with("YOUR_PROJECT_ID").
31+
and_return(gcloud)
32+
33+
expect {
34+
load File.expand_path("../quickstart.rb", __dir__)
35+
}.to output(
36+
"Dataset my_new_dataset created\.\n"
37+
).to_stdout
38+
39+
expect(bigquery.dataset "my_new_dataset").not_to be nil
40+
end
41+
42+
end
43+

datastore/quickstart.rb

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Copyright 2016 Google, Inc
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# [START datastore_quickstart]
16+
# Imports the Google Cloud client library
17+
require "google/cloud"
18+
19+
# Your Google Cloud Platform project ID
20+
project_id = "YOUR_PROJECT_ID"
21+
22+
# Instantiates a client
23+
gcloud = Google::Cloud.new project_id
24+
datastore = gcloud.datastore
25+
26+
# The kind for the new entity
27+
kind = "Task"
28+
# The name/ID for the new entity
29+
name = "sampletask1"
30+
# The Cloud Datastore key for the new entity
31+
task_key = datastore.key kind, name
32+
33+
# Prepares the new entity
34+
task = datastore.entity task_key do |t|
35+
t["description"] = "Buy milk"
36+
end
37+
38+
# Saves the entity
39+
datastore.save task
40+
41+
puts "Saved #{task.key.name}: #{task['description']}"
42+
# [END datastore_quickstart]
43+

datastore/spec/quickstart_spec.rb

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Copyright 2016 Google, Inc
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
require "rspec"
16+
require "google/cloud"
17+
18+
describe "Datastore Quickstart" do
19+
20+
it "creates a new entity" do
21+
gcloud = Google::Cloud.new ENV["GOOGLE_CLOUD_PROJECT"]
22+
datastore = gcloud.datastore
23+
task_key = datastore.key "Task", "sampletask1"
24+
25+
if datastore.find task_key
26+
task = datastore.find task_key
27+
datastore.delete task
28+
end
29+
30+
expect(datastore.find task_key).to be nil
31+
expect(Google::Cloud).to receive(:new).with("YOUR_PROJECT_ID").
32+
and_return(gcloud)
33+
34+
expect {
35+
load File.expand_path("../quickstart.rb", __dir__)
36+
}.to output {
37+
"Saved Task: Buy milk\n"
38+
}.to_stdout
39+
40+
task_key = datastore.find(task_key)
41+
expect(task_key).not_to be nil
42+
expect(task_key["description"]).to eq "Buy milk"
43+
end
44+
45+
end

language/quickstart.rb

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Copyright 2016 Google, Inc
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# [START language_quickstart]
16+
# Imports the Google Cloud client library
17+
require "google/cloud"
18+
19+
# Your Google Cloud Platform project ID
20+
project_id = "YOUR_PROJECT_ID"
21+
22+
# Instantiates a client
23+
gcloud = Google::Cloud.new project_id
24+
language = gcloud.language
25+
26+
# The text to analyze
27+
text = "Hello, world!"
28+
document = language.document text
29+
30+
# Detects the sentiment of the text
31+
sentiment = document.sentiment
32+
33+
puts "Text: #{text}"
34+
puts "Sentiment: #{sentiment.polarity}, #{sentiment.magnitude}"
35+
# [END language_quickstart]
36+

language/spec/quickstart_spec.rb

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Copyright 2016 Google, Inc
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
require "rspec"
16+
require "google/cloud"
17+
18+
describe "Language Quickstart" do
19+
20+
it "detect sentiment" do
21+
gcloud = Google::Cloud.new ENV["GOOGLE_CLOUD_PROJECT"]
22+
expect(Google::Cloud).to receive(:new).with("YOUR_PROJECT_ID").
23+
and_return(gcloud)
24+
25+
expect {
26+
load File.expand_path("../quickstart.rb", __dir__)
27+
}.to output(
28+
/Text: Hello, world!\nSentiment: 1.0/
29+
).to_stdout
30+
end
31+
32+
end
33+

logging/quickstart.rb

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Copyright 2016 Google, Inc
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# [START logging_quickstart]
16+
# Imports the Google Cloud client library
17+
require "google/cloud"
18+
19+
# Your Google Cloud Platform project ID
20+
project_id = "YOUR_PROJECT_ID"
21+
22+
# Instantiates a client
23+
gcloud = Google::Cloud.new project_id
24+
logging = gcloud.logging
25+
26+
# Prepares a log entry
27+
entry = logging.entry
28+
# The data to log
29+
entry.payload = "Hello, world!"
30+
# The name of the log to write to
31+
entry.log_name = "my-log"
32+
# The resource associated with the data
33+
entry.resource.type = "global"
34+
35+
# Writes the log entry
36+
logging.write_entries entry
37+
38+
puts "Logged #{entry.payload}"
39+
# [END logging_quickstart]
40+

logging/spec/quickstart_spec.rb

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# Copyright 2016 Google, Inc
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
require "rspec"
16+
require "google/cloud"
17+
18+
describe "Logging Quickstart" do
19+
20+
# Simple wait method. Test for condition 5 times, delaying 1 second each time
21+
def wait_until times: 5, delay: 1, &condition
22+
times.times do
23+
return if condition.call
24+
sleep delay
25+
end
26+
raise "Condition not met. Waited #{times} times with #{delay} sec delay"
27+
end
28+
29+
before do
30+
@gcloud = Google::Cloud.new ENV["GOOGLE_CLOUD_PROJECT"]
31+
@logging = @gcloud.logging
32+
@entry = @logging.entry
33+
@log_name = "projects/#{ENV["GOOGLE_CLOUD_PROJECT"]}/logs/" +
34+
"quickstart_log_#{Time.now.to_i}"
35+
36+
@entry.log_name = @log_name
37+
end
38+
39+
after do
40+
begin
41+
@logging.delete_log @log_name
42+
rescue Google::Cloud::NotFoundError
43+
end
44+
end
45+
46+
def test_log_entries
47+
@logging.entries filter: %Q{logName="#{@log_name}"}
48+
end
49+
50+
it "logs a new entry" do
51+
expect(Google::Cloud).to receive(:new).with("YOUR_PROJECT_ID").
52+
and_return(@gcloud)
53+
expect(@gcloud).to receive(:logging).and_return(@logging)
54+
expect(@logging).to receive(:entry).and_return(@entry)
55+
allow(@entry).to receive(:log_name=).with("my-log")
56+
57+
expect(test_log_entries).to be_empty
58+
59+
expect {
60+
load File.expand_path("../quickstart.rb", __dir__)
61+
}.to output(
62+
"Logged Hello, world!\n"
63+
).to_stdout
64+
65+
entries = []
66+
67+
wait_until { entries = test_log_entries; entries.any? }
68+
69+
expect(entries).not_to be_empty
70+
expect(entries.length).to eq 1
71+
72+
entry = entries.first
73+
expect(entry.payload).to eq "Hello, world!"
74+
end
75+
end
76+

logging/spec/sample_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ def cleanup!
144144
timestamp = "\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2} [^\\\\]+"
145145

146146
expect { list_log_entries }.to output(
147-
%r{\[#{timestamp}\] #{my_application_log_name} "Log message"}
147+
%r{\[#{timestamp}\] #{my_application_log_name} \"Log message}
148148
).to_stdout
149149
end
150150

0 commit comments

Comments
 (0)