google_spreadsheet_fetcher 1.5.0 → 1.8.1

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: 19986f7cb8f74972b30f9ea8b64e824a10eb27d2674ae505687e975e1a6d39a6
4
- data.tar.gz: 743b07edcaac696de6d0bae0dba814b7b4d63d90e35533f04b08bfcffce7644e
3
+ metadata.gz: 01317b3b4070bd85ad5c1348628e12d328ff48d14c043dd5d24ac6cfc2e9d6bc
4
+ data.tar.gz: 28bfbd3c4cce51f95c99a741f398d73e8069834e69a09bd7cb11cfbb873fb86b
5
5
  SHA512:
6
- metadata.gz: 7d18fbdd7aecd3556d9a691548f7ded59966a9ceee8fc5f3539168f8ce1387d46a1fa5d6bc80ef984e3b2cd34963ad5f27f48cdaaa9b4fea66bac4bd02afe5f4
7
- data.tar.gz: 7fed94f3c9c7a0f9c66d733600361d150bd564d201143e63e75ce92ad871cf20c54a2850057ad1866e7ef049355ec920cc98d2a492abea2ff18be145369dd922
6
+ metadata.gz: 7b3f1cab21db6c6f37894307559f16ba4c85a2a8a6b581845ce3cf3641c85b24be03efe54c67d9b12d06eecac259a6b7974e62aa891366236987b27dc212db80
7
+ data.tar.gz: a3481761544d9b371cfdba6caf4e111921b8fddd6b6628c2e900d3d90bd00193a94385a9bb87c609a0ae324715ff2939e356d5f6b1c498587b961917e83fdf4e
data/README.md CHANGED
@@ -25,20 +25,32 @@ Or install it yourself as:
25
25
  - Make OAuth 2.0 Client. (other)
26
26
  - Download client secret json
27
27
 
28
+
28
29
  ```ruby
29
- credential_store_file = Rails.root.join('config', 'credential-oauth2-supporter.json').to_s
30
- sheet_key = 'YOUR_SHEET_KEY'
30
+ sheet_key = 'example_sheet_id'
31
31
 
32
32
  GoogleSpreadsheetFetcher.configure do |config|
33
- config.client_secrets_file_path = Rails.root.join('config', 'client_secrets_pokota_supporter.json').to_s
33
+ config.client_secrets_file = 'client_secrets_file_path.json'
34
+ config.credential_store_file = 'credential_store_file_path.json'
34
35
  end
35
36
 
36
37
  user_id = 'sample'
37
- fetcher = GoogleSpreadsheetFetcher::Fetcher.new(credential_store_file, user_id, sheet_key)
38
38
 
39
- fetcher.fetch_by_index(0)
40
- fetcher.fetch_by_title('title')
41
- fetcher.fetch_by_gid('gid')
39
+ fetcher = GoogleSpreadsheetFetcher::Fetcher.new(sheet_key, user_id)
40
+
41
+ fetcher.fetch_all_rows_by!(index: 0)
42
+ fetcher.fetch_all_rows_by!(title: 'sheet_title')
43
+ fetcher.fetch_all_rows_by!(sheet_id: 1234567890)
44
+
45
+
46
+ # or, you can do a bulk fetching. In this case, you only need to access the API once,
47
+ # but it will take a little longer on first fetch.
48
+ fetcher = GoogleSpreadsheetFetcher::BulkFetcher.new(sheet_key, user_id)
49
+ fetcher.fetch
50
+
51
+ fetcher.all_rows_by!(index: 0)
52
+ fetcher.all_rows_by!(title: 'sheet_title')
53
+ fetcher.all_rows_by!(sheet_id: 1234567890)
42
54
  ```
43
55
 
44
56
  ## Development
@@ -6,8 +6,8 @@ require 'google_spreadsheet_fetcher/version'
6
6
  Gem::Specification.new do |spec|
7
7
  spec.name = "google_spreadsheet_fetcher"
8
8
  spec.version = GoogleSpreadsheetFetcher::VERSION
9
- spec.authors = ["Takahiro Ooishi"]
10
- spec.email = ["taka0125@gmail.com"]
9
+ spec.authors = ["Takahiro Ooishi", "Yuya Yokosuka"]
10
+ spec.email = ["taka0125@gmail.com", "yuya.yokosuka@gmail.com"]
11
11
 
12
12
  spec.summary = %q{Google Spreadsheet fetcher}
13
13
  spec.description = %q{Google Spreadsheet fetcher}
@@ -0,0 +1,82 @@
1
+ require 'google/apis/sheets_v4'
2
+ require 'shellwords'
3
+
4
+ module GoogleSpreadsheetFetcher
5
+ class BulkFetcher
6
+ # @param [String] spreadsheet_id
7
+ # @param [String] user_id
8
+ # @param [GoogleSpreadsheetFetcher::Config] config
9
+ # @param [String] application_name
10
+ def initialize(spreadsheet_id, user_id, config: nil, application_name: nil)
11
+ @spreadsheet_id = spreadsheet_id
12
+ @user_id = user_id
13
+ @config = config || GoogleSpreadsheetFetcher.config
14
+ @application_name = application_name
15
+
16
+ @spreadsheet = nil
17
+ end
18
+
19
+ def fetch
20
+ @spreadsheet = service.get_spreadsheet(@spreadsheet_id, fields: 'sheets(properties,data.rowData.values(formattedValue))')
21
+ self
22
+ end
23
+
24
+ # @param [Integer] index
25
+ # @param [Integer] sheet_id
26
+ # @param [String] title
27
+ # @param [Integer] skip
28
+ # @param [Boolean] structured
29
+ def all_rows_by!(index: nil, sheet_id: nil, title: nil, skip: 0, structured: false)
30
+ sheet = sheet_by!(index: index, sheet_id: sheet_id, title: title)
31
+ sheet_to_array(sheet, skip: skip, structured: structured)
32
+ end
33
+
34
+ def service
35
+ @service ||= GoogleSpreadsheetFetcher::SheetsServiceBuilder.new(@user_id, config: @config, application_name: @application_name).build
36
+ end
37
+
38
+ private
39
+
40
+ def sheet_by!(index: nil, sheet_id: nil, title: nil)
41
+ raise SpreadsheetNotFound if @spreadsheet.sheets.blank?
42
+
43
+ if index.present?
44
+ return @spreadsheet.sheets.find { |sheet| sheet.properties.index == index }
45
+ elsif sheet_id.present?
46
+ return @spreadsheet.sheets.find { |sheet| sheet.properties.sheet_id == sheet_id }
47
+ elsif title.present?
48
+ return @spreadsheet.sheets.find { |sheet| sheet.properties.title == title }
49
+ end
50
+
51
+ raise SheetNotFound
52
+ end
53
+
54
+ def sheet_to_array(sheet, skip: 0, structured: false)
55
+ sheet_data = sheet&.data&.first
56
+ return [] if sheet_data.nil?
57
+
58
+ rows = sheet_data.row_data.map do |row_data|
59
+ values = row_data.values
60
+ next [''] if values.nil?
61
+
62
+ values.map { |cell| cell&.formatted_value || "" }
63
+ end
64
+
65
+ headers = rows.first
66
+ count = headers.count
67
+
68
+ if structured
69
+ rows.delete_at(0)
70
+ rows.slice!(0, skip)
71
+ rows.map { |r| headers.zip(r).to_h }
72
+ else
73
+ rows.slice!(0, skip)
74
+ rows.map { |r| fill_array(r, count) }
75
+ end
76
+ end
77
+
78
+ def fill_array(items, count, fill: '')
79
+ items + (count - items.count).times.map { fill }
80
+ end
81
+ end
82
+ end
@@ -5,7 +5,8 @@ module GoogleSpreadsheetFetcher
5
5
  include ActiveSupport::Configurable
6
6
 
7
7
  config_accessor :client_secrets_file
8
- config_accessor :credential_store_file
8
+ config_accessor :credential_store_file # required if token_store not set
9
+ config_accessor :token_store # required if credential_store_file not set
9
10
  config_accessor :scopes
10
11
 
11
12
  def self.default_config
@@ -1,3 +1,4 @@
1
1
  module GoogleSpreadsheetFetcher
2
+ class SpreadsheetNotFound < StandardError; end
2
3
  class SheetNotFound < StandardError; end
3
4
  end
@@ -1,6 +1,4 @@
1
1
  require 'google/apis/sheets_v4'
2
- require 'googleauth'
3
- require 'googleauth/stores/file_token_store'
4
2
  require 'shellwords'
5
3
 
6
4
  module GoogleSpreadsheetFetcher
@@ -14,7 +12,7 @@ module GoogleSpreadsheetFetcher
14
12
  def initialize(spreadsheet_id, user_id, config: nil, application_name: nil)
15
13
  @spreadsheet_id = spreadsheet_id
16
14
  @user_id = user_id
17
- @config = config || GoogleSpreadsheetFetcher.config
15
+ @config = config
18
16
  @application_name = application_name
19
17
  end
20
18
 
@@ -24,10 +22,7 @@ module GoogleSpreadsheetFetcher
24
22
  end
25
23
 
26
24
  def service
27
- @service ||= Google::Apis::SheetsV4::SheetsService.new.tap do |service|
28
- service.authorization = fetch_credentials
29
- service.client_options.application_name = @application_name if @application_name.present?
30
- end
25
+ @service ||= GoogleSpreadsheetFetcher::SheetsServiceBuilder.new(@user_id, config: @config, application_name: @application_name).build
31
26
  end
32
27
 
33
28
  private
@@ -71,24 +66,8 @@ module GoogleSpreadsheetFetcher
71
66
  raise SheetNotFound
72
67
  end
73
68
 
74
- def fetch_credentials
75
- client_id = Google::Auth::ClientId.from_file(@config.client_secrets_file)
76
- token_store = Google::Auth::Stores::FileTokenStore.new(file: @config.credential_store_file)
77
- authorizer = Google::Auth::UserAuthorizer.new(client_id, @config.scopes, token_store)
78
-
79
- credentials = authorizer.get_credentials(@user_id)
80
- return credentials if credentials.present?
81
-
82
- url = authorizer.get_authorization_url(base_url: OOB_URI)
83
- escaped_url = url.shellescape
84
- system("open #{escaped_url}")
85
- puts "Open #{url} in your browser and enter the resulting code: "
86
- code = STDIN.gets
87
- authorizer.get_and_store_credentials_from_code(user_id: @user_id, code: code, base_url: OOB_URI)
88
- end
89
-
90
69
  def spreadsheet
91
- service.get_spreadsheet(@spreadsheet_id)
70
+ @spreadsheet ||= service.get_spreadsheet(@spreadsheet_id)
92
71
  end
93
72
 
94
73
  def fill_array(items, count, fill: "")
@@ -0,0 +1,44 @@
1
+ require 'google/apis/sheets_v4'
2
+ require 'googleauth'
3
+ require 'googleauth/stores/file_token_store'
4
+ require 'shellwords'
5
+
6
+ module GoogleSpreadsheetFetcher
7
+ class SheetsServiceBuilder
8
+ OOB_URI = 'urn:ietf:wg:oauth:2.0:oob'
9
+
10
+ # @param [String] user_id
11
+ # @param [GoogleSpreadsheetFetcher::Config] config
12
+ # @param [String] application_name
13
+ def initialize(user_id, config: nil, application_name: nil)
14
+ @user_id = user_id
15
+ @config = config || GoogleSpreadsheetFetcher.config
16
+ @application_name = application_name
17
+ @token_store = @config.token_store || Google::Auth::Stores::FileTokenStore.new(file: @config.credential_store_file)
18
+ end
19
+
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?
24
+ end
25
+ end
26
+
27
+ private
28
+
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
43
+ end
44
+ end
@@ -1,3 +1,3 @@
1
1
  module GoogleSpreadsheetFetcher
2
- VERSION = "1.5.0"
2
+ VERSION = "1.8.1"
3
3
  end
@@ -4,6 +4,8 @@ require "google_spreadsheet_fetcher/version"
4
4
  require "google_spreadsheet_fetcher/config"
5
5
  require "google_spreadsheet_fetcher/error"
6
6
  require "google_spreadsheet_fetcher/fetcher"
7
+ require "google_spreadsheet_fetcher/bulk_fetcher"
8
+ require "google_spreadsheet_fetcher/sheets_service_builder"
7
9
 
8
10
  module GoogleSpreadsheetFetcher
9
11
  def self.config
metadata CHANGED
@@ -1,14 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: google_spreadsheet_fetcher
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.5.0
4
+ version: 1.8.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Takahiro Ooishi
8
- autorequire:
8
+ - Yuya Yokosuka
9
+ autorequire:
9
10
  bindir: exe
10
11
  cert_chain: []
11
- date: 2020-04-17 00:00:00.000000000 Z
12
+ date: 2021-12-09 00:00:00.000000000 Z
12
13
  dependencies:
13
14
  - !ruby/object:Gem::Dependency
14
15
  name: google-api-client
@@ -83,6 +84,7 @@ dependencies:
83
84
  description: Google Spreadsheet fetcher
84
85
  email:
85
86
  - taka0125@gmail.com
87
+ - yuya.yokosuka@gmail.com
86
88
  executables: []
87
89
  extensions: []
88
90
  extra_rdoc_files: []
@@ -97,14 +99,16 @@ files:
97
99
  - bin/setup
98
100
  - google_spreadsheet_fetcher.gemspec
99
101
  - lib/google_spreadsheet_fetcher.rb
102
+ - lib/google_spreadsheet_fetcher/bulk_fetcher.rb
100
103
  - lib/google_spreadsheet_fetcher/config.rb
101
104
  - lib/google_spreadsheet_fetcher/error.rb
102
105
  - lib/google_spreadsheet_fetcher/fetcher.rb
106
+ - lib/google_spreadsheet_fetcher/sheets_service_builder.rb
103
107
  - lib/google_spreadsheet_fetcher/version.rb
104
108
  homepage: https://github.com/taka0125/google_spreadsheet_fetcher
105
109
  licenses: []
106
110
  metadata: {}
107
- post_install_message:
111
+ post_install_message:
108
112
  rdoc_options: []
109
113
  require_paths:
110
114
  - lib
@@ -119,8 +123,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
119
123
  - !ruby/object:Gem::Version
120
124
  version: '0'
121
125
  requirements: []
122
- rubygems_version: 3.1.2
123
- signing_key:
126
+ rubygems_version: 3.2.22
127
+ signing_key:
124
128
  specification_version: 4
125
129
  summary: Google Spreadsheet fetcher
126
130
  test_files: []