Skip to content

Instantly share code, notes, and snippets.

@SeyZ
Created March 27, 2018 10:24
Show Gist options
  • Save SeyZ/7ee73aba6ea9ae922b53afaf6a3aa5c6 to your computer and use it in GitHub Desktop.
Save SeyZ/7ee73aba6ea9ae922b53afaf6a3aa5c6 to your computer and use it in GitHub Desktop.
doc-lumber-smart-chart-repartition
// The value MUST be formatted like this.
{
value: [{
key: <string> ,
value: <number>
}, {
key: <string> ,
value: <number>
}, …]
}
# /config/routes.rb
Rails.application.routes.draw do
# MUST be declared before the mount ForestLiana::Engine.
namespace :forest do
post '/stats/credit-card-country-repartition' => 'charts#credit_card_country_repartition'
end
mount ForestLiana::Engine => '/forest'
end
# /app/controllers/forest/charts_controller.rb
class Forest::ChartsController < ForestLiana::ApplicationController
def credit_card_country_repartition
repartition = []
from = Date.parse('2018-03-01').to_time(:utc).to_i
to = Date.parse('2018-03-20').to_time(:utc).to_i
Stripe::Charge.list({
created: { gte: from, lte: to },
limit: 100
}).each do |charge|
country = charge.source.country || 'Others'
entry = repartition.find { |e| e[:key] == country }
if !entry
repartition << { key: country, value: 1 }
else
++entry[:value]
end
end
stat = ForestLiana::Model::Stat.new({ value: repartition })
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