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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2857e937b68c10de7780fa28220f3d70e6953f29
4
- data.tar.gz: a4c19ba124ac231e4795d86047d6c8a5aafd7786
3
+ metadata.gz: c005b79314b36975007d1318e3d641d97447b3b2
4
+ data.tar.gz: a5c1ba67e4685638dd57f619c06d3aaa7c562c5c
5
5
  SHA512:
6
- metadata.gz: 27ebca38276d76f625c781204b68fed14ed387a204d1c7a9543b58bb35d020548dde02cb6d04198e90bd37d5f2dc0292ae56b4fc92008d5098cc555d52d46509
7
- data.tar.gz: 4822b82da85d3ddc9e20d963170c5802267f54e81749acb185d0d1eb31bf8ccb5965200a3410b381e8acb4bc3cdd43884fca6fbbd1b9a048f297f34d5ec109c0
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
- activate :google_drive, load_sheets: {
25
- mysheet: google_spreadsheet_key,
26
- moresheet: another_key
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
- <h1>My spreadsheet is called: <%= data.mysheet['title'] %></h1>
32
- <% data.mysheet['Sheet1'].each do [row] %>
33
- Column one header: <%= row['Column one header'] %>
34
- Column two header: <%= row['Column two header'] %>
35
- My column name: <%= row['My column name'] %>
36
- <% end %>
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
@@ -151,9 +151,8 @@ class GoogleDrive
151
151
  end
152
152
  alias_method :copy_doc, :copy
153
153
 
154
- def redo_auth
154
+ def clear_auth
155
155
  File.delete @credentials if @key.nil?
156
- do_auth
157
156
  end
158
157
 
159
158
  def do_auth
@@ -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, {}, 'Hash of google spreadsheets to load. Hash value is the id or slug of the entry to load, hash key is the data attribute to load the sheet data into.'
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.each do |k, v|
18
- loop do
19
- begin
20
- app.data.store(k, drive.prepared_spreadsheet(v))
21
- doc = drive.find(v)
22
- puts <<-MSG
23
- == Loaded data.#{k} from Google Doc "#{doc['title']}"
24
- == #{doc['alternateLink']}
25
- MSG
26
- break
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
- puts <<-MSG
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
- Would you like to try again? [Y/n]
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
@@ -1,5 +1,5 @@
1
1
  module Middleman
2
2
  module GoogleDrive
3
- VERSION = '0.2.2'
3
+ VERSION = '0.2.3'
4
4
  end
5
5
  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.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-04 00:00:00.000000000 Z
12
+ date: 2014-11-20 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: middleman-core