Skip to content

Instantly share code, notes, and snippets.

@1syo
Last active August 29, 2015 14:17
Show Gist options
  • Save 1syo/b5b84d9eab89f53578c7 to your computer and use it in GitHub Desktop.
Save 1syo/b5b84d9eab89f53578c7 to your computer and use it in GitHub Desktop.
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