google_spreadsheet_fetcher 0.3.0 → 0.4.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: 2fd96c831d8647c6b4c6d03e2ab5b638a1cbcdc0898b09e41cf009b0f8641540
4
- data.tar.gz: 230ae8888e6c43f225da7d7e02a6c711d118838f25800c0317351cb932e1b6c6
3
+ metadata.gz: 13de044dc3ec95b8296d47bb5374e451cc6b10069b95536c560db70eeafa22d2
4
+ data.tar.gz: a95098f831784ceb09d2d9b7e36d15cb8f3e1145118a3359124edb660c978582
5
5
  SHA512:
6
- metadata.gz: 8ce5c1465561f4178c5e9169a18c6e7bbaab425c70dae8c92012fe68cb2d49286ea179ea457d403f09d82d7f067624865319f9e8c2bd4015c3000f542f6851b0
7
- data.tar.gz: ab06f820814367235871b12845d06b39487ef745a78619064d2dc2a35b6acafef0ae02db2da9519699800bec0052c493f2ddc2257d53eb201ca72a2629338d8d
6
+ metadata.gz: f6155050646d3a53498c7fe12d6f435e59b8e0eff0fc6cccdc8c25ff2eaf38465409a8334ed0b08968ca208a42a46ce4a6965342cf39bc8e2f45face082eb5b5
7
+ data.tar.gz: fa6eacccacaae8c6d5a3d62dee48271c794cdc68d2470001ad97c0329e8cc642371cb61ae102667a7789bce022003e4f50e894e497af88e3297179c8f22f2ce3
@@ -15,34 +15,64 @@ module GoogleSpreadsheetFetcher
15
15
  @application_name = application_name
16
16
  end
17
17
 
18
- def fetch_by_index(index)
19
- fetch_worksheet_by(index: index)
18
+ #
19
+ # Fetch all rows
20
+ #
21
+ # @param [String] ranges https://developers.google.com/sheets/api/guides/concepts#a1_notation
22
+ 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?
25
+
26
+ rows.slice!(0, skip)
27
+ rows
20
28
  end
21
29
 
22
- def fetch_by_title(title)
23
- fetch_worksheet_by(title: title)
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)
24
36
  end
25
37
 
26
- def fetch_by_gid(gid)
27
- fetch_worksheet_by(gid: gid)
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)
28
44
  end
29
45
 
30
- private
46
+ def fetch_all_rows_by_title(title, skip: 0)
47
+ sheet = sheet_by_title(title)
48
+ raise if sheet.nil?
31
49
 
32
- def fetch_worksheet_by(index: nil, title: nil, gid: nil)
33
- credentials = fetch_credentials
50
+ range = "#{sheet.properties.title}!A:Z"
51
+ fetch_all_rows(range, skip: skip)
52
+ end
34
53
 
35
- service = Google::Apis::SheetsV4::SheetsService.new
36
- service.client_options.application_name = @application_name if @application_name
37
- service.authorization = credentials
54
+ def service
55
+ @service ||= begin
56
+ credentials = fetch_credentials
38
57
 
39
- spreadsheet = service.get_spreadsheet(@sheet_key)
58
+ service = Google::Apis::SheetsV4::SheetsService.new
59
+ service.authorization = credentials
60
+ service
61
+ end
62
+ end
40
63
 
41
- return spreadsheet.worksheets[index] unless index.nil?
42
- return spreadsheet.worksheet_by_title(title) unless title.nil?
43
- return spreadsheet.worksheet_by_gid(gid) unless gid.nil?
64
+ private
65
+
66
+ def sheet_by_index(index)
67
+ service.get_spreadsheet(@sheet_key).sheets[index]
68
+ end
69
+
70
+ def sheet_by_gid(gid)
71
+ service.get_spreadsheet(@sheet_key).sheets.find { |s| s.properties.sheet_id == gid }
72
+ end
44
73
 
45
- raise
74
+ def sheet_by_title(title)
75
+ service.get_spreadsheet(@sheet_key).sheets.find { |s| s.properties.title == title }
46
76
  end
47
77
 
48
78
  def fetch_credentials
@@ -1,3 +1,3 @@
1
1
  module GoogleSpreadsheetFetcher
2
- VERSION = "0.3.0"
2
+ VERSION = "0.4.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.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Takahiro Ooishi