Created
April 28, 2014 21:19
-
-
Save soulcutter/11384382 to your computer and use it in GitHub Desktop.
Twilio Country Report
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
class TwilioCountryReport < TwilioReport | |
def generate_stats | |
@country_stats = HashWithIndifferentAccess.new { |h, country| h[country] = TwilioCountryStat.new(country) } | |
build_country_stats | |
country_stats | |
end | |
def country_stats | |
@country_stats.values | |
end | |
private | |
def build_country_stats | |
{ | |
past_year: 1.year.ago, | |
past_month: 1.month.ago, | |
past_week: 1.week.ago, | |
}.each do |description, time_period| | |
stats = HashWithIndifferentAccess.new(0) | |
stats.merge! by_time_period(time_period).group(:country).count(:country) | |
countries.each do |country| | |
@country_stats[country].fill_in(description, stats) | |
end | |
end | |
end | |
def countries | |
TwilioEvent::COUNTRIES | |
end | |
TwilioCountryStat = Struct.new(:name, :past_week, :past_month, :past_year) do | |
def fill_in(type, stats) | |
send("#{type}=", stats[name]) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment