google_spreadsheet_fetcher 1.9.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8b6d94fea13b2f79415f7f890b6286d427efc2510d583c166178ec873d6f7154
4
- data.tar.gz: ed22e78b3d512387121ff938c8768319a2d3ba670f6ef3729683d314b2d5a552
3
+ metadata.gz: 58033b85b7409d8388984000a267c5b7bff059d62abf75eab033605e0d689531
4
+ data.tar.gz: f561bc69c27f1a6dde5b4a28066d5950db78ead62dc5ceab1e17c67250bcb2da
5
5
  SHA512:
6
- metadata.gz: e6f43fc91a0613c39e79e09d0cc806202bab013f92b6926978439f492c3766fff71aa97779caf6d672f132ea9ead485188234f34c9e691b7b64108898ca93600
7
- data.tar.gz: 1e64e297e726be3625750c72dd54187f9a48d64a6c257356b309ce79240d2e9c190e3a01bca4b0ba590de6083febe97720d544f7a631ae321c18bc44cbf087da
6
+ metadata.gz: 5d35206bb7db14693b68afa1073da9ba2c639989bb37297d2aa0af11f75a1014ff046289b9f776b74b9e687edbae8d208394e61ac1c657bb2d15285e8f8dd4d7
7
+ data.tar.gz: a66f04dc7b23c5598f177b54b6193dc485589ff028cb91e906795cfcbbc0ab15ef623ef1cb745c5e319ead782270f6f560e5a0cccbe8ad3877e456f2d94588bd
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 Takahiro Ooishi
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # GoogleSpreadsheetFetcher
2
2
 
3
- Use OAuth 2 authentication to retrieve data from Google Spreadsheet.
3
+ Provides access to Google spreadsheets
4
4
 
5
5
  ## Installation
6
6
 
@@ -20,18 +20,11 @@ Or install it yourself as:
20
20
 
21
21
  ## Usage
22
22
 
23
- - Make project. https://cloud.google.com/resource-manager/docs/creating-managing-projects
24
- - Enable Google Drive API
25
- - Make OAuth 2.0 Client. (other)
26
- - Download client secret json
27
-
28
-
29
23
  ```ruby
30
24
  sheet_key = 'example_sheet_id'
31
25
 
32
26
  GoogleSpreadsheetFetcher.configure do |config|
33
- config.client_secrets_file = 'client_secrets_file_path.json'
34
- config.credential_store_file = 'credential_store_file_path.json'
27
+ ...
35
28
  end
36
29
 
37
30
  user_id = 'sample'
@@ -53,6 +46,14 @@ fetcher.all_rows_by!(title: 'sheet_title')
53
46
  fetcher.all_rows_by!(sheet_id: 1234567890)
54
47
  ```
55
48
 
49
+ ### Use Service Account
50
+
51
+ https://github.com/taka0125/google_spreadsheet_fetcher/wiki/Use-Service-Account
52
+
53
+ ### Use OAuth2
54
+
55
+ https://github.com/taka0125/google_spreadsheet_fetcher/wiki/Use-OAuth2
56
+
56
57
  ## Development
57
58
 
58
59
  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.
@@ -12,6 +12,7 @@ Gem::Specification.new do |spec|
12
12
  spec.summary = %q{Google Spreadsheet fetcher}
13
13
  spec.description = %q{Google Spreadsheet fetcher}
14
14
  spec.homepage = "https://github.com/taka0125/google_spreadsheet_fetcher"
15
+ spec.licenses = ['MIT']
15
16
 
16
17
  spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
18
  f.match(%r{^(test|spec|features)/})
@@ -22,7 +23,9 @@ Gem::Specification.new do |spec|
22
23
 
23
24
  spec.required_ruby_version = '>= 2.3.0'
24
25
 
26
+ spec.add_dependency 'rack', '< 3.0'
25
27
  spec.add_dependency 'google-api-client', '~> 0.9'
28
+ spec.add_dependency 'googleauth'
26
29
  spec.add_dependency 'activesupport'
27
30
 
28
31
  spec.add_development_dependency "bundler"
