sheet_reader 0.0.0.pre
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/sheet_reader/errors.rb +23 -0
- data/lib/sheet_reader.rb +65 -0
- data/lib/version.rb +3 -0
- metadata +74 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 980987a112f514cf9c81745f1affe0180a30ff75
|
4
|
+
data.tar.gz: ac9a86acf7217220f71a18a1f6817541015b6052
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: af26e1e990c24b31752d8c126e82c2db6b0848e20d41d76a1bebb8e6fb70b6568ca23c65fc7bdb6c28f64e2a759e0630e54f9f16029e09279c613de73127996d
|
7
|
+
data.tar.gz: 11d7094e5cf6c4e0d7d53e4910fabff56222eea361e95af7a609ccf08e64753b8ed9e5c040d559631575e287a767ea3a23ce3384b8614d67dfbfc2194bb558ab
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module SheetReader
|
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
|
+
|
16
|
+
class MissingEnvVars < Error
|
17
|
+
def message
|
18
|
+
"Missing environment variables for the service account.\n" \
|
19
|
+
"Please provide #{REQUIRED_ENV_VARS.join(', ')}.\n" \
|
20
|
+
"See https://github.com/clearbit/sheet_reader for more info.\n"
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
data/lib/sheet_reader.rb
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
require 'google/apis/sheets_v4'
|
2
|
+
require 'googleauth'
|
3
|
+
require 'sheet_reader/errors'
|
4
|
+
|
5
|
+
module SheetReader
|
6
|
+
REQUIRED_ENV_VARS = %w[GOOGLE_CLIENT_EMAIL
|
7
|
+
GOOGLE_ACCOUNT_TYPE
|
8
|
+
GOOGLE_PRIVATE_KEY]
|
9
|
+
|
10
|
+
# Fetches the content of a google spreadsheet
|
11
|
+
#
|
12
|
+
# Example:
|
13
|
+
# >> SheetReader.read("1ukhJwquqRTgfX-G-nxV6AsAH726TOsKQpPJfpqNjWGg")
|
14
|
+
# => [{"foo"=>"hey", "bar"=>"ho"},
|
15
|
+
# {"foo"=>"let's ", "bar"=>"go"}]
|
16
|
+
#
|
17
|
+
# Arguments:
|
18
|
+
# sheet_id: (String) The google sheet identifier.
|
19
|
+
# sheet_name: (String) The sheet name, by default it's the first one
|
20
|
+
|
21
|
+
def self.read(sheet_id, sheet_name = "")
|
22
|
+
raise MissingEnvVars unless required_env_vars?
|
23
|
+
|
24
|
+
begin
|
25
|
+
sheets = Google::Apis::SheetsV4::SheetsService.new
|
26
|
+
scopes = ['https://www.googleapis.com/auth/spreadsheets.readonly']
|
27
|
+
sheets.authorization = Google::Auth.get_application_default(scopes)
|
28
|
+
raw_values = sheets.get_spreadsheet_values(sheet_id, "#{sheet_name}!A:ZZ").values
|
29
|
+
rescue Google::Apis::ClientError => e
|
30
|
+
raise BadSheetId if e.message =~ /notFound/
|
31
|
+
rescue
|
32
|
+
raise Error
|
33
|
+
end
|
34
|
+
|
35
|
+
rows_as_hashes(raw_values)
|
36
|
+
end
|
37
|
+
|
38
|
+
private
|
39
|
+
|
40
|
+
def self.rows_as_hashes(rows)
|
41
|
+
keys, *rest = rows
|
42
|
+
|
43
|
+
rest.map do |row|
|
44
|
+
Hash[keys.zip(convery_empty_cells_to_nil(row))]
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def self.convery_empty_cells_to_nil(row)
|
49
|
+
row.map do |cell|
|
50
|
+
if cell.strip == ""
|
51
|
+
nil
|
52
|
+
else
|
53
|
+
cell
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
def self.required_env_vars?
|
59
|
+
REQUIRED_ENV_VARS.all? do |e|
|
60
|
+
ENV.has_key?(e) &&
|
61
|
+
ENV.fetch(e) &&
|
62
|
+
ENV.fetch(e).strip != ""
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
data/lib/version.rb
ADDED
metadata
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sheet_reader
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.0.pre
|
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 google spreadsheets
|
42
|
+
email: daniel@clearbit.com
|
43
|
+
executables: []
|
44
|
+
extensions: []
|
45
|
+
extra_rdoc_files: []
|
46
|
+
files:
|
47
|
+
- lib/sheet_reader.rb
|
48
|
+
- lib/sheet_reader/errors.rb
|
49
|
+
- lib/version.rb
|
50
|
+
homepage: http://github.com/cleabit/sheet_reader
|
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: 1.3.1
|
68
|
+
requirements: []
|
69
|
+
rubyforge_project:
|
70
|
+
rubygems_version: 2.2.5
|
71
|
+
signing_key:
|
72
|
+
specification_version: 4
|
73
|
+
summary: Read google spreadsheets
|
74
|
+
test_files: []
|