google_spreadsheet_bulk_fetcher 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: c750dce50e2eefaf085870396c8f78688d8873cdb84d09ec488cab63fa2949bd
4
+ data.tar.gz: fc5ef752e2abad5202b56f7032b6d8b15e1b5fa72add76878e618d8d54ecb813
5
+ SHA512:
6
+ metadata.gz: eb1f9ee0d9f106e4751be54dfc40f064f91c8c26a1579fe697d969faf6df5631c8c93fe5147dff792d54e320a898d05ce1188a2c154b06c02e2120728bbb7277
7
+ data.tar.gz: 78b087d242de68336c51391844d645f34b371f2648a09ea3fdb6eb2817b92c4b491c140df9db0a44efbb817e92f49dd62065040e0d188c17303cb5e9fc6fbe86
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
13
+
14
+ vendor/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.1
5
+ before_install: gem install bundler -v 1.14.6
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in google_spreadsheet_bulk_fetcher.gemspec
4
+ gemspec
@@ -0,0 +1,56 @@
1
+ # GoogleSpreadsheetBulkFetcher
2
+
3
+ Use OAuth 2 authentication to retrieve the values of all sheets with a single API access.
4
+
5
+ Fork from taka0125/google_spreadsheet_fetcher
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'google_spreadsheet_bulk_fetcher'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install google_spreadsheet_bulk_fetcher
22
+
23
+ ## Usage
24
+
25
+ - Make project. https://cloud.google.com/resource-manager/docs/creating-managing-projects
26
+ - Enable Google Drive API
27
+ - Make OAuth 2.0 Client. (other)
28
+ - Download client secret json
29
+
30
+ ```ruby
31
+ sheet_key = 'example_sheet_id'
32
+
33
+ GoogleSpreadsheetBulkFetcher.configure do |config|
34
+ config.client_secrets_file = 'client_secrets_file_path.json'
35
+ config.credential_store_file = 'credential_store_file_path.json'
36
+ end
37
+
38
+ user_id = 'sample'
39
+ fetcher = GoogleSpreadsheetBulkFetcher::Fetcher.new(sheet_key, user_id)
40
+ fetcher.fetch
41
+
42
+ fetcher.all_rows_by!(index: 0)
43
+ fetcher.all_rows_by!(title: 'sheet_title')
44
+ fetcher.all_rows_by!(sheet_id: 1234567890)
45
+ ```
46
+
47
+ ## Development
48
+
49
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
50
+
51
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
52
+
53
+ ## Contributing
54
+
55
+ Bug reports and pull requests are welcome on GitHub at https://github.com/YuyaYokosuka/google_spreadsheet_bulk_fetcher.
56
+
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "google_spreadsheet_bulk_fetcher"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'google_spreadsheet_bulk_fetcher/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "google_spreadsheet_bulk_fetcher"
8
+ spec.version = GoogleSpreadsheetBulkFetcher::VERSION
9
+ spec.authors = ["Yuya Yokosuka"]
10
+ spec.email = ["yuya.yokosuka@gmail.com"]
11
+
12
+ spec.summary = %q{Google Spreadsheet bulk fetcher}
13
+ spec.description = %q{Use OAuth 2 authentication to retrieve the values of all sheets with a single API access.}
14
+ spec.homepage = "https://github.com/YuyaYokosuka/google_spreadsheet_bulk_fetcher"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+ spec.bindir = "exe"
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.required_ruby_version = '>= 2.3.0'
24
+
25
+ spec.add_dependency 'google-api-client', '~> 0.9'
26
+ spec.add_dependency 'activesupport'
27
+
28
+ spec.add_development_dependency "bundler"
29
+ spec.add_development_dependency "rake"
30
+ spec.add_development_dependency "rspec"
31
+ end
@@ -0,0 +1,16 @@
1
+ require "active_support/json"
2
+ require "active_support/core_ext"
3
+ require "google_spreadsheet_bulk_fetcher/version"
4
+ require "google_spreadsheet_bulk_fetcher/config"
5
+ require "google_spreadsheet_bulk_fetcher/error"
6
+ require "google_spreadsheet_bulk_fetcher/fetcher"
7
+
8
+ module GoogleSpreadsheetBulkFetcher
9
+ def self.config
10
+ @config ||= Config.default_config
11
+ end
12
+
13
+ def self.configure
14
+ yield config
15
+ end
16
+ end
@@ -0,0 +1,17 @@
1
+ require 'active_support/configurable'
2
+
3
+ module GoogleSpreadsheetBulkFetcher
4
+ class Config
5
+ include ActiveSupport::Configurable
6
+
7
+ config_accessor :client_secrets_file
8
+ config_accessor :credential_store_file
9
+ config_accessor :scopes
10
+
11
+ def self.default_config
12
+ new.tap do |config|
13
+ config.scopes = [Google::Apis::SheetsV4::AUTH_SPREADSHEETS_READONLY]
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,4 @@
1
+ module GoogleSpreadsheetBulkFetcher
2
+ class SpreadsheetNotFound < StandardError; end
3
+ class SheetNotFound < StandardError; end
4
+ end
@@ -0,0 +1,105 @@
1
+ require 'google/apis/sheets_v4'
2
+ require 'googleauth'
3
+ require 'googleauth/stores/file_token_store'
4
+ require 'shellwords'
5
+
6
+ module GoogleSpreadsheetBulkFetcher
7
+ class Fetcher
8
+ OOB_URI = 'urn:ietf:wg:oauth:2.0:oob'
9
+
10
+ # @param [String] spreadsheet_id
11
+ # @param [String] user_id
12
+ # @param [GoogleSpreadsheetBulkFetcher::Config] config
13
+ # @param [String] application_name
14
+ def initialize(spreadsheet_id, user_id, config: nil, application_name: nil)
15
+ @spreadsheet_id = spreadsheet_id
16
+ @user_id = user_id
17
+ @config = config || GoogleSpreadsheetBulkFetcher.config
18
+ @application_name = application_name
19
+
20
+ @spreadsheet = nil
21
+ end
22
+
23
+ def fetch
24
+ @spreadsheet = service.get_spreadsheet(@spreadsheet_id, fields: 'sheets(properties,data.rowData.values(formattedValue))')
25
+ self
26
+ end
27
+
28
+ # @param [Integer] index
29
+ # @param [Integer] sheet_id
30
+ # @param [String] title
31
+ # @param [Integer] skip
32
+ # @param [Boolean] structured
33
+ def all_rows_by!(index: nil, sheet_id: nil, title: nil, skip: 0, structured: false)
34
+ sheet = sheet_by!(index: index, sheet_id: sheet_id, title: title)
35
+ sheet_to_array(sheet, skip: skip, structured: structured)
36
+ end
37
+
38
+ def service
39
+ @service ||= Google::Apis::SheetsV4::SheetsService.new.tap do |service|
40
+ service.authorization = fetch_credentials
41
+ service.client_options.application_name = @application_name if @application_name.present?
42
+ end
43
+ end
44
+
45
+ private
46
+
47
+ def sheet_by!(index: nil, sheet_id: nil, title: nil)
48
+ raise SpreadsheetNotFound if @spreadsheet.sheets.blank?
49
+
50
+ if index.present?
51
+ return @spreadsheet.sheets.find { |sheet| sheet.properties.index == index }
52
+ elsif sheet_id.present?
53
+ return @spreadsheet.sheets.find { |sheet| sheet.properties.sheet_id == sheet_id }
54
+ elsif title.present?
55
+ return @spreadsheet.sheets.find { |sheet| sheet.properties.title == title }
56
+ end
57
+
58
+ raise SheetNotFound
59
+ end
60
+
61
+ def sheet_to_array(sheet, skip: 0, structured: false)
62
+ sheet_data = sheet&.data&.first
63
+ return [] if sheet_data.nil?
64
+
65
+ rows = sheet_data.row_data.map do |row_data|
66
+ values = row_data.values
67
+ next [''] if values.nil?
68
+
69
+ values.map { |cell| cell&.formatted_value || "" }
70
+ end
71
+
72
+ headers = rows.first
73
+ count = headers.count
74
+
75
+ if structured
76
+ rows.delete_at(0)
77
+ rows.slice!(0, skip)
78
+ rows.map { |r| headers.zip(r).to_h }
79
+ else
80
+ rows.slice!(0, skip)
81
+ rows.map { |r| fill_array(r, count) }
82
+ end
83
+ end
84
+
85
+ def fetch_credentials
86
+ client_id = Google::Auth::ClientId.from_file(@config.client_secrets_file)
87
+ token_store = Google::Auth::Stores::FileTokenStore.new(file: @config.credential_store_file)
88
+ authorizer = Google::Auth::UserAuthorizer.new(client_id, @config.scopes, token_store)
89
+
90
+ credentials = authorizer.get_credentials(@user_id)
91
+ return credentials if credentials.present?
92
+
93
+ url = authorizer.get_authorization_url(base_url: OOB_URI)
94
+ escaped_url = url.shellescape
95
+ system("open #{escaped_url}")
96
+ puts "Open #{url} in your browser and enter the resulting code: "
97
+ code = STDIN.gets
98
+ authorizer.get_and_store_credentials_from_code(user_id: @user_id, code: code, base_url: OOB_URI)
99
+ end
100
+
101
+ def fill_array(items, count, fill: '')
102
+ items + (count - items.count).times.map { fill }
103
+ end
104
+ end
105
+ end
@@ -0,0 +1,3 @@
1
+ module GoogleSpreadsheetBulkFetcher
2
+ VERSION = "1.0.0"
3
+ end
metadata ADDED
@@ -0,0 +1,127 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: google_spreadsheet_bulk_fetcher
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Yuya Yokosuka
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-07-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: google-api-client
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.9'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.9'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Use OAuth 2 authentication to retrieve the values of all sheets with
84
+ a single API access.
85
+ email:
86
+ - yuya.yokosuka@gmail.com
87
+ executables: []
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - ".gitignore"
92
+ - ".rspec"
93
+ - ".travis.yml"
94
+ - Gemfile
95
+ - README.md
96
+ - Rakefile
97
+ - bin/console
98
+ - bin/setup
99
+ - google_spreadsheet_bulk_fetcher.gemspec
100
+ - lib/google_spreadsheet_bulk_fetcher.rb
101
+ - lib/google_spreadsheet_bulk_fetcher/config.rb
102
+ - lib/google_spreadsheet_bulk_fetcher/error.rb
103
+ - lib/google_spreadsheet_bulk_fetcher/fetcher.rb
104
+ - lib/google_spreadsheet_bulk_fetcher/version.rb
105
+ homepage: https://github.com/YuyaYokosuka/google_spreadsheet_bulk_fetcher
106
+ licenses: []
107
+ metadata: {}
108
+ post_install_message:
109
+ rdoc_options: []
110
+ require_paths:
111
+ - lib
112
+ required_ruby_version: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: 2.3.0
117
+ required_rubygems_version: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - ">="
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ requirements: []
123
+ rubygems_version: 3.0.3
124
+ signing_key:
125
+ specification_version: 4
126
+ summary: Google Spreadsheet bulk fetcher
127
+ test_files: []