Last active
August 29, 2015 14:17
-
-
Save 1syo/b5b84d9eab89f53578c7 to your computer and use it in GitHub Desktop.
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
module KPI | |
module Summary | |
class SalesReport | |
def client | |
@client ||= Mysql2::Client.new( | |
host: Settings.kpi.database.host, | |
database: Settings.kpi.database.xxxxxxx, | |
username: config['username'], | |
password: config['password'] | |
) | |
end | |
def initialize(start_at: 1.days.ago.beginning_of_month, end_at: Time.now.beginning_of_day) | |
@start_at = start_at | |
@end_at = end_at | |
end | |
def start_at | |
client.escape(@start_at.strftime("%Y-%m-%d %T")) | |
end | |
def end_at | |
client.escape(@end_at.strftime("%Y-%m-%d %T")) | |
end | |
def collect | |
sql = <<-__SQL__ | |
select * | |
from (#中略...) salse_reports | |
where placed_at >= '#{start_at}' | |
and placed_at < '#{end_at}' | |
order by placed_at | |
__SQL__ | |
client.query(sql) | |
end | |
def rows | |
@rows ||= collect.map { |row| row.values } | |
end | |
def save | |
spreadsheet = KPI::GoogleDrive::Collection.new.spreadsheet(@start_at) | |
sheet = spreadsheet.worksheet(:sales_report, rows) | |
sheet.clear | |
sheet.save | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment