Sha256: d3fed46e71b4323e0636757bbd25f3ba19abaea3d393da1a89e1ac6c2240557f

Contents?: true

Size: 1.21 KB

Versions: 1

Compression:

Stored size: 1.21 KB

Contents

require 'google_spreadsheet'

module GSMetrics
  class Session
    def initialize email, password
      @email = email
      @password = password
    end

    def worksheet doc_title, worksheet_id
      Worksheet.new doc_title, worksheet_id, GoogleSpreadsheet.login(@email, @password)
    end
  end

private
  class Worksheet
    attr_accessor :check_worksheet_size
    def initialize doc_title, worksheet_title, session
      @worksheet = session.spreadsheet_by_title(doc_title).worksheet_by_title(worksheet_title)
      @items = []
      @rows = []
    end

    def append item
      @items << (item.nil? ? "" : item)
    end

    def << item
      append item
    end

    def next_row
      @rows << @items unless @items.empty?
      @items = []
    end

    def save row_id = nil
      next_row
      row_id ||= @worksheet.num_rows + 1

      @worksheet.max_rows = row_id + @rows.size

      @rows.each_with_index do |items, row_index|
        items.each_with_index do |item, item_index|
          @worksheet[row_id + row_index, item_index + 1] = item
        end
      end
      put "Save failed" unless @worksheet.save
      @items = []
    end

    def set_worksheet_size size
      @worksheet.max_rows = size
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
gsmetrics-0.1.0 lib/gsmetrics/gsmetrics.rb