Last active
August 20, 2020 09:11
-
-
Save absk1317/051acd8d68f687772b03c47b95c117d4 to your computer and use it in GitHub Desktop.
Google Sheets Entries
This file contains hidden or 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
# frozen_string_literal: true | |
# For abstracting out updating approved users sheet | |
class GoogleSheets::Users | |
SHEET_NAME = 'users' | |
QUEUE_NAME = 'google_sheet' | |
include Sidekiq::Worker | |
sidekiq_options queue: QUEUE_NAME | |
sidekiq_options lock: :until_and_while_executing | |
attr_accessor :user | |
def perform(user_id) | |
# trigger only for production | |
return unless Rails.env.production? | |
@user = User.find(user_id) | |
Google::Apis.logger = Logger.new('/dev/null') # throws Huge logs on stdout | |
add_new_entry | |
worksheet.save | |
end | |
private | |
def add_new_entry | |
(1..worksheet.num_rows).each do |row| | |
if worksheet[row, 1] == login | |
@existing_entry = true | |
break | |
end | |
end | |
return if @existing_entry | |
worksheet.insert_rows(worksheet.num_rows + 1, [ | |
[ | |
login, | |
user.created_at, | |
user.address&.city_name, | |
user.profile_type, | |
user.name, | |
user.approved_at, | |
user.posts.count, | |
# ... more fields | |
] | |
]) | |
end | |
def worksheet | |
@worksheet ||= session.spreadsheet_by_title(SHEET_NAME)&.worksheets&.first | |
end | |
def session | |
@session ||= GoogleDrive::Session.from_config('config/google_drive.json') | |
end | |
def login | |
@login ||= user.login.to_s | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment