google_spreadsheet_fetcher 0.4.0 → 1.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: 13de044dc3ec95b8296d47bb5374e451cc6b10069b95536c560db70eeafa22d2
4
- data.tar.gz: a95098f831784ceb09d2d9b7e36d15cb8f3e1145118a3359124edb660c978582
3
+ metadata.gz: 5a797f4e71d74b4c24d5005bf91c06e699baab814970e1c4d6070808550bfa41
4
+ data.tar.gz: 3822500ace1a0d202f0b6739b1e99f68163b4b5c1577b4ac434c470ef59a684a
5
5
  SHA512:
6
- metadata.gz: f6155050646d3a53498c7fe12d6f435e59b8e0eff0fc6cccdc8c25ff2eaf38465409a8334ed0b08968ca208a42a46ce4a6965342cf39bc8e2f45face082eb5b5
7
- data.tar.gz: fa6eacccacaae8c6d5a3d62dee48271c794cdc68d2470001ad97c0329e8cc642371cb61ae102667a7789bce022003e4f50e894e497af88e3297179c8f22f2ce3
6
+ metadata.gz: 4592c56919027ea291b6a773e0c83417051db6469a5aae1acf3324b016f90db6a83bb0c5a825afa7615a12e0e2c74abb55a46535e1fd84e37015bba50406920f
7
+ data.tar.gz: '09340c7e08bfcac42337476365ab53277b93b71946ef8797c52c9c7013dd77eb30b4ddd2a11e3ac13c61c441f2854f8aa8c4aaa27c6eb05b3f2f241c65ba138b'
@@ -23,7 +23,7 @@ Gem::Specification.new do |spec|
23
23
  spec.required_ruby_version = '>= 2.3.0'
24
24
 
25
25
  spec.add_dependency 'google-api-client', '~> 0.9'
26
- spec.add_dependency 'google_drive', '~> 2.1.3'
26
+ spec.add_dependency 'activesupport'
27
27
 
28
28
  spec.add_development_dependency "bundler"
29
29
  spec.add_development_dependency "rake"
@@ -1,10 +1,12 @@
1
+ require "active_support/core_ext"
1
2
  require "google_spreadsheet_fetcher/version"
2
3
  require "google_spreadsheet_fetcher/config"
4
+ require "google_spreadsheet_fetcher/error"
3
5
  require "google_spreadsheet_fetcher/fetcher"
4
6
 
5
7
  module GoogleSpreadsheetFetcher
6
8
  def self.config
7
- @config ||= Config.new
9
+ @config ||= Config.default_config
8
10
  end
9
11
 
10
12
  def self.configure
@@ -1,7 +1,15 @@
1
- require 'active_support/configurable'
2
-
3
1
  module GoogleSpreadsheetFetcher
4
2
  class Config
5
- attr_accessor :client_secrets_file_path
3
+ include ActiveSupport::Configurable
4
+
5
+ config_accessor :client_secrets_file
6
+ config_accessor :credential_store_file
7
+ config_accessor :scopes
8
+
9
+ def self.default_config
10
+ new.tap do |config|
11
+ config.scopes = [Google::Apis::SheetsV4::AUTH_SPREADSHEETS_READONLY]
12
+ end
13
+ end
6
14
  end
7
15
  end
@@ -0,0 +1,3 @@
1
+ module GoogleSpreadsheetFetcher
2
+ class SheetNotFound < StandardError; end
3
+ end
@@ -7,99 +7,72 @@ module GoogleSpreadsheetFetcher
7
7
  class Fetcher
8
8
  OOB_URI = 'urn:ietf:wg:oauth:2.0:oob'
9
9
 
10
- def initialize(credential_store_file, user_id, sheet_key, application_name: nil)
11
- @client_secret_file = GoogleSpreadsheetFetcher.config.client_secrets_file_path
12
- @credential_store_file = credential_store_file
10
+ # @param [String] spreadsheet_id
11
+ # @param [String] user_id
12
+ # @param [GoogleSpreadsheetFetcher::Config] config
13
+ # @param [String] application_name
14
+ def initialize(spreadsheet_id, user_id, config: nil, application_name: nil)
15
+ @spreadsheet_id = spreadsheet_id
13
16
  @user_id = user_id
14
- @sheet_key = sheet_key
17
+ @config = config || GoogleSpreadsheetFetcher.config
15
18
  @application_name = application_name
16
19
  end
17
20
 
18
- #
19
- # Fetch all rows
20
- #
21
- # @param [String] ranges https://developers.google.com/sheets/api/guides/concepts#a1_notation
21
+ # @param [String] range(https://developers.google.com/sheets/api/guides/concepts#a1_notation)
22
+ # @param [Integer] skip
22
23
  def fetch_all_rows(range, skip: 0)
23
- rows = service.batch_get_spreadsheet_values(@sheet_key, ranges: [range])&.value_ranges&.first&.values
24
- return if rows.empty?
24
+ rows = service.batch_get_spreadsheet_values(@spreadsheet_id, ranges: [range])&.value_ranges&.first&.values
25
+ return [] if rows.blank?
25
26
 
26
27
  rows.slice!(0, skip)
27
28
  rows
28
29
  end
29
30
 
30
- def fetch_all_rows_by_index(index, skip: 0)
31
- sheet = sheet_by_index(index)
32
- raise if sheet.nil?
33
-
34
- range = "#{sheet.properties.title}!A:Z"
35
- fetch_all_rows(range, skip: skip)
36
- end
37
-
38
- def fetch_all_rows_by_gid(gid, skip: 0)
39
- sheet = sheet_by_gid(gid)
40
- raise if sheet.nil?
41
-
42
- range = "#{sheet.properties.title}!A:Z"
43
- fetch_all_rows(range, skip: skip)
44
- end
45
-
46
- def fetch_all_rows_by_title(title, skip: 0)
47
- sheet = sheet_by_title(title)
48
- raise if sheet.nil?
31
+ def fetch_all_rows_by!(index: nil, sheet_id: nil, title: nil, skip: 0)
32
+ sheet = fetch_sheet_by!(index: index, sheet_id: sheet_id, title: title)
49
33
 
50
34
  range = "#{sheet.properties.title}!A:Z"
51
35
  fetch_all_rows(range, skip: skip)
52
36
  end
53
37
 
54
38
  def service
55
- @service ||= begin
56
- credentials = fetch_credentials
57
-
58
- service = Google::Apis::SheetsV4::SheetsService.new
59
- service.authorization = credentials
60
- 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?
61
42
  end
62
43
  end
63
44
 
64
45
  private
65
46
 
66
- def sheet_by_index(index)
67
- service.get_spreadsheet(@sheet_key).sheets[index]
68
- end
47
+ def fetch_sheet_by!(index: nil, sheet_id: nil, title: nil)
48
+ sheets = spreadsheet.sheets
49
+ raise SheetNotFound if sheets.blank?
69
50
 
70
- def sheet_by_gid(gid)
71
- service.get_spreadsheet(@sheet_key).sheets.find { |s| s.properties.sheet_id == gid }
72
- end
51
+ return sheets[index] if index.present?
52
+ return sheets.find { |s| s.properties.sheet_id == sheet_id } if sheet_id.present?
53
+ return sheets.find { |s| s.properties.title == title } if title.present?
73
54
 
74
- def sheet_by_title(title)
75
- service.get_spreadsheet(@sheet_key).sheets.find { |s| s.properties.title == title }
55
+ raise SheetNotFound
76
56
  end
77
57
 
78
58
  def fetch_credentials
79
- oob_uri = 'urn:ietf:wg:oauth:2.0:oob'
80
-
81
- client_id = Google::Auth::ClientId.from_file(@client_secret_file)
82
- token_store = Google::Auth::Stores::FileTokenStore.new(file: @credential_store_file)
83
- authorizer = Google::Auth::UserAuthorizer.new(client_id, scopes, token_store)
59
+ client_id = Google::Auth::ClientId.from_file(@config.client_secrets_file)
60
+ token_store = Google::Auth::Stores::FileTokenStore.new(file: @config.credential_store_file)
61
+ authorizer = Google::Auth::UserAuthorizer.new(client_id, @config.scopes, token_store)
84
62
 
85
63
  credentials = authorizer.get_credentials(@user_id)
86
-
87
- if credentials.nil?
88
- url = authorizer.get_authorization_url(base_url: OOB_URI)
89
- escaped_url = url.shellescape
90
- system("open #{escaped_url}")
91
- puts "Open #{url} in your browser and enter the resulting code: "
92
- code = STDIN.gets
93
- credentials = authorizer.get_and_store_credentials_from_code(user_id: @user_id, code: code, base_url: oob_uri)
94
- end
95
-
96
- credentials
64
+ return credentials if credentials.present?
65
+
66
+ url = authorizer.get_authorization_url(base_url: OOB_URI)
67
+ escaped_url = url.shellescape
68
+ system("open #{escaped_url}")
69
+ puts "Open #{url} in your browser and enter the resulting code: "
70
+ code = STDIN.gets
71
+ authorizer.get_and_store_credentials_from_code(user_id: @user_id, code: code, base_url: OOB_URI)
97
72
  end
98
73
 
99
- def scopes
100
- [
101
- Google::Apis::SheetsV4::AUTH_SPREADSHEETS_READONLY
102
- ]
74
+ def spreadsheet
75
+ service.get_spreadsheet(@spreadsheet_id)
103
76
  end
104
77
  end
105
78
  end
@@ -1,3 +1,3 @@
1
1
  module GoogleSpreadsheetFetcher
2
- VERSION = "0.4.0"
2
+ VERSION = "1.0.0"
3
3
  end
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: 0.4.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Takahiro Ooishi
@@ -25,19 +25,19 @@ dependencies:
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0.9'
27
27
  - !ruby/object:Gem::Dependency
28
- name: google_drive
28
+ name: activesupport
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - "~>"
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: 2.1.3
33
+ version: '0'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - "~>"
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
- version: 2.1.3
40
+ version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: bundler
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -98,6 +98,7 @@ files:
98
98
  - google_spreadsheet_fetcher.gemspec
99
99
  - lib/google_spreadsheet_fetcher.rb
100
100
  - lib/google_spreadsheet_fetcher/config.rb
101
+ - lib/google_spreadsheet_fetcher/error.rb
101
102
  - lib/google_spreadsheet_fetcher/fetcher.rb
102
103
  - lib/google_spreadsheet_fetcher/version.rb
103
104
  homepage: https://github.com/taka0125/google_spreadsheet_fetcher