Class: Plugin::GoogleSheet
- Inherits:
-
LanGrove::Plugin::BufferedPersistor
- Object
- LanGrove::Plugin::BufferedPersistor
- Plugin::GoogleSheet
- Defined in:
- lib/plugin/discovery.rb,
lib/plugin/google_sheet.rb
Overview
Impractical for high frequency single row updates
Can ony reference cells by column and row number
Will need a key column to allow for sorting (to not break updates expecting certain records in certain rows)
Not ENTIRELY compatable as a LanGrove Persistor…
-
cannot update one row without fetching the entire sheet
unless perhaps:
-
streamline connecting
-
initializer to fetch whole sheet
-
expensive to update only one row.…!!!!
-
will need to batch (somehow – no such thing in persistor atm)
Instance Method Summary collapse
- #add_row(sheet, row) ⇒ Object
- #connect ⇒ Object
- #create_new(sheet) ⇒ Object
- #get_sheet ⇒ Object
- #index(sheet) ⇒ Object
-
#initialize(username, password, key) ⇒ GoogleSheet
constructor
A new instance of GoogleSheet.
- #play_more ⇒ Object
- #play_with_it ⇒ Object
- #store_all ⇒ Object
- #update(sheet) ⇒ Object
- #update_row(sheet, row_number, row) ⇒ Object
- #validate_config ⇒ Object
Constructor Details
#initialize(username, password, key) ⇒ GoogleSheet
Returns a new instance of GoogleSheet.
37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/plugin/discovery.rb', line 37 def initialize( username, password, key ) @username = username @password = password @key = key puts "loging in..." @session = GoogleDrive.login( username, password ) puts "getting sheet..." @spreadsheet = @session.spreadsheet_by_key( @key ) end |
Instance Method Details
#add_row(sheet, row) ⇒ Object
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/plugin/google_sheet.rb', line 134 def add_row( sheet, row ) # # increment and offset for heading # next_row = (@row_count += 1) @logger.debug "Creating row:#{next_row} with data: #{row.inspect}" row.each do |k, value| sheet[ next_row, @col_index[k.to_s] ] = value end end |
#connect ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/plugin/google_sheet.rb', line 31 def connect # # TODO: restore session (a cookie or something) # @logger.debug "Connecting as user:#{@username}" @session = GoogleDrive.login( @username, @password ) @logger.debug "Getting document: #{@google_key}" @document = @session.spreadsheet_by_key( @google_key ) @document.title = @database end |
#create_new(sheet) ⇒ Object
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/plugin/google_sheet.rb', line 164 def create_new( sheet ) row_number = 0 @capsules.each do |key, row| row_number += 1 if row_number == 1 col_number = 0 row.each_key do |heading| sheet[ row_number, col_number += 1 ] = heading end row_number += 1 end col_number = 0 row.each do |k, value| sheet[ row_number, col_number += 1 ] = value end end return sheet end |
#get_sheet ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/plugin/google_sheet.rb', line 60 def get_sheet @logger.debug "Getting sheet #{@table}" sheet = @document.worksheet_by_title( @table ) if sheet.nil? @logger.debug "Creating sheet #{@table}" sheet = @document.add_worksheet( @table ) return true, create_new( sheet ) end index sheet return false, sheet end |
#index(sheet) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/plugin/google_sheet.rb', line 82 def index( sheet ) @logger.debug "Indexing for key: #{@key_column}" @sheet_hash_array = sheet.list.to_hash_array @row_index = {} @row_count = 1 # first row for headings @sheet_hash_array.each do |row| @row_index[ row[ @key_column.to_s ] ] = @row_count += 1 end @col_index = {} @col_count = 0 @sheet_hash_array[0].each_key do |key| @col_index[ key ] = @col_count += 1 end end |
#play_more ⇒ Object
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 |
# File 'lib/plugin/discovery.rb', line 52 def play_more puts "getting worksheet..." sheet = @spreadsheet.worksheet_by_title('Sheet One') puts "getting list..." list = sheet.list puts "to_hash_array..." ap list.to_hash_array # # [ # [0] { # "key column" => "key1", # "value 1" => "2", # "value 2" => "3" # }, # [1] { # "key column" => "key2", # "value 1" => "5", # "value 2" => "6" # } # ] # #puts "get List::Row..." #row = list[ 98 ] # 100th row #ap row.to_hash end |
#play_with_it ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/plugin/discovery.rb', line 86 def play_with_it puts "getting worksheet..." sheet = @spreadsheet.worksheet_by_title('Sheet One') puts "updating rows..." # # Write into call A1 # sheet[1,1] = 'key column' sheet[1,2] = 'value 1' sheet[1,3] = 'value 2' sheet[2,1] = 'key1' sheet[2,2] = '2' sheet[2,3] = '3' sheet[3,1] = 'key2' sheet[3,2] = '5' sheet[3,3] = '6' sheet[100,1] = 'key3' sheet[100,2] = '8' sheet[100,3] = '9' puts 'saving...' sheet.save puts 'done...' end |
#store_all ⇒ Object
50 51 52 53 54 55 56 57 58 |
# File 'lib/plugin/google_sheet.rb', line 50 def store_all new, sheet = get_sheet update( sheet ) unless new sheet.save end |
#update(sheet) ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/plugin/google_sheet.rb', line 109 def update( sheet ) @capsules.each do |key, row| if @row_index.has_key?( key ) # # Updates by row number (as indexed) # # Sorting the spreadsheet while this is in # progress will have perculiar effects # update_row( sheet, @row_index[key], row ) else add_row( sheet, row ) end end end |
#update_row(sheet, row_number, row) ⇒ Object
152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/plugin/google_sheet.rb', line 152 def update_row( sheet, row_number, row ) @logger.debug "Updating row:#{row_number} with data: #{row.inspect}" row.each do |k, value| sheet[ row_number, @col_index[k.to_s] ] = value end end |
#validate_config ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/plugin/google_sheet.rb', line 7 def validate_config super raise LanGrove::DaemonConfigException.new( "#{self.class} requires :username:, :password: and :google_key:" ) if ( @config[:username].nil? or @config[:password].nil? or @config[:google_key].nil? ) @username = @config[:username] @password = @config[:password] @google_key = @config[:google_key] connect end |