@@ -0,0 +1,9 @@
1
+ module GoogleSpreadsheetFetcher
2
+ module Authorizer
3
+ module Interface
4
+ def fetch_credentials!(user_id: nil)
5
+ raise NotImplementedError
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,51 @@
1
+ # https://github.com/googleapis/google-api-ruby-client/blob/main/docs/oauth-web.md
2
+
3
+ require 'googleauth'
4
+ require 'googleauth/web_user_authorizer'
5
+ require 'googleauth/stores/file_token_store'
6
+
7
+ module GoogleSpreadsheetFetcher
8
+ module Authorizer
9
+ module Oauth2
10
+ class Authorizer
11
+ include Interface
12
+
13
+ def initialize(config: nil, callback_uri: nil)
14
+ @config = config || ::GoogleSpreadsheetFetcher::Authorizer::Oauth2::Config.new
15
+ @callback_uri = callback_uri || ::GoogleSpreadsheetFetcher::Authorizer::Oauth2::RackApplication::CALLBACK_PATH
16
+ @web_user_authorizer = build_web_user_authorizer
17
+
18
+ freeze
19
+ end
20
+
21
+ def fetch_credentials!(user_id: nil)
22
+ web_user_authorizer.get_credentials(user_id || config.user_id)
23
+ end
24
+
25
+ def fetch_credentials
26
+ fetch_credentials!
27
+ rescue StandardError
28
+ nil
29
+ end
30
+
31
+ def get_authorization_url(rack_request)
32
+ web_user_authorizer.get_authorization_url(request: rack_request)
33
+ end
34
+
35
+ def handle_auth_callback(rack_request)
36
+ web_user_authorizer.handle_auth_callback(config.user_id, rack_request)
37
+ end
38
+
39
+ private
40
+
41
+ attr_reader :config, :callback_uri, :web_user_authorizer
42
+
43
+ def build_web_user_authorizer
44
+ ::Google::Auth::WebUserAuthorizer.new(
45
+ config.client_id, ::GoogleSpreadsheetFetcher.config.scopes, config.token_store, callback_uri
46
+ )
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,28 @@
1
+ module GoogleSpreadsheetFetcher
2
+ module Authorizer
3
+ module Oauth2
4
+ class Config
5
+ attr_reader :client_secrets_json_path, :user_id, :token_store, :client_id
6
+
7
+ def initialize(client_secrets_json_path: nil, user_id: nil, token_store: nil)
8
+ @client_secrets_json_path = client_secrets_json_path || ::GoogleSpreadsheetFetcher.config.client_secrets_file
9
+ @user_id = user_id || ::GoogleSpreadsheetFetcher.config.user_id
10
+ @token_store = token_store || default_token_store
11
+ @client_id = ::Google::Auth::ClientId.from_file(self.client_secrets_json_path)
12
+
13
+ freeze
14
+ end
15
+
16
+ private
17
+
18
+ def default_token_store
19
+ store = ::GoogleSpreadsheetFetcher.config.token_store
20
+ return store if store.present?
21
+
22
+ file = ::GoogleSpreadsheetFetcher.config.credential_store_file
23
+ ::Google::Auth::Stores::FileTokenStore.new(file: file)
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,70 @@
1
+ require 'securerandom'
2
+
3
+ module GoogleSpreadsheetFetcher
4
+ module Authorizer
5
+ module Oauth2
6
+ class RackApplication
7
+ AUTHORIZE_PATH = '/authorize'.freeze
8
+ CALLBACK_PATH = '/oauth2callback'.freeze
9
+
10
+ def initialize(config: nil)
11
+ @config = config || ::GoogleSpreadsheetFetcher::Authorizer::Oauth2::Config.new
12
+ @authorizer = ::GoogleSpreadsheetFetcher::Authorizer::Oauth2::Authorizer.new(config: config)
13
+
14
+ freeze
15
+ end
16
+
17
+ def call(env)
18
+ path = env['PATH_INFO']
19
+ request_method = env['REQUEST_METHOD']
20
+
21
+ return handle_authorize(env) if path == AUTHORIZE_PATH && request_method == 'GET'
22
+ return handle_callback(env) if path == CALLBACK_PATH && request_method == 'GET'
23
+
24
+ plain_response(400, 'invalid access')
25
+ end
26
+
27
+ def cookie_settings(secret: nil)
28
+ {
29
+ domain: 'localhost',
30
+ path: '/',
31
+ expire_after: 3600*24,
32
+ secret: secret || SecureRandom.uuid
33
+ }
34
+ end
35
+
36
+ private
37
+
38
+ attr_reader :config, :authorizer
39
+
40
+ def handle_authorize(env)
41
+ request = ::Rack::Request.new(env)
42
+
43
+ credentials = authorizer.fetch_credentials
44
+ if credentials.nil?
45
+ redirect_url = authorizer.get_authorization_url(request)
46
+
47
+ return redirect_response(redirect_url)
48
+ end
49
+
50
+ plain_response(200, 'authorized')
51
+ end
52
+
53
+ def handle_callback(env)
54
+ request = ::Rack::Request.new(env)
55
+ authorizer.handle_auth_callback(request)
56
+
57
+ plain_response(200, 'token stored')
58
+ end
59
+
60
+ def redirect_response(url)
61
+ [302, {'location' => url}, []]
62
+ end
63
+
64
+ def plain_response(status, text)
65
+ [status, {'content-type' => 'text/plain'}, [text]]
66
+ end
67
+ end
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,27 @@
1
+ module GoogleSpreadsheetFetcher
2
+ module Authorizer
3
+ class ServiceAccount
4
+ include Interface
5
+
6
+ def initialize(credential, scope: nil)
7
+ @credential = credential
8
+ @scope = scope || ::GoogleSpreadsheetFetcher.config.scopes
9
+
10
+ freeze
11
+ end
12
+
13
+ def setup!; end
14
+
15
+ def fetch_credentials!(user_id: nil)
16
+ ::Google::Auth::ServiceAccountCredentials.make_creds(
17
+ json_key_io: StringIO.new(credential),
18
+ scope: scope
19
+ ).tap(&:fetch_access_token!)
20
+ end
21
+
22
+ private
23
+
24
+ attr_reader :credential, :scope
25
+ end
26
+ end
27
+ end
@@ -1,6 +1,3 @@
1
- require 'google/apis/sheets_v4'
2
- require 'shellwords'
3
-
4
1
  module GoogleSpreadsheetFetcher
5
2
  class BulkFetcher
6
3
  # @param [String] spreadsheet_id
@@ -4,14 +4,17 @@ module GoogleSpreadsheetFetcher
4
4
  class Config
5
5
  include ActiveSupport::Configurable
6
6
 
7
+ config_accessor :authorizer
8
+
7
9
  config_accessor :client_secrets_file
8
10
  config_accessor :credential_store_file # required if token_store not set
9
11
  config_accessor :token_store # required if credential_store_file not set
10
12
  config_accessor :scopes
13
+ config_accessor :user_id
11
14
 
12
15
  def self.default_config
13
16
  new.tap do |config|
14
- config.scopes = [Google::Apis::SheetsV4::AUTH_SPREADSHEETS_READONLY]
17
+ config.scopes = [::Google::Apis::SheetsV4::AUTH_SPREADSHEETS_READONLY]
15
18
  end
16
19
  end
17
20
  end
@@ -1,10 +1,5 @@
1
- require 'google/apis/sheets_v4'
2
- require 'shellwords'
3
-
4
1
  module GoogleSpreadsheetFetcher
5
2
  class Fetcher
6
- OOB_URI = 'urn:ietf:wg:oauth:2.0:oob'
7
-
8
3
  # @param [String] spreadsheet_id
9
4
  # @param [String] user_id
10
5
  # @param [GoogleSpreadsheetFetcher::Config] config
@@ -1,12 +1,5 @@
1
- require 'google/apis/sheets_v4'
2
- require 'googleauth'
3
- require 'googleauth/stores/file_token_store'
4
- require 'shellwords'
5
-
6
1
  module GoogleSpreadsheetFetcher
7
2
  class SheetsServiceBuilder
8
- OOB_URI = 'urn:ietf:wg:oauth:2.0:oob'
9
-
10
3
  # @param [String] user_id
11
4
  # @param [GoogleSpreadsheetFetcher::Config] config
12
5
  # @param [String] application_name
@@ -14,31 +7,20 @@ module GoogleSpreadsheetFetcher
14
7
  @user_id = user_id
15
8
  @config = config || GoogleSpreadsheetFetcher.config
16
9
  @application_name = application_name
17
- @token_store = @config.token_store || Google::Auth::Stores::FileTokenStore.new(file: @config.credential_store_file)
18
10
  end
19
11
 
20
- def build
21
- Google::Apis::SheetsV4::SheetsService.new.tap do |service|
22
- service.authorization = fetch_credentials
23
- service.client_options.application_name = @application_name if @application_name.present?
12
+ def build(authorizer: nil)
13
+ authorizer = authorizer || config.authorizer || ::GoogleSpreadsheetFetcher::Authorizer::Oauth2::Authorizer.new
14
+ raise 'Authorizer is not configured' if authorizer.blank?
15
+
16
+ ::Google::Apis::SheetsV4::SheetsService.new.tap do |service|
17
+ service.authorization = authorizer.fetch_credentials!(user_id: user_id)
18
+ service.client_options.application_name = application_name if application_name.present?
24
19
  end
25
20
  end
26
21
 
27
22
  private
28
23
 
29
- def fetch_credentials
30
- client_id = Google::Auth::ClientId.from_file(@config.client_secrets_file)
31
- authorizer = Google::Auth::UserAuthorizer.new(client_id, @config.scopes, @token_store)
32
-
33
- credentials = authorizer.get_credentials(@user_id)
34
- return credentials if credentials.present?
35
-
36
- url = authorizer.get_authorization_url(base_url: OOB_URI)
37
- escaped_url = url.shellescape
38
- system("open #{escaped_url}")
39
- puts "Open #{url} in your browser and enter the resulting code: "
40
- code = STDIN.gets
41
- authorizer.get_and_store_credentials_from_code(user_id: @user_id, code: code, base_url: OOB_URI)
42
- end
24
+ attr_reader :user_id, :config, :application_name
43
25
  end
44
26
  end
@@ -1,3 +1,3 @@
1
1
  module GoogleSpreadsheetFetcher
2
- VERSION = "1.9.0"
2
+ VERSION = "2.0.0"
3
3
  end
@@ -1,13 +1,21 @@
1
- require "active_support"
2
- require "active_support/json"
3
- require "active_support/core_ext"
4
- require "google_spreadsheet_fetcher/version"
5
- require "google_spreadsheet_fetcher/config"
6
- require "google_spreadsheet_fetcher/error"
7
- require "google_spreadsheet_fetcher/fetcher"
8
- require "google_spreadsheet_fetcher/sheet_url"
9
- require "google_spreadsheet_fetcher/bulk_fetcher"
10
- require "google_spreadsheet_fetcher/sheets_service_builder"
1
+ require 'active_support'
2
+ require 'active_support/json'
3
+ require 'active_support/core_ext'
4
+ require 'googleauth'
5
+ require 'google/apis/sheets_v4'
6
+
7
+ require 'google_spreadsheet_fetcher/version'
8
+ require 'google_spreadsheet_fetcher/config'
9
+ require 'google_spreadsheet_fetcher/error'
10
+ require 'google_spreadsheet_fetcher/authorizer/interface'
11
+ require 'google_spreadsheet_fetcher/authorizer/service_account'
12
+ require 'google_spreadsheet_fetcher/authorizer/oauth2/config'
13
+ require 'google_spreadsheet_fetcher/authorizer/oauth2/rack_application'
14
+ require 'google_spreadsheet_fetcher/authorizer/oauth2/authorizer'
15
+ require 'google_spreadsheet_fetcher/fetcher'
16
+ require 'google_spreadsheet_fetcher/sheet_url'
17
+ require 'google_spreadsheet_fetcher/bulk_fetcher'
18
+ require 'google_spreadsheet_fetcher/sheets_service_builder'
11
19
 
12
20
  module GoogleSpreadsheetFetcher
13
21
  def self.config
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: google_spreadsheet_fetcher
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.9.0
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Takahiro Ooishi
@@ -9,8 +9,22 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2022-08-11 00:00:00.000000000 Z
12
+ date: 2022-12-03 00:00:00.000000000 Z
13
13
  dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rack
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "<"
19
+ - !ruby/object:Gem::Version
20
+ version: '3.0'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "<"
26
+ - !ruby/object:Gem::Version
27
+ version: '3.0'
14
28
  - !ruby/object:Gem::Dependency
15
29
  name: google-api-client
16
30
  requirement: !ruby/object:Gem::Requirement
@@ -25,6 +39,20 @@ dependencies:
25
39
  - - "~>"
26
40
  - !ruby/object:Gem::Version
27
41
  version: '0.9'
42
+ - !ruby/object:Gem::Dependency
43
+ name: googleauth
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ type: :runtime
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
28
56
  - !ruby/object:Gem::Dependency
29
57
  name: activesupport
30
58
  requirement: !ruby/object:Gem::Requirement
@@ -95,12 +123,18 @@ files:
95
123
  - ".rspec"
96
124
  - ".travis.yml"
97
125
  - Gemfile
126
+ - LICENSE
98
127
  - README.md
99
128
  - Rakefile
100
129
  - bin/console
101
130
  - bin/setup
102
131
  - google_spreadsheet_fetcher.gemspec
103
132
  - lib/google_spreadsheet_fetcher.rb
133
+ - lib/google_spreadsheet_fetcher/authorizer/interface.rb
134
+ - lib/google_spreadsheet_fetcher/authorizer/oauth2/authorizer.rb
135
+ - lib/google_spreadsheet_fetcher/authorizer/oauth2/config.rb
136
+ - lib/google_spreadsheet_fetcher/authorizer/oauth2/rack_application.rb
137
+ - lib/google_spreadsheet_fetcher/authorizer/service_account.rb
104
138
  - lib/google_spreadsheet_fetcher/bulk_fetcher.rb
105
139
  - lib/google_spreadsheet_fetcher/config.rb
106
140
  - lib/google_spreadsheet_fetcher/error.rb
@@ -109,7 +143,8 @@ files:
109
143
  - lib/google_spreadsheet_fetcher/sheets_service_builder.rb
110
144
  - lib/google_spreadsheet_fetcher/version.rb
111
145
  homepage: https://github.com/taka0125/google_spreadsheet_fetcher
112
- licenses: []
146
+ licenses:
147
+ - MIT
113
148
  metadata: {}
114
149
  post_install_message:
115
150
  rdoc_options: []