Skip to content

Instantly share code, notes, and snippets.

@soulcutter
Created April 28, 2014 21:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save soulcutter/11384382 to your computer and use it in GitHub Desktop.
Save soulcutter/11384382 to your computer and use it in GitHub Desktop.
Twilio Country Report
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