module AbsPopulation
Constants
- DATA_PATH
- STYLE_TITLE
Public Instance Methods
Source
# File lib/abs_population.rb, line 13 def cities @cities ||= JSON.parse(File.read(DATA_PATH)) end
Source
# File lib/abs_population.rb, line 21 def find_city(query) key = query.to_s.strip.downcase.gsub(/[^a-z]/, "") return nil if key.empty? cities[key] || cities.values.find do |row| row["name"].to_s.downcase.include?(key) || key.include?(row["name"].to_s.downcase.gsub(/[^a-z]/, "")) end end
Source
# File lib/abs_population.rb, line 50 def format_number(value) value.to_i.to_s.reverse.gsub(/(\d{3})(?=\d)/, '\\1,').reverse end
Source
# File lib/abs_population.rb, line 35 def format_population(query, fetch: nil) row = find_city(query) lines = [ IrcFormat.report_header(tag: "ABS", title: "Population ยท #{query}", style: STYLE_TITLE) ] if row.nil? lines << "City not found โ try Perth, Sydney, Melbourne, Brisbane, Gold Coast, Newcastle" else pop = population_for(row, fetch: fetch) lines << "#{row['name']} (#{row['state']}) โ #{format_number(pop)} people (ERP)" end lines << IrcFormat.report_footer("estimated resident population", "Australian Bureau of Statistics") lines.join("\n") end
Source
# File lib/abs_population.rb, line 30 def population_for(row, fetch: nil) live = AbsApi.fetch_population(row, fetch: fetch) live&.positive? ? live : row["population"].to_i end