sheet_reader_writer 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 49a1e38c8954ca72c9ac99c94edfbe60beb6447b
4
+ data.tar.gz: 3e5cd57904c2011a473f9045207cd1007fb9aeca
5
+ SHA512:
6
+ metadata.gz: 951d29bb7c422436803385f7b26abb8d35c6fd36b1f4b308585ebeb3c8353d35b64f16f9d44d9861da253fdd7de8bbbb626611c55bc13de522c7ecc55fbe536c
7
+ data.tar.gz: fe95f4ac11f327ad5c64caaa9cfd733f197090c32650de95a8848c19eb21cafdd4369b90518a6359bc23b653a8297b3f07eba7169ce33a243637e3a4ef00b0fe
@@ -0,0 +1,148 @@
1
+ require 'google/apis/sheets_v4'
2
+ require 'googleauth'
3
+ require 'sheet_reader_writer/errors'
4
+
5
+ class SheetReaderWriter
6
+ REQUIRED_ENV_VARS = %w[GOOGLE_CLIENT_EMAIL
7
+ GOOGLE_ACCOUNT_TYPE
8
+ GOOGLE_PRIVATE_KEY]
9
+
10
+ # Creates a new instance to interact with Google Sheets
11
+ #
12
+ # Example:
13
+ # >> SheetReaderWriter.new("1ukhJwquqRTgfX-G-nxV6AsAH726TOsKQpPJfpqNjWGg").read("Sheet 1")
14
+ # => [{"foo"=>"hey", "bar"=>"ho"},
15
+ # {"foo"=>"let's ", "bar"=>"go"}]
16
+ #
17
+ # Arguments:
18
+ # sheet_id: (String) The google sheet identifier.
19
+ #
20
+ def initialize(sheet_id, write_permission: true)
21
+ raise MissingEnvVars unless required_env_vars?
22
+ ensure_valid_key_format
23
+
24
+ with_exceptions do
25
+ sheets = Google::Apis::SheetsV4::SheetsService.new
26
+
27
+ scopes = if write_permission
28
+ ['https://www.googleapis.com/auth/spreadsheets']
29
+ else
30
+ ['https://www.googleapis.com/auth/spreadsheets.readonly']
31
+ end
32
+
33
+ sheets.authorization = Google::Auth.get_application_default(scopes)
34
+
35
+ @sheet_service = sheets
36
+ @sheet_id = sheet_id
37
+ end
38
+ end
39
+
40
+ # Fetches the content of a google spreadsheet
41
+ #
42
+ # Example:
43
+ # >> sheet_reader_writer.read("Sheet 1")
44
+ # => [{"foo"=>"hey", "bar"=>"ho"},
45
+ # {"foo"=>"let's ", "bar"=>"go"}]
46
+ #
47
+ # Arguments:
48
+ # sheet_name: (String) The sheet name, by default it's the first one
49
+ #
50
+ def read(sheet_name = "")
51
+ with_exceptions do
52
+ raw_values = @sheet_service.get_spreadsheet_values(@sheet_id, "#{sheet_name}!A:ZZ").values
53
+ rows_as_hashes(raw_values)
54
+ end
55
+ end
56
+
57
+ # Writes the specified content to a google spreadsheet
58
+ #
59
+ # Example:
60
+ #
61
+ # screen_reader_writer.write [
62
+ # {"foo"=>"hey", "bar"=>"ho"},
63
+ # {"foo"=>"let's"},
64
+ # {"bar"=>"go"}
65
+ # ]
66
+ #
67
+ # Arguments:
68
+ # row_hashes: (Array of hashes) The values to update in the same format as returned by the read method
69
+ #
70
+ def write(row_hashes, sheet_name = "")
71
+ value_range_object = {
72
+ major_dimension: "ROWS",
73
+ values: to_values_array(row_hashes)
74
+ }
75
+
76
+ with_exceptions do
77
+ @sheet_service.update_spreadsheet_value(@sheet_id, "#{sheet_name}!A:ZZ", value_range_object, value_input_option: 'USER_ENTERED')
78
+ end
79
+ end
80
+
81
+ # Clears a google spreadsheet
82
+ #
83
+ # Example:
84
+ # >> sheet_reader_writer.clear
85
+ #
86
+ def clear(sheet_name = "")
87
+ with_exceptions do
88
+ @sheet_service.clear_values(@sheet_id, "#{sheet_name}!A:ZZ")
89
+ end
90
+ end
91
+
92
+ private
93
+
94
+ def to_values_array(row_hashes)
95
+ keys = row_hashes.map do |row_hash|
96
+ row_hash.keys
97
+ end.flatten.uniq.sort
98
+
99
+ values_array = row_hashes.map do |row_hash|
100
+ keys.map do |key|
101
+ row_hash[key]
102
+ end
103
+ end
104
+
105
+ values_array.unshift(keys)
106
+ end
107
+
108
+ def with_exceptions
109
+ begin
110
+ yield
111
+ rescue Google::Apis::ClientError => e
112
+ raise BadSheetId if e.message =~ /notFound/
113
+ raise Unauthorized if e.message =~ /forbidden/
114
+ rescue
115
+ raise Error
116
+ end
117
+ end
118
+
119
+ def ensure_valid_key_format
120
+ ENV['GOOGLE_PRIVATE_KEY'] = ENV['GOOGLE_PRIVATE_KEY'].gsub(/\\n/, "\n")
121
+ end
122
+
123
+ def rows_as_hashes(rows)
124
+ keys, *rest = rows
125
+
126
+ rest.map do |row|
127
+ Hash[keys.zip(convery_empty_cells_to_nil(row))]
128
+ end
129
+ end
130
+
131
+ def convery_empty_cells_to_nil(row)
132
+ row.map do |cell|
133
+ if cell.strip == ""
134
+ nil
135
+ else
136
+ cell
137
+ end
138
+ end
139
+ end
140
+
141
+ def required_env_vars?
142
+ REQUIRED_ENV_VARS.all? do |e|
143
+ ENV.has_key?(e) &&
144
+ ENV.fetch(e) &&
145
+ ENV.fetch(e).strip != ""
146
+ end
147
+ end
148
+ end
@@ -0,0 +1,24 @@
1
+ class SheetReaderWriter
2
+ class Error < StandardError;
3
+ def backtrace
4
+ return cause.backtrace if cause
5
+ super
6
+ end
7
+
8
+ def message
9
+ return cause.message if cause
10
+ super
11
+ end
12
+ end
13
+
14
+ class BadSheetId < Error; end
15
+ class Unauthorized < Error; end
16
+
17
+ class MissingEnvVars < Error
18
+ def message
19
+ "Missing environment variables for the service account.\n" \
20
+ "Please provide #{REQUIRED_ENV_VARS.join(', ')}.\n" \
21
+ "See https://github.com/clearbit/sheet_reader_writer for more info.\n"
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,3 @@
1
+ class SheetReaderWriter
2
+ VERSION='1.0.0'
3
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sheet_reader_writer
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Clearbit
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-11-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: googleauth
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.5'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: google-api-client
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.9'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.9'
41
+ description: A gem to read and write to google spreadsheets
42
+ email: daniel@clearbit.com
43
+ executables: []
44
+ extensions: []
45
+ extra_rdoc_files: []
46
+ files:
47
+ - lib/sheet_reader_writer.rb
48
+ - lib/sheet_reader_writer/errors.rb
49
+ - lib/sheet_reader_writer/version.rb
50
+ homepage: http://github.com/cleabit/sheet_reader_writer
51
+ licenses:
52
+ - MIT
53
+ metadata: {}
54
+ post_install_message:
55
+ rdoc_options: []
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ requirements: []
69
+ rubyforge_project:
70
+ rubygems_version: 2.2.5
71
+ signing_key:
72
+ specification_version: 4
73
+ summary: Read and write to google spreadsheets
74
+ test_files: []