jekyll-gdrive 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 719bbba214913cefb73468b8aa6542ea6b7b4202
4
- data.tar.gz: 82de8b6c9547bdc7d6904c1481da2c0eb1e379a8
3
+ metadata.gz: 208531d9b84e92237cd3af0bc03826e17e6b088f
4
+ data.tar.gz: ecd4d899db376f71c7a6ff418504ef1728d2c9c8
5
5
  SHA512:
6
- metadata.gz: ea474057a9e558379f6e6ada345ef0d363d58b977e031f49ff44857ec2d08a7cdf1ee6b7a5b6ca6ff7e705bd3299b822e0a284e6cbb56110ee3e3c268666877a
7
- data.tar.gz: c94eddbb04990a5b7edb090e7310df7c4f1d82785dc49edefc52317eb7fb833d7298c0a30c7a4f44f055adb505e4e9e3c33c48c69eedb7ad5a16cf3270d2669f
6
+ metadata.gz: 2070cc9782ace522aac70935e49402128c035c66e05551efaa97a0d0b1ce86044e652f15e8a5bc063c3288557cbf4ddf8b0ce6340640c17987f9ad6cf9d5fc54
7
+ data.tar.gz: 329a2314635d1de54c54d8e0166f08fa83eda7d1ab15d6e809ccb648da0dd253d1a54ab48688fa837e9d7b2fb81fb8845d561e591758004fb0ba3215e52a9e1e
data/README.md CHANGED
@@ -62,6 +62,22 @@ Example:
62
62
  </table>
63
63
  ```
64
64
 
65
+ ## Caching
66
+
67
+ Google's Drive API is not incredibly fast. When working in `jekyll serve --watch` mode you'll quickly get tired of waiting for the spreadsheet data to load on every refresh.
68
+
69
+ To work around this you can specify a cache period for the gdrive plugin in the Jekyll configuration:
70
+
71
+ ```yaml
72
+ gdrive:
73
+ sheet: "My sheet"
74
+ cache_period: 5 minutes
75
+ ```
76
+
77
+ This will store the sheet in a local `_gdrive_cache` file and only reload the sheet data once every 5 minutes.
78
+
79
+ You can specify the time in `seconds`, `minutes` or `hours`.
80
+
65
81
  ## Contributing
66
82
 
67
83
  1. Fork it ( https://github.com/[my-github-username]/jekyll-gdrive/fork )
@@ -2,12 +2,38 @@ module Jekyll
2
2
  module Gdrive
3
3
  class Generator < Jekyll::Generator
4
4
  def generate(site)
5
- sheet_name = site.config['gdrive'] && site.config['gdrive']['sheet']
6
- credentials = ENV['GDRIVE'] && ENV['GDRIVE'].split(":")
5
+ sheet_name = site.config['gdrive'] && site.config['gdrive']['sheet']
6
+ cache_period = site.config['gdrive'] && site.config['gdrive']['cache_period']
7
+ credentials = ENV['GDRIVE'] && ENV['GDRIVE'].split(":")
7
8
 
8
9
  raise "No sheet specified for the GDrive Data Plugin\nSet 'gdrive.sheet' in your '_config.yml'" unless sheet_name
9
10
  raise "No credentials specified for the GDrive Data Plugin\nSet it in a GRDIVE environment variable\nEg.: export GDRIVE_TOKEN=<client_id>:<client_secret>:<refresh_token>\nRun 'jekyll gdrive' to get an export statement you can cut and past" unless credentials
10
11
 
12
+ data = load_from_cache(cache_period)
13
+ unless data
14
+ sheet = load_from_sheet(sheet_name, credentials)
15
+
16
+ data = []
17
+
18
+ (0..sheet.num_rows).each do |row|
19
+ data[row] = []
20
+ (0..sheet.num_cols).each do |col|
21
+ data[row][col] = sheet[row+1, col+1]
22
+ end
23
+ end
24
+
25
+ # remove empty rows
26
+ while data.last.all? {|c| c == "" || c.nil? }
27
+ data.pop
28
+ end
29
+
30
+ store_in_cache(data) if cache_period
31
+ end
32
+
33
+ site.data['google_sheet'] = data
34
+ end
35
+
36
+ def load_from_sheet(sheet_name, credentials)
11
37
  client = Google::APIClient.new(
12
38
  :application_name => "Jekyll GDrive Plugin",
13
39
  :application_version => Jekyll::Gdrive::VERSION
@@ -19,25 +45,47 @@ module Jekyll
19
45
  auth.fetch_access_token!()
20
46
 
21
47
  session = GoogleDrive.login_with_oauth(auth.access_token)
48
+ session.file_by_title(sheet_name).worksheets.first
49
+ end
22
50
 
23
- sheet = session.file_by_title(sheet_name).worksheets.first
51
+ def store_in_cache(data)
52
+ File.open("_gdrive_cache", "w") do |file|
53
+ file.write YAML.dump({
54
+ "ts" => Time.now.to_i,
55
+ "data" => data
56
+ })
57
+ end
58
+ end
24
59
 
25
- data = []
60
+ def load_from_cache(cache_period)
61
+ cache_seconds = period_to_seconds(cache_period)
62
+ return nil unless cache_seconds
63
+ return nil unless cache_seconds > 0
26
64
 
27
- (0..sheet.num_rows).each do |row|
28
- data[row] = []
29
- (0..sheet.num_cols).each do |col|
30
- data[row][col] = sheet[row+1, col+1]
31
- end
32
- end
65
+ cache = YAML.load(File.read("_gdrive_cache")) rescue nil
66
+ return nil unless cache
67
+ return nil if Time.at(cache['ts'].to_i) + period_to_seconds(cache_period) < Time.now
33
68
 
34
- # remove empty rows
35
- while data.last.all? {|c| c == "" || c.nil? }
36
- data.pop
69
+ cache['data']
70
+ end
71
+
72
+ def period_to_seconds(period)
73
+ return nil unless period
74
+
75
+ _, value, unit = *period.match(/(\d+)\s*(s|second|seconds|m|minute|minutes|h|hour|hours)/)
76
+
77
+ return puts "Bad time period for GDrive cache '#{period}'" unless value && unit
78
+
79
+ multiplier = case unit
80
+ when "s", "second", "seconds"
81
+ 1
82
+ when "m", "minute", "minutes"
83
+ 60
84
+ when "h", "hour", "hours"
85
+ 60 * 60
37
86
  end
38
87
 
39
- puts "Site data for google sheet: #{data}"
40
- site.data['google_sheet'] = data
88
+ multiplier * value.to_i
41
89
  end
42
90
  end
43
91
  end
@@ -1,5 +1,5 @@
1
1
  module Jekyll
2
2
  module Gdrive
3
- VERSION = "0.2.0"
3
+ VERSION = "0.3.0"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jekyll-gdrive
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mathias Biilmann Christensen
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-12-12 00:00:00.000000000 Z
11
+ date: 2014-12-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler