google_spreadsheet_fetcher 1.1.0 → 1.6.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: cf51e7e9a7e8375b321cff2bb3398cfe2234ca57af2c1952ea2fb1a272a9de27
4
- data.tar.gz: 9e83feb58028dbe9344834f1067dc188fbd01b273bc25f8c1847a40119f8a2cf
3
+ metadata.gz: 484a5652add269569653176f0362c21e7490a90e7207cef09fc0b9337514ac9e
4
+ data.tar.gz: 5c506fdb046946b261e345e82f26c2c07eb9bd4ef4f55f1fd0936376c7404f0b
5
5
  SHA512:
6
- metadata.gz: 9a7b5a6bc218f915bd4f987e7679157e0db5c89018b434f8ad76c9f4e647cfd0dc32c66e40053d07366165926b6638d37254fddf5043ffed8b7de5b55b035fa4
7
- data.tar.gz: 0476d4f615072e193077a59855ad6236ee7ec1d52a0fc6eb0838284cbdb1e47e96236fde5e6aad4e4ffa41d2dee733be32e948ee3a443141fe0903110243d56f
6
+ metadata.gz: 79a4a84c92f1405e04482804c93d8746616aac842ecc4151332ac4a6f10a8e9ffbde8a41e1792e875bc4f9fcd712153afa136ec50695fa208332108a007ef759
7
+ data.tar.gz: c82292382ef20832fd6ec8ccd6b686c677d2aaec9afabc86e96a0023a4be8243ddd3f7bf06d28c5abc40e8142f1d50de060e52a3f009fae26ed40998dfe61bda
@@ -1,3 +1,4 @@
1
+ require "active_support/json"
1
2
  require "active_support/core_ext"
2
3
  require "google_spreadsheet_fetcher/version"
3
4
  require "google_spreadsheet_fetcher/config"
@@ -1,3 +1,5 @@
1
+ require 'active_support/configurable'
2
+
1
3
  module GoogleSpreadsheetFetcher
2
4
  class Config
3
5
  include ActiveSupport::Configurable
@@ -1,6 +1,6 @@
1
+ require 'google/apis/sheets_v4'
1
2
  require 'googleauth'
2
3
  require 'googleauth/stores/file_token_store'
3
- require "google/apis/sheets_v4"
4
4
  require 'shellwords'
5
5
 
6
6
  module GoogleSpreadsheetFetcher
@@ -18,27 +18,9 @@ module GoogleSpreadsheetFetcher
18
18
  @application_name = application_name
19
19
  end
20
20
 
21
- # @param [String] range(https://developers.google.com/sheets/api/guides/concepts#a1_notation)
22
- # @param [Integer] skip
23
- def fetch_all_rows(range, skip: 0, structured: false)
24
- rows = service.batch_get_spreadsheet_values(@spreadsheet_id, ranges: [range])&.value_ranges&.first&.values
25
- return [] if rows.blank?
26
-
27
- if structured
28
- headers = rows.delete_at(0)
29
- rows.slice!(0, skip)
30
- rows.map { |r| [headers, r].transpose.to_h }
31
- else
32
- rows.slice!(0, skip)
33
- rows
34
- end
35
- end
36
-
37
21
  def fetch_all_rows_by!(index: nil, sheet_id: nil, title: nil, skip: 0, structured: false)
38
22
  sheet = fetch_sheet_by!(index: index, sheet_id: sheet_id, title: title)
39
-
40
- range = "#{sheet.properties.title}!A:Z"
41
- fetch_all_rows(range, skip: skip, structured: structured)
23
+ fetch_all_rows(sheet, skip: skip, structured: structured)
42
24
  end
43
25
 
44
26
  def service
@@ -50,13 +32,41 @@ module GoogleSpreadsheetFetcher
50
32
 
51
33
  private
52
34
 
35
+ # @param [Google::Apis::SheetsV4::Sheet] sheet
36
+ # @param [Integer] skip
37
+ # @param [Boolean] structured
38
+ def fetch_all_rows(sheet, skip: 0, structured: false)
39
+ # https://developers.google.com/sheets/api/guides/concepts#a1_notation
40
+ range = "#{sheet.properties.title}!A:ZZ"
41
+ rows = service.get_spreadsheet_values(@spreadsheet_id, range)&.values
42
+ return [] if rows.blank?
43
+
44
+ headers = rows.first
45
+ count = headers.count
46
+
47
+ if structured
48
+ rows.delete_at(0)
49
+ rows.slice!(0, skip)
50
+ rows.map { |r| headers.zip(r).to_h }
51
+ else
52
+ rows.slice!(0, skip)
53
+ rows.map { |r| fill_array(r, count) }
54
+ end
55
+ end
56
+
53
57
  def fetch_sheet_by!(index: nil, sheet_id: nil, title: nil)
54
58
  sheets = spreadsheet.sheets
55
59
  raise SheetNotFound if sheets.blank?
56
60
 
57
- return sheets[index] if index.present?
58
- return sheets.find { |s| s.properties.sheet_id == sheet_id } if sheet_id.present?
59
- return sheets.find { |s| s.properties.title == title } if title.present?
61
+ sheet = if index.present?
62
+ sheets[index]
63
+ elsif sheet_id.present?
64
+ sheets.find { |s| s.properties.sheet_id == sheet_id }
65
+ elsif title.present?
66
+ sheets.find { |s| s.properties.title == title }
67
+ end
68
+
69
+ return sheet if sheet.present?
60
70
 
61
71
  raise SheetNotFound
62
72
  end
@@ -78,7 +88,11 @@ module GoogleSpreadsheetFetcher
78
88
  end
79
89
 
80
90
  def spreadsheet
81
- service.get_spreadsheet(@spreadsheet_id)
91
+ @spreadsheet ||= service.get_spreadsheet(@spreadsheet_id)
92
+ end
93
+
94
+ def fill_array(items, count, fill: "")
95
+ items + (count - items.count).times.map { fill }
82
96
  end
83
97
  end
84
98
  end
@@ -1,3 +1,3 @@
1
1
  module GoogleSpreadsheetFetcher
2
- VERSION = "1.1.0"
2
+ VERSION = "1.6.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: google_spreadsheet_fetcher
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Takahiro Ooishi
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-10-03 00:00:00.000000000 Z
11
+ date: 2020-06-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: google-api-client
@@ -119,7 +119,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
119
119
  - !ruby/object:Gem::Version
120
120
  version: '0'
121
121
  requirements: []
122
- rubygems_version: 3.0.3
122
+ rubygems_version: 3.1.2
123
123
  signing_key:
124
124
  specification_version: 4
125
125
  summary: Google Spreadsheet fetcher