class Ruboty::Brains::GoogleSpreadsheet

Public Class Methods

new() click to toggle source
Calls superclass method
# File lib/ruboty/brains/google_spreadsheet.rb, line 10
def initialize
  super

  @threads = []
  @threads << Thread.new { sync }
  @threads << Thread.new { reautorize }
  @threads.each { |thread| thread.abort_on_exception = true }

  @client = Ruboty::GoogleSpreadsheet::Client.new(
    client_id: ENV["GOOGLE_CLIENT_ID"],
    client_secret: ENV["GOOGLE_CLIENT_SECRET"],
    redirect_uri: ENV["GOOGLE_REDIRECT_URI"],
    refresh_token: ENV["GOOGLE_REFRESH_TOKEN"]
  )
  @client.authorize!
end

Public Instance Methods

data() click to toggle source
# File lib/ruboty/brains/google_spreadsheet.rb, line 27
def data
  @data ||= Ruboty::GoogleSpreadsheet::Spreadsheet.new(
    access_token: @client.access_token,
    spreadsheet_key: ENV["GOOGLE_SPREADSHEET_KEY"]
  )
end

Private Instance Methods

reauthorize() click to toggle source
# File lib/ruboty/brains/google_spreadsheet.rb, line 43
def reauthorize
  loop do
    # access token will expire in 3600 sec.
    sleep 3599
    @client.authorize!
    @data = nil
  end
end
sync() click to toggle source
# File lib/ruboty/brains/google_spreadsheet.rb, line 36
def sync
  loop do
    sleep 5
    data.synchronize
  end
end