Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Log size of record for each model to Google SpreadSheet
- Loading branch information
Showing
5 changed files
with
70 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
require "oauth" | ||
require "google_spreadsheet" | ||
|
||
class LogGss | ||
|
||
def self.update(arr) | ||
self.new.update(arr) | ||
end | ||
|
||
def insert_row | ||
@insert_row ||= worksheet.num_rows + 1 | ||
end | ||
|
||
def update(arr) | ||
arr.unshift(Time.now.to_s(:db)) | ||
arr.each_with_index do |item, index| | ||
worksheet[insert_row, index+1] = item | ||
end | ||
worksheet.save | ||
end | ||
|
||
def worksheet | ||
@worksheet ||= sheet.worksheets.first | ||
end | ||
|
||
def sheet | ||
@sheet ||= session.spreadsheet_by_key ENV["GOOGLE_LOG_SHEET_KEY"] | ||
end | ||
|
||
def session | ||
@google_session ||= GoogleSpreadsheet.login_with_oauth access_token | ||
end | ||
|
||
def consumer | ||
return @consumer if @consumer | ||
key = ENV["GOOGLE_CONSUMER_KEY"] | ||
secret = ENV["GOOGLE_CONSUMER_SECRET"] | ||
@consumer = OAuth::Consumer.new key, secret, { | ||
:signature_method => 'HMAC-SHA1', | ||
:site => 'https://www.google.com', | ||
:request_token_path => '/accounts/OAuthGetRequestToken', | ||
:authorize_path => '/accounts/OAuthAuthorizeToken', | ||
:access_token_path => '/accounts/OAuthGetAccessToken', | ||
} | ||
end | ||
|
||
def access_token | ||
return @access_token if @access_token | ||
token = ENV["GOOGLE_ACCESS_TOKEN"] | ||
secret = ENV["GOOGLE_ACCESS_SECRET"] | ||
@access_token = OAuth::AccessToken.new(consumer, token, secret) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
require "lib/log_gss" | ||
|
||
desc "This task is called by the Heroku cron add-on" | ||
task :cron => :environment do | ||
# log size of record for each model to Google SpreadSheet | ||
opt = [User.count, | ||
Manga.count] | ||
LogGss.update(opt) | ||
end |