middleman-google_drive 0.2.2 → 0.2.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +69 -10
- data/lib/google_drive.rb +1 -2
- data/lib/middleman-google_drive/extension.rb +29 -28
- data/lib/middleman-google_drive/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c005b79314b36975007d1318e3d641d97447b3b2
|
4
|
+
data.tar.gz: a5c1ba67e4685638dd57f619c06d3aaa7c562c5c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e4b75a88d830e8ba1f7605367da9427f2e5b2bdc2f754ba7c8b364a48e62774b9c4b1b4704e88bcb548dd5d409f3c54db903db87e44b36c52d0e9f3359f1c831
|
7
|
+
data.tar.gz: 376ab37406c05421ac306b2ea81980ad133101063fb5c43366f0ae61d0c948c43f1d90fdc9e251e3c17eab05ea695b8afcfea65d08ca4985948a6312d0f76ee7
|
data/README.md
CHANGED
@@ -21,19 +21,78 @@ Or install it yourself as:
|
|
21
21
|
|
22
22
|
The extension will get loaded in automatically, you'll just need to activate it.
|
23
23
|
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
24
|
+
```ruby
|
25
|
+
activate :google_drive, load_sheets: {
|
26
|
+
mysheet: google_spreadsheet_key,
|
27
|
+
moresheet: another_key
|
28
|
+
}
|
29
|
+
```
|
28
30
|
|
29
31
|
Then you can use the data in your templates:
|
30
32
|
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
33
|
+
```erb
|
34
|
+
<h1>My spreadsheet is called: <%= data.mysheet['title'] %></h1>
|
35
|
+
<% data.mysheet['Sheet1'].each do [row] %>
|
36
|
+
Column one header: <%= row['Column one header'] %>
|
37
|
+
Column two header: <%= row['Column two header'] %>
|
38
|
+
My column name: <%= row['My column name'] %>
|
39
|
+
<% end %>
|
40
|
+
```
|
41
|
+
|
42
|
+
You can also use a simplified Google Drive interface from inside your project, via the `drive`
|
43
|
+
variable.
|
44
|
+
|
45
|
+
```ruby
|
46
|
+
# get metadata for a document
|
47
|
+
doc_info = drive.find('google_document_key')
|
48
|
+
|
49
|
+
# get the same spreadsheet data without using the load_sheets param above
|
50
|
+
mysheet = drive.prepared_spreadsheet('google_document_key')
|
51
|
+
|
52
|
+
# get a fancy spreadsheet object
|
53
|
+
spreadsheet = drive.spreadsheet('google_document_key')
|
54
|
+
```
|
55
|
+
|
56
|
+
The fancy spreadsheet object comes from the [roo gem](https://github.com/Empact/roo). Unfortunately documentation is slim. Here is an example from the `roo` readme:
|
57
|
+
|
58
|
+
```ruby
|
59
|
+
spreadsheet.sheet('Info').row(1)
|
60
|
+
spreadsheet.sheet(0).row(1)
|
61
|
+
|
62
|
+
# use this to find the sheet with the most data to parse
|
63
|
+
|
64
|
+
spreadsheet.longest_sheet
|
65
|
+
|
66
|
+
# this file has multiple worksheets, let's iterate through each of them and process
|
67
|
+
|
68
|
+
spreadsheet.each_with_pagename do |name, sheet|
|
69
|
+
p sheet.row(1)
|
70
|
+
end
|
71
|
+
|
72
|
+
# pull out a hash of exclusive column data (get rid of useless columns and save memory)
|
73
|
+
|
74
|
+
spreadsheet.each(:id => 'UPC',:qty => 'ATS') {|hash| arr << hash}
|
75
|
+
#=> hash will appear like {:upc=>727880013358, :qty => 12}
|
76
|
+
|
77
|
+
# NOTE: .parse does the same as .each, except it returns an array (similar to each vs. map)
|
78
|
+
|
79
|
+
# not sure exactly what a column will be named? try a wildcard search with the character *
|
80
|
+
# regex characters are allowed ('^price\s')
|
81
|
+
# case insensitive
|
82
|
+
|
83
|
+
spreadsheet.parse(:id => 'UPC*SKU',:qty => 'ATS*\sATP\s*QTY$')
|
84
|
+
|
85
|
+
# if you need to locate the header row and assign the header names themselves,
|
86
|
+
# use the :header_search option
|
87
|
+
|
88
|
+
spreadsheet.parse(:header_search => ['UPC*SKU','ATS*\sATP\s*QTY$'])
|
89
|
+
#=> each element will appear in this fashion:
|
90
|
+
#=> {"UPC" => 123456789012, "STYLE" => "987B0", "COLOR" => "blue", "QTY" => 78}
|
91
|
+
|
92
|
+
# want to strip out annoying unicode characters and surrounding white space?
|
93
|
+
|
94
|
+
spreadsheet.parse(:clean => true)
|
95
|
+
```
|
37
96
|
|
38
97
|
## Setup
|
39
98
|
|
data/lib/google_drive.rb
CHANGED
@@ -5,32 +5,39 @@ module Middleman
|
|
5
5
|
module GoogleDrive
|
6
6
|
# Middle man extension that loads the google doc data
|
7
7
|
class Extension < Middleman::Extension
|
8
|
-
option :load_sheets, {}, '
|
8
|
+
option :load_sheets, {}, 'Key for a google spreadsheet or a hash of google spreadsheets to load. Hash value is the id or slug of the spreadsheet to load, hash key is the data attribute to load the sheet data into.'
|
9
9
|
|
10
10
|
def initialize(klass, options_hash = {}, &block)
|
11
11
|
super
|
12
12
|
|
13
|
-
drive = ::GoogleDrive.new
|
13
|
+
@drive = ::GoogleDrive.new
|
14
14
|
|
15
15
|
app = klass.inst
|
16
|
-
app.set :drive, drive # so you can access the drive api directly
|
17
|
-
options.load_sheets.
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
rescue ::GoogleDrive::GoogleDriveError => exc
|
28
|
-
if drive.server?
|
29
|
-
puts "== FAILED to load Google Doc \"#{exc.message}\""
|
30
|
-
break
|
31
|
-
end
|
16
|
+
app.set :drive, @drive # so you can access the drive api directly
|
17
|
+
if options.load_sheets.is_a? String
|
18
|
+
load_doc(options.load_sheets).each do |name, sheet|
|
19
|
+
app.data.store(name, sheet)
|
20
|
+
end
|
21
|
+
else
|
22
|
+
options.load_sheets.each do |name, key|
|
23
|
+
app.data.store(name, load_doc(key))
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
32
27
|
|
33
|
-
|
28
|
+
def load_doc(key)
|
29
|
+
data = @drive.prepared_spreadsheet(key)
|
30
|
+
doc = @drive.find(key)
|
31
|
+
puts <<-MSG
|
32
|
+
== Loaded data from Google Doc "#{doc['title']}"
|
33
|
+
== #{doc['alternateLink']}
|
34
|
+
MSG
|
35
|
+
data
|
36
|
+
rescue ::GoogleDrive::GoogleDriveError => exc
|
37
|
+
if @drive.server?
|
38
|
+
puts "== FAILED to load Google Doc \"#{exc.message}\""
|
39
|
+
else
|
40
|
+
puts <<-MSG
|
34
41
|
|
35
42
|
Could not load the Google Doc.
|
36
43
|
|
@@ -40,17 +47,11 @@ Things to check:
|
|
40
47
|
- Make sure you have access to the document
|
41
48
|
|
42
49
|
Google said "#{exc.message}." It might be a lie.
|
43
|
-
|
44
|
-
|
45
|
-
MSG
|
46
|
-
resp = $stdin.read 1
|
47
|
-
break unless resp.strip.empty? || resp =~ /[Yy]/
|
48
|
-
|
49
|
-
drive.redo_auth
|
50
|
-
end
|
51
|
-
end
|
50
|
+
MSG
|
51
|
+
@drive.clear_auth
|
52
52
|
end
|
53
53
|
end
|
54
|
+
|
54
55
|
end
|
55
56
|
end
|
56
57
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: middleman-google_drive
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ryan Mark
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2014-11-
|
12
|
+
date: 2014-11-20 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: middleman-core
|