Created
March 27, 2018 10:33
-
-
Save SeyZ/2dfe97ff20aec371ea46aa6b0ce74b5a to your computer and use it in GitHub Desktop.
doc-lumber-smart-chart-time-based
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
// The value MUST be formatted like this. | |
{ | |
value: [{ | |
label: <string> , | |
values: { value: <number> } | |
}, { | |
label: <string> , | |
values: { value: <number> } | |
}, …] | |
} |
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
# /config/routes.rb | |
Rails.application.routes.draw do | |
# MUST be declared before the mount ForestLiana::Engine. | |
namespace :forest do | |
post '/stats/charges-per-day' => 'charts#charges_per_day' | |
end | |
mount ForestLiana::Engine => '/forest' | |
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
# /app/controllers/forest/charts_controller.rb | |
class Forest::ChartsController < ForestLiana::ApplicationController | |
def charges_per_day | |
values = [] | |
from = Date.parse('2018-03-01').to_time(:utc).to_i | |
to = Date.parse('2018-03-31').to_time(:utc).to_i | |
Stripe::Charge.list({ | |
created: { gte: from, lte: to }, | |
limit: 100 | |
}).each do |charge| | |
date = Time.at(charge.created).beginning_of_day.strftime("%d/%m/%Y") | |
entry = values.find { |e| e[:label] == date } | |
if !entry | |
values << { label: date, values: { value: 1 } } | |
else | |
++entry[:values][:value] | |
end | |
end | |
stat = ForestLiana::Model::Stat.new({ value: values }) | |
render json: serialize_model(stat) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment