google_spreadsheet_fetcher 1.6.0 → 1.7.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 484a5652add269569653176f0362c21e7490a90e7207cef09fc0b9337514ac9e
4
- data.tar.gz: 5c506fdb046946b261e345e82f26c2c07eb9bd4ef4f55f1fd0936376c7404f0b
3
+ metadata.gz: adf35fcabe96fa86ae6e1a6e30d728531471b24c8a1e358e7bf7506db08286ba
4
+ data.tar.gz: e6fa6c579750fb93ab46f28bfdf8f6ddda7d00b2c00c79dd29aff62ea9e269eb
5
5
  SHA512:
6
- metadata.gz: 79a4a84c92f1405e04482804c93d8746616aac842ecc4151332ac4a6f10a8e9ffbde8a41e1792e875bc4f9fcd712153afa136ec50695fa208332108a007ef759
7
- data.tar.gz: c82292382ef20832fd6ec8ccd6b686c677d2aaec9afabc86e96a0023a4be8243ddd3f7bf06d28c5abc40e8142f1d50de060e52a3f009fae26ed40998dfe61bda
6
+ metadata.gz: 40492b6fbe256c65df40a92fc1e4a9d941a2eb25feb82f9f04a68226eb3d4b60defb5d5ba9cebf4dae042d8cb0e8c8ed2b60d76d9b61f2c886d646b04ba88d3d
7
+ data.tar.gz: 1ccab637e4927b1031c6e4b262461fb108a5728582bc9e7c9ffd31f29545c23ab146640da89543a56912958bf200452874e8cb5614329eb1ece7ccdc5a909e1d
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}
@@ -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
@@ -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
@@ -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,22 +66,6 @@ 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
70
  @spreadsheet ||= service.get_spreadsheet(@spreadsheet_id)
92
71
  end
@@ -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
+ end
18
+
19
+ def build
20
+ Google::Apis::SheetsV4::SheetsService.new.tap do |service|
21
+ service.authorization = fetch_credentials
22
+ service.client_options.application_name = @application_name if @application_name.present?
23
+ end
24
+ end
25
+
26
+ private
27
+
28
+ def fetch_credentials
29
+ client_id = Google::Auth::ClientId.from_file(@config.client_secrets_file)
30
+ token_store = Google::Auth::Stores::FileTokenStore.new(file: @config.credential_store_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.6.0"
2
+ VERSION = "1.7.0"
3
3
  end
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.6.0
4
+ version: 1.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Takahiro Ooishi
8
+ - Yuya Yokosuka
8
9
  autorequire:
9
10
  bindir: exe
10
11
  cert_chain: []
11
- date: 2020-06-29 00:00:00.000000000 Z
12
+ date: 2020-07-17 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,9 +99,11 @@ 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: []
@@ -119,7 +123,7 @@ 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
126
+ rubygems_version: 3.0.3
123
127
  signing_key:
124
128
  specification_version: 4
125
129
  summary: Google Spreadsheet fetcher