From fb82cbc54d4a5f932031c7665c11c2d9d11e8f41 Mon Sep 17 00:00:00 2001 From: tknzk Date: Thu, 26 May 2016 16:59:41 +0900 Subject: [PATCH 1/9] cli sample for sheets api --- samples/cli/lib/samples/sheets.rb | 63 +++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 samples/cli/lib/samples/sheets.rb diff --git a/samples/cli/lib/samples/sheets.rb b/samples/cli/lib/samples/sheets.rb new file mode 100644 index 000000000..2b015e9a1 --- /dev/null +++ b/samples/cli/lib/samples/sheets.rb @@ -0,0 +1,63 @@ +# 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 'google/apis/sheets_v4' +require 'base_cli' + + +module Samples + # Examples for the Google Sheets API + # + # Sample usage: + # + # $ ./google-api-samples sheets get_values --spreadsheet_key='1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms' --range='Class Data!A1:F' + # + # Student Name Gender Class Level Home State Major Extracurricular Activity + # Alexandra Female 4. Senior CA English Drama Club + # Andrew Male 1. Freshman SD Math Lacrosse + # Anna Female 1. Freshman NC English Basketball + # Becky Female 2. Sophomore SD Art Baseball + # Benjamin Male 4. Senior WI English Basketball + # Carl Male 3. Junior MD Art Debate + # Carrie Female 3. Junior NE English Track & Field + # Dorothy Female 4. Senior MD Math Lacrosse + # Dylan Male 1. Freshman MA Math Baseball + # Edward Male 3. Junior FL English Drama Club + # Ellen Female 1. Freshman WI Physics Drama Club + # Fiona Female 1. Freshman MA Art Debate + # John Male 3. Junior CA Physics Basketball + # Jonathan Male 2. Sophomore SC Math Debate + class Sheets < BaseCli + Sheets = Google::Apis::SheetsV4 + + desc 'get_values', 'Get values from Google Spreadseets ' + method_option :spreadsheet_key, type: :string, required: true + method_option :range, type: :string, required: true + def get_values + sheets = Sheets::SheetsService.new + sheets.authorization = user_credentials_for(Sheets::AUTH_SPREADSHEETS_READONLY) + result = sheets.get_spreadsheet_values(options[:spreadsheet_key], options[:range]) + puts result.inspect + data = [] + rows = result.values.length + + rows.times.each do |k| + data.push(result.values[k]) + end + print_table(data) + end + + default_task :get_vlaues + end +end From 53567aa788011e59260d382105b132daf1970846 Mon Sep 17 00:00:00 2001 From: tknzk Date: Thu, 26 May 2016 17:04:11 +0900 Subject: [PATCH 2/9] fix each val --- samples/cli/lib/samples/sheets.rb | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/samples/cli/lib/samples/sheets.rb b/samples/cli/lib/samples/sheets.rb index 2b015e9a1..1e69fd685 100644 --- a/samples/cli/lib/samples/sheets.rb +++ b/samples/cli/lib/samples/sheets.rb @@ -51,9 +51,8 @@ module Samples puts result.inspect data = [] rows = result.values.length - - rows.times.each do |k| - data.push(result.values[k]) + rows.times.each do |row| + data.push(result.values[row]) end print_table(data) end From 3a0d4b95303a568f03c40effe87acb4f6cb795bb Mon Sep 17 00:00:00 2001 From: tknzk Date: Fri, 27 May 2016 09:27:28 +0900 Subject: [PATCH 3/9] fix a typo. --- samples/cli/lib/samples/sheets.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/cli/lib/samples/sheets.rb b/samples/cli/lib/samples/sheets.rb index 1e69fd685..c72b6f096 100644 --- a/samples/cli/lib/samples/sheets.rb +++ b/samples/cli/lib/samples/sheets.rb @@ -57,6 +57,6 @@ module Samples print_table(data) end - default_task :get_vlaues + default_task :get_values end end From ccd694a916a6ae0cc428d7ff954c88beace0d76f Mon Sep 17 00:00:00 2001 From: Yosuke Kabuto Date: Sun, 29 May 2016 13:13:42 +0900 Subject: [PATCH 4/9] Google::APIClient::FileStore.load_credentials loads all attrs of the json --- spec/google/api_client/auth/storages/file_store_spec.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/spec/google/api_client/auth/storages/file_store_spec.rb b/spec/google/api_client/auth/storages/file_store_spec.rb index 83199774b..e1f72916f 100644 --- a/spec/google/api_client/auth/storages/file_store_spec.rb +++ b/spec/google/api_client/auth/storages/file_store_spec.rb @@ -28,7 +28,9 @@ describe Google::APIClient::FileStore do it 'should load credentials' do subject.path = json_file credentials = subject.load_credentials - expect(credentials).to include('access_token', 'authorization_uri', 'refresh_token') + expect(credentials).to include( + 'access_token', 'authorization_uri', 'refresh_token', 'client_id', + 'client_secret', 'expires_in', 'token_credential_uri', 'issued_at') end it 'should write credentials' do From 07d8f226127b30e6a0b05d13815ba5d4d9cdccf0 Mon Sep 17 00:00:00 2001 From: Yosuke Kabuto Date: Sun, 29 May 2016 19:51:36 +0900 Subject: [PATCH 5/9] Remove white spaces --- lib/google/apis/errors.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/google/apis/errors.rb b/lib/google/apis/errors.rb index 96c5a4056..29fed01de 100644 --- a/lib/google/apis/errors.rb +++ b/lib/google/apis/errors.rb @@ -19,7 +19,7 @@ module Google attr_reader :status_code attr_reader :header attr_reader :body - + def initialize(err, status_code: nil, header: nil, body: nil) @cause = nil @@ -42,7 +42,7 @@ module Google end end end - + # An error which is raised when there is an unexpected response or other # transport error that prevents an operation from succeeding. class TransmissionError < Error From 657bfc0e01f87574fefbb347773811562e431dcd Mon Sep 17 00:00:00 2001 From: Yosuke Kabuto Date: Sun, 29 May 2016 19:52:27 +0900 Subject: [PATCH 6/9] Create specs for Google::Apis::Error --- spec/google/apis/errors_spec.rb | 96 +++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 spec/google/apis/errors_spec.rb diff --git a/spec/google/apis/errors_spec.rb b/spec/google/apis/errors_spec.rb new file mode 100644 index 000000000..4585679e8 --- /dev/null +++ b/spec/google/apis/errors_spec.rb @@ -0,0 +1,96 @@ +# 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 'spec_helper' + +RSpec.describe Google::Apis::Error do + describe 'constructor' do + subject { described_class.new(error) } + + describe 'message' do + context 'first arg is a string which does not respond to :backtrace' do + let(:error) { 'an error occurred' } + + it 'creates a instance of Error with message' do + expect(subject.message).to eq 'an error occurred' + end + end + + context 'first arg responds to :backtrace' do + let(:error) { double } + + it 'creates a instance of Error with message' do + allow(error).to receive(:respond_to?).with(:backtrace) { true } + allow(error).to receive(:message) { 'error message' } + expect(subject.message).to eq 'error message' + end + end + end + + describe 'instance variables @body, @header, and @status_code' do + let(:error) { 'an error occurred' } + + context 'params :body, :header, and :status_code are not given' do + it 'instance variables @body, @header, and @status_code will be nil' do + expect(subject.body).to be_nil + expect(subject.header).to be_nil + expect(subject.status_code).to be_nil + end + end + + context 'params :body, :header, and :status_code are given' do + subject do + described_class.new( + error, + body: 'this is body', + header: 'this is header', + status_code: 200) + end + + it 'instance variables @body, @header, and @status_code will not be nil' do + expect(subject.body).to eq 'this is body' + expect(subject.header).to eq 'this is header' + expect(subject.status_code).to eq 200 + end + end + end + end + + describe '#backtrace' do + subject { described_class.new('error') } + let(:cause) { instance_double('Error', backtrace: 'this is backtrace') } + + context '@cause is truthy' do + it 'calls @cause.backtrace' do + subject.instance_variable_set(:@cause, cause) + expect(subject.backtrace).to eq 'this is backtrace' + end + end + + context '@cause is falsy' do + before do + subject.class.superclass.class_eval do + def backtrace + "super class's #backtrace called" + end + end + end + + it "calls super class's #backtrace" do + subject.instance_variable_set(:@cause, false) + expect(subject.backtrace).to eq "super class's #backtrace called" + end + end + end +end From ab8cfe52081e8a02366746d7f27cc377d29c9cfb Mon Sep 17 00:00:00 2001 From: Yosuke Kabuto Date: Sun, 29 May 2016 20:20:59 +0900 Subject: [PATCH 7/9] Stub instance method :backtrace, insted of defining it --- spec/google/apis/errors_spec.rb | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/spec/google/apis/errors_spec.rb b/spec/google/apis/errors_spec.rb index 4585679e8..ad2a576e3 100644 --- a/spec/google/apis/errors_spec.rb +++ b/spec/google/apis/errors_spec.rb @@ -80,10 +80,8 @@ RSpec.describe Google::Apis::Error do context '@cause is falsy' do before do - subject.class.superclass.class_eval do - def backtrace - "super class's #backtrace called" - end + subject.class.superclass.any_instance.stub(:backtrace) do + "super class's #backtrace called" end end From f1f01125122216bb6685364a6bccd56ae23a2a77 Mon Sep 17 00:00:00 2001 From: tknzk Date: Wed, 1 Jun 2016 14:19:44 +0900 Subject: [PATCH 8/9] remove debug line. --- samples/cli/lib/samples/sheets.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/samples/cli/lib/samples/sheets.rb b/samples/cli/lib/samples/sheets.rb index c72b6f096..e7a9dc48e 100644 --- a/samples/cli/lib/samples/sheets.rb +++ b/samples/cli/lib/samples/sheets.rb @@ -48,7 +48,6 @@ module Samples sheets = Sheets::SheetsService.new sheets.authorization = user_credentials_for(Sheets::AUTH_SPREADSHEETS_READONLY) result = sheets.get_spreadsheet_values(options[:spreadsheet_key], options[:range]) - puts result.inspect data = [] rows = result.values.length rows.times.each do |row| From 36bfc5506f7773888d01f2aad7153d48075fa14c Mon Sep 17 00:00:00 2001 From: tknzk Date: Wed, 1 Jun 2016 16:24:54 +0900 Subject: [PATCH 9/9] cli sample for bigquery --- samples/cli/lib/samples/bigquery.rb | 101 ++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 samples/cli/lib/samples/bigquery.rb diff --git a/samples/cli/lib/samples/bigquery.rb b/samples/cli/lib/samples/bigquery.rb new file mode 100644 index 000000000..863f4ab9c --- /dev/null +++ b/samples/cli/lib/samples/bigquery.rb @@ -0,0 +1,101 @@ +# 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 'google/apis/bigquery_v2' +require 'base_cli' + + +module Samples + # Examples for the Google BigQuery API + # + # Sample usage: + # + # $ ./google-api-samples bigquery list_datasets --project_id=publicdata + # $ ./google-api-samples bigquery list_tables --project_id=publicdata --dataset_id=samples + # $ ./google-api-samples bigquery list_table_data --project_id=publicdata --dataset_id=samples --table_id=wikipedia --max_results=10 + class Bigquery < BaseCli + Bigquery = Google::Apis::BigqueryV2 + + desc 'list datasets', 'List datasets' + method_option :project_id, type: :string + def list_datasets + bigquery = Bigquery::BigqueryService.new + bigquery.authorization = user_credentials_for(Bigquery::AUTH_BIGQUERY) + + begin + project_id = options[:project_id] || 'publicdata' + result = bigquery.list_datasets(project_id) + data = [] + data << ['id','kind'] + result.datasets.each do |dataset| + data << [ + dataset.id, + dataset.kind, + ] + end + print_table(data) + end + end + + desc 'list tables', 'List tables' + method_option :project_id, type: :string + method_option :dataset_id, type: :string + def list_tables + bigquery = Bigquery::BigqueryService.new + bigquery.authorization = user_credentials_for(Bigquery::AUTH_BIGQUERY) + + begin + project_id = options[:project_id] || 'publicdata' + dataset_id = options[:dataset_id] || 'samples' + result = bigquery.list_tables(project_id, dataset_id) + data = [] + data << ['id','kind'] + result.tables.each do |table| + data << [ + table.id, + table.kind, + ] + end + print_table(data) + end + end + + desc 'list table data', 'List table data' + method_option :project_id, type: :string + method_option :dataset_id, type: :string + method_option :table_id, type: :string + def list_table_data + bigquery = Bigquery::BigqueryService.new + bigquery.authorization = user_credentials_for(Bigquery::AUTH_BIGQUERY) + + begin + project_id = options[:project_id] || 'publicdata' + dataset_id = options[:dataset_id] || 'samples' + table_id = options[:table_id] || 'wikipedia' + max_results = options[:max_results] || 10 + result = bigquery.list_table_data(project_id, dataset_id, table_id, max_results: max_results) + + datas = [] + result.rows.each do |row| + data = [] + row.f.each do |field| + data << field.v + end + datas << data + end + print_table(datas) + end + end + end +end