Module: BrazilianUtils::AreaCodeUtils
- Defined in:
- lib/brazilian-utils/area-code-utils.rb
Overview
Utilities for looking up Brazilian area codes (DDD), sourced from the Anatel Plano Geral de Numeração.
Constant Summary collapse
- DATA_FILE =
File.join(File.dirname(__FILE__), 'data', 'area_codes.json')
Class Method Summary collapse
-
.get_info(area_code) ⇒ Hash?
Returns the state and region a DDD (area code) belongs to.
-
.list_by_state(state_code) ⇒ Array<Integer>
Returns every DDD that serves a state, sorted ascending.
Class Method Details
.get_info(area_code) ⇒ Hash?
Returns the state and region a DDD (area code) belongs to.
34 35 36 37 38 39 40 41 42 |
# File 'lib/brazilian-utils/area-code-utils.rb', line 34 def self.get_info(area_code) return nil if area_code.nil? str = area_code.to_s.strip return nil unless str.match?(/\A\d+\z/) row = load_data.find { |r| r['areaCode'] == str.to_i } row && to_symbolized(row) end |
.list_by_state(state_code) ⇒ Array<Integer>
Returns every DDD that serves a state, sorted ascending.
48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/brazilian-utils/area-code-utils.rb', line 48 def self.list_by_state(state_code) return [] unless state_code.is_a?(String) normalized = state_code.strip.upcase return [] if normalized.empty? load_data .select { |r| r['stateCodes'].include?(normalized) } .map { |r| r['areaCode'] } .sort end |