Class: Middleman::GoogleDrive::Extension
- Inherits:
-
Extension
- Object
- Extension
- Middleman::GoogleDrive::Extension
- Defined in:
- lib/middleman-google_drive/extension.rb
Overview
Middle man extension that loads the google doc data
Instance Method Summary collapse
- #data_path(basename) ⇒ Object
- #handle_option(option, type) ⇒ Object
-
#initialize(klass, options_hash = {}, &block) ⇒ Extension
constructor
A new instance of Extension.
- #load_doc(key, type) ⇒ Object
- #load_local_copy(filename) ⇒ Object
- #store_data(key, data) ⇒ Object
Constructor Details
#initialize(klass, options_hash = {}, &block) ⇒ Extension
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/middleman-google_drive/extension.rb', line 14 def initialize(klass, = {}, &block) super @drive = ::GoogleDrive.new @app = klass.inst @cache_dir = File.join(@app.root, 'google_drive_cache') Dir.mkdir(@cache_dir) unless Dir.exist?(@cache_dir) handle_option(.load_sheets, :xlsx) handle_option(.load_docs, :txt) handle_option(.load_docs_html, :html) handle_option(.load_docs_archieml, :archieml) end |
Instance Method Details
#data_path(basename) ⇒ Object
117 118 119 |
# File 'lib/middleman-google_drive/extension.rb', line 117 def data_path(basename) File.join(@cache_dir, basename) end |
#handle_option(option, type) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/middleman-google_drive/extension.rb', line 31 def handle_option(option, type) if option.is_a? Hash option.each do |name, key| store_data(name, load_doc(key.to_s, type)) end elsif type == :xlsx load_doc(option.to_s, type).each do |name, sheet| store_data(name, sheet) end else store_data('doc', load_doc(option.to_s, type)) end end |
#load_doc(key, type) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/middleman-google_drive/extension.rb', line 45 def load_doc(key, type) doc = @drive.find(key) puts "== Loading data from Google Doc \"\#{doc['title']}\"\n== \#{doc['alternateLink']}\n MSG\n filename = data_path(\"\#{key}.\#{type}\")\n\n case type.to_sym\n when :xlsx\n if @drive.server?\n filename = @drive.export_to_file(key, :xlsx)\n else\n @drive.export_to_file(key, :xlsx, filename)\n end\n ret = @drive.prepare_spreadsheet(filename)\n File.unlink(filename) if @drive.server?\n when :archieml\n if @drive.server?\n ret = Archieml.load(@drive.export(key, :txt))\n else\n @drive.export_to_file(key, :txt, filename)\n ret = Archieml.load(File.read(filename))\n end\n else\n if @drive.server?\n ret = @drive.export(key, type)\n else\n @drive.export_to_file(key, type, filename)\n ret = File.read(filename)\n end\n end\n return ret\nrescue ::Faraday::ConnectionFailed => exc\n puts \"== FAILED to load Google Doc \\\"\#{exc.message}\\\"\"\n return load_local_copy(filename)\nrescue ::GoogleDrive::GoogleDriveError => exc\n puts \"== FAILED to load Google Doc \\\"\#{exc.message}\\\"\"\n unless @drive.server?\n puts <<-MSG\n\nCould not load the Google Doc.\n\nThings to check:\n- Make sure the Google Doc key is correct\n- Make sure you're logging in with the correct account\n- Make sure you have access to the document\n\n MSG\n @drive.clear_auth\n end\n return load_local_copy(filename)\nend\n" |
#load_local_copy(filename) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/middleman-google_drive/extension.rb', line 99 def load_local_copy(filename) if File.exist? filename puts '== Loading Google Doc from local cache' type = File.extname(filename).gsub('.','').to_sym case type when :xlsx return @drive.prepare_spreadsheet(filename) when :archieml return Archieml.load(File.read(filename)) else return File.read(filename) end else puts '== No local copy of Google Doc' return nil end end |
#store_data(key, data) ⇒ Object
121 122 123 |
# File 'lib/middleman-google_drive/extension.rb', line 121 def store_data(key, data) @app.data.store(key, data) end |