google_spreadsheet_fetcher 1.4.0 → 1.8.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: 31e87471fab356c69fa5e7459604c56f2d11f72a0ec650f9c9f35db74b9ac568
4
- data.tar.gz: 203cdf0b177bab7864e92244316ae5d4673ef7770d8811910146f1549bbb1840
3
+ metadata.gz: cff3e92433c66cea9ef0c7b38034a0a1dc8b60147d23b79b8fdd8c63b75d410b
4
+ data.tar.gz: 3990a45e68356829594c4efe28ca5e5d22930e960276a66787c85e8746aef0a8
5
5
  SHA512:
6
- metadata.gz: 9dd989ea11274389253191e93524693ba7133650b08fb364b5406c77b7b62f55631d6604347e206579c3fa185018887bfe1c50e594cb2beda23d635137a8f9ab
7
- data.tar.gz: e6292cc11e63682c90c74884ee4eb8676bc7d1e1d652fcb1fe59b994f55c0303b348428c95ab6c58c72cc8eddfcf13ff7b5582a7383c86b59c0948539cf3cf29
6
+ metadata.gz: a89913bd36f9d02448d8705e5b28e901306ab65d7abaa56879d1bfd961939e5a7978a4169a9e9b49ceee0b90b38fda03435e230fe00a25c3cfd05487161e7c18
7
+ data.tar.gz: 1378109ec133abfbb2caed2022cd3b31fe472a4ca9ec1ee7a9c4c3532df045f70c917e3adc38c00d47d4d56519aefc96f8d0a065d019de8c568163ab35f20cc2
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
@@ -37,7 +32,7 @@ module GoogleSpreadsheetFetcher
37
32
  # @param [Boolean] structured
38
33
  def fetch_all_rows(sheet, skip: 0, structured: false)
39
34
  # https://developers.google.com/sheets/api/guides/concepts#a1_notation
40
- range = "#{sheet.properties.title}!A:Z"
35
+ range = "#{sheet.properties.title}!A:ZZ"
41
36
  rows = service.get_spreadsheet_values(@spreadsheet_id, range)&.values
42
37
  return [] if rows.blank?
43
38
 
@@ -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.4.0"
2
+ VERSION = "1.8.0"
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.4.0
4
+ version: 1.8.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: 2019-10-05 00:00:00.000000000 Z
12
+ date: 2021-11-12 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: []