google_sheet_reader 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5fddd75645c1a3b2ab59543f2b803bb176597392
4
+ data.tar.gz: 723e426884952c3d1aa67b7177dd3f9bfe1cf847
5
+ SHA512:
6
+ metadata.gz: 0416b7d595761e4c5236ad68889cce2ea4f7ba48b7bfb1c9fac152424e6d2f25dbb4e53f69ec522d0b017c2c8f6d9b6b0d27c3b4103faa7a186d770aa69a01b7
7
+ data.tar.gz: 09da21de9750c5ee53b31b6705a4e0dc55ed9a3c9f33ff5aee13bb73e1876bf0c494b428eb44a742810bb21faa7c8deaeb7b9f6bcac9e5c0792ef41a19a23be0
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.0
4
+ before_install: gem install bundler -v 1.10.5
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in google_sheet_reader.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 MJ Rossetti (@s2t2)
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,83 @@
1
+ # GoogleSheetReader
2
+
3
+ A ruby library
4
+ for extracting spreadsheet data from [Google Drive](https://www.google.com/drive/).
5
+
6
+ Performs a custom ETL procedure on each row.
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'google_sheet_reader'
14
+ ```
15
+
16
+ Or install yourself with `gem install google_sheet_reader`.
17
+
18
+ ## Usage
19
+
20
+ ```` rb
21
+ spreadsheet_id = "1a2B3C45_x0G44lk15Ff_M33ps"
22
+ # see Prerequisites section below for instructions on how to obtain most of these configuration option values ...
23
+ configuration_options = {
24
+ :app_name => "my-app",
25
+ :email_address => "abc-123@developer.gserviceaccount.com",
26
+ :key_file_path => File.expand_path("../key_file/my-app-abc123xy456z.p12", __FILE__),
27
+ :extraction_procedure => Proc.new do |row|
28
+ "PARSING A SPREADSHEET ROW HERE -- #{row.inspect}"
29
+ end
30
+ }
31
+
32
+ GoogleSheetReader.extract(spreadsheet_id, configuration_options)
33
+ ````
34
+
35
+ ### Prerequisites
36
+
37
+ #### Drive API-enabled App
38
+
39
+ Visit the [google developer console](https://console.developers.google.com), create a new application, and note its name.
40
+
41
+ Navigate to the **APIs & Auth > APIs** menu and enable the *Drive API* by searching for it.
42
+
43
+ #### Service Account
44
+
45
+ Navigate to the **APIs & Auth > Credentials** menu and add credentials for a service account.
46
+
47
+ Choose the .p12 key file option, download it, optionally move it somewhere else, and finally note its file path.
48
+
49
+ Back in the browser, also note the service account's email address.
50
+
51
+ #### Spreadsheet
52
+
53
+ Create a spreadsheet, and inspect its url to find its document identifier: *docs.google.com/spreadsheets/d/`spreadsheet_id`/edit*.
54
+
55
+ Share the spreadsheet with the service account's email address in a "can edit" role.
56
+
57
+ ## Contributing
58
+
59
+ Browse existing [issues](https://github.com/data-creative/google-sheet-reader-ruby/issues) or create a new issue to communicate bugs, desired features, etc.
60
+
61
+ After forking the repo and pushing your changes, create a pull request referencing the applicable issue(s).
62
+
63
+ ### Developing
64
+
65
+ After checking out the repo, run `bin/setup` to install dependencies.
66
+
67
+ ### Testing
68
+
69
+ Store a real service account .p12 key file in the **spec/key_file/** directory, and add the following environment variables to your .bash_profile:
70
+
71
+ + `GOOGLE_SHEET_READER_APP_NAME`
72
+ + `GOOGLE_SHEET_READER_EMAIL`
73
+ + `GOOGLE_SHEET_READER_KEY_FILE_NAME`
74
+
75
+ Run `rake rspec` or `bundle exec rspec spec/` to run the tests.
76
+
77
+ Optionally run `bin/console` for an interactive prompt that will allow you to experiment.
78
+
79
+ ### Releasing
80
+
81
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
82
+
83
+ ## [License](LICENSE.txt)
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "google_sheet_reader"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ require "pry"
11
+ Pry.start
12
+
13
+ # require "irb"
14
+ # IRB.start
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'google_sheet_reader/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "google_sheet_reader"
8
+ spec.version = GoogleSheetReader::VERSION
9
+ spec.authors = ["MJ Rossetti (@s2t2)"]
10
+ spec.email = ["s2t2mail+git@gmail.com"]
11
+
12
+ spec.summary = %q{A ruby library for extracting spreadsheet data from Google Drive.}
13
+ spec.description = %q{A ruby library for extracting spreadsheet data from Google Drive. Performs a custom ETL procedure on each row.}
14
+ spec.homepage = "https://github.com/data-creative/google-sheet-reader-ruby"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.10"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "rspec"
25
+ spec.add_development_dependency "pry", "~> 0.10"
26
+ spec.add_development_dependency "yard"
27
+ spec.add_dependency 'google-api-client'
28
+ end
@@ -0,0 +1,72 @@
1
+ require 'google/api_client'
2
+ require 'csv'
3
+
4
+ require "google_sheet_reader/version"
5
+
6
+ module GoogleSheetReader
7
+
8
+ def self.client(options = {})
9
+ app_name = options[:app_name]
10
+ email_address = options[:email_address]
11
+ key_file_path = options[:key_file_path]
12
+
13
+ key = Google::APIClient::KeyUtils.load_from_pkcs12(key_file_path, 'notasecret')
14
+ asserter = Google::APIClient::JWTAsserter.new(email_address, 'https://www.googleapis.com/auth/drive', key)
15
+ google_api_client = Google::APIClient.new({:application_name => app_name, :application_version => "1.0"})
16
+ google_api_client.authorization = asserter.authorize()
17
+ return google_api_client
18
+ end
19
+
20
+ # Extract data from a Google Drive spreadsheet.
21
+ #
22
+ # @param [String] spreadsheet_id
23
+ # @param [Hash] options [String] app_name
24
+ # @param [Hash] options [String] email_address
25
+ # @param [Hash] options [String] key_file_path
26
+ # @param [Hash] options [Proc] extraction_procedure
27
+ #
28
+ # @example GoogleSheetReader.extract(file_id, opts)
29
+ #
30
+ def self.extract(spreadsheet_id, options = {})
31
+ app_name = options[:app_name]
32
+ email_address = options[:email_address]
33
+ key_file_path = options[:key_file_path]
34
+ extraction_procedure = options[:extraction_procedure]
35
+
36
+ puts "CONNECTING TO GOOGLE DRIVE ..."
37
+
38
+ google_api_client = self.client(options)
39
+ drive = google_api_client.discovered_api('drive', 'v2')
40
+
41
+ get_request_result = google_api_client.execute(
42
+ :api_method => drive.files.get,
43
+ :parameters => { 'fileId' => spreadsheet_id }
44
+ )
45
+ raise FileRetrievalError unless get_request_result.status == 200
46
+
47
+ file = get_request_result.data
48
+ download_url = file.exportLinks["text/csv"]
49
+ raise FileDownloadLinkError unless download_url
50
+
51
+ download_request_result = google_api_client.execute(:uri => download_url)
52
+ raise FileDownloadError unless download_request_result.status == 200
53
+
54
+ file_contents = download_request_result.body
55
+ csv_result = CSV.parse(file_contents, headers:true)
56
+ #headers = csv_result.headers
57
+
58
+ raise EmptyFileError.new("This spreadsheet has no data. Please add rows.") unless csv_result.any?
59
+
60
+ csv_result.each do |row|
61
+ extraction_procedure.call(row)
62
+ end
63
+
64
+ puts "PARSED #{csv_result.count} ROWS"
65
+ return {:status => "SUCCESS", :row_count => csv_result.count}
66
+ end
67
+
68
+ class FileRetrievalError < StandardError ; end
69
+ class FileDownloadLinkError < StandardError ; end
70
+ class FileDownloadError < StandardError ; end
71
+ class EmptyFileError < StandardError ; end
72
+ end
@@ -0,0 +1,3 @@
1
+ module GoogleSheetReader
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,36 @@
1
+ #<Google::APIClient::Schema::Drive::V2::File:0x3fc9526f4edc DATA:
2
+ {
3
+ "kind"=>"drive#file",
4
+ "id"=>"xyz_123",
5
+ "etag"=>"\"abcd\"",
6
+ "selfLink"=>"https://www.googleapis.com/drive/v2/files/xyz_123",
7
+ "alternateLink"=>"https://docs.google.com/spreadsheets/d/xyz_123/edit?usp=drivesdk",
8
+ "embedLink"=>"https://docs.google.com/spreadsheets/d/xyz_123/htmlembed",
9
+ "iconLink"=>"https://ssl.gstatic.com/docs/doclist/images/icon_11_spreadsheet_list.png",
10
+ "thumbnailLink"=>"https://docs.google.com/feeds/vt?gd=true&id=xyz_123&v=0&s=ggggg",
11
+ "title"=>"my-sheet",
12
+ "mimeType"=>"application/vnd.google-apps.spreadsheet",
13
+ "labels"=>{"starred"=>false, "hidden"=>false, "trashed"=>false, "restricted"=>false, "viewed"=>false}, "createdDate"=>"2015-09-18T23:40:45.972Z",
14
+ "modifiedDate"=>"2015-09-19T02:01:13.137Z",
15
+ "markedViewedByMeDate"=>"1970-01-01T00:00:00.000Z",
16
+ "sharedWithMeDate"=>"2015-09-19T02:01:13.144Z",
17
+ "version"=>"98647",
18
+ "sharingUser"=>{"kind"=>"drive#user",
19
+ "displayName"=>"Jim Jones",
20
+ "picture"=>{"url"=>"https://lh6.googleusercontent.com/photo.jpg"}, "isAuthenticatedUser"=>false, "permissionId"=>"1234567890987654321",
21
+ "emailAddress"=>"jim.Jones@gmail.com"}, "parents"=>[],"exportLinks"=>{"text/csv"=>"https://docs.google.com/spreadsheets/export?id=xyz_123&exportFormat=csv",
22
+ "application/pdf"=>"https://docs.google.com/spreadsheets/export?id=xyz_123&exportFormat=pdf",
23
+ "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"=>"https://docs.google.com/spreadsheets/export?id=xyz_123&exportFormat=xlsx"}, "userPermission"=>{"kind"=>"drive#permission",
24
+ "etag"=>"\"tagggggggggg3434738473\"",
25
+ "id"=>"me",
26
+ "selfLink"=>"https://www.googleapis.com/drive/v2/files/xyz_123/permissions/me",
27
+ "role"=>"writer",
28
+ "type"=>"user"}, "quotaBytesUsed"=>"0",
29
+ "ownerNames"=>["Jim Jones"], "owners"=>[{"kind"=>"drive#user",
30
+ "displayName"=>"Jim Jones",
31
+ "picture"=>{"url"=>"https://lh6.googleusercontent.com/photo.jpg"}, "isAuthenticatedUser"=>false, "permissionId"=>"1234567890987654321",
32
+ "emailAddress"=>"jim.Jones@gmail.com"}], "lastModifyingUserName"=>"Jim Jones",
33
+ "lastModifyingUser"=>{"kind"=>"drive#user",
34
+ "displayName"=>"Jim Jones",
35
+ "picture"=>{"url"=>"https://lh6.googleusercontent.com/photo.jpg"}, "isAuthenticatedUser"=>false, "permissionId"=>"1234567890987654321",
36
+ "emailAddress"=>"jim.Jones@gmail.com"}, "editable"=>true, "copyable"=>true, "writersCanShare"=>true, "shared"=>true, "explicitlyTrashed"=>false, "appDataContents"=>false, "spaces"=>["drive"]} #>
metadata ADDED
@@ -0,0 +1,143 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: google_sheet_reader
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - MJ Rossetti (@s2t2)
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-09-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.10'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.10'
69
+ - !ruby/object:Gem::Dependency
70
+ name: yard
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: google-api-client
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: A ruby library for extracting spreadsheet data from Google Drive. Performs
98
+ a custom ETL procedure on each row.
99
+ email:
100
+ - s2t2mail+git@gmail.com
101
+ executables: []
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - ".gitignore"
106
+ - ".rspec"
107
+ - ".travis.yml"
108
+ - Gemfile
109
+ - LICENSE.txt
110
+ - README.md
111
+ - Rakefile
112
+ - bin/console
113
+ - bin/setup
114
+ - google_sheet_reader.gemspec
115
+ - lib/google_sheet_reader.rb
116
+ - lib/google_sheet_reader/version.rb
117
+ - mocks/spreadsheet_file.rb
118
+ homepage: https://github.com/data-creative/google-sheet-reader-ruby
119
+ licenses:
120
+ - MIT
121
+ metadata: {}
122
+ post_install_message:
123
+ rdoc_options: []
124
+ require_paths:
125
+ - lib
126
+ required_ruby_version: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - ">="
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ required_rubygems_version: !ruby/object:Gem::Requirement
132
+ requirements:
133
+ - - ">="
134
+ - !ruby/object:Gem::Version
135
+ version: '0'
136
+ requirements: []
137
+ rubyforge_project:
138
+ rubygems_version: 2.4.5
139
+ signing_key:
140
+ specification_version: 4
141
+ summary: A ruby library for extracting spreadsheet data from Google Drive.
142
+ test_files: []
143
+ has_rdoc: