lib/phonelib/core.rb in phonelib-0.1.3 vs lib/phonelib/core.rb in phonelib-0.2.0
- old
+ new
@@ -2,10 +2,14 @@
# main module that includes all basic data and methods
module Core
# variable will include hash with data for validation
@@phone_data = nil
+ # default country for parsing variable setting
+ mattr_accessor :default_country
+ @@default_country = nil
+
# gem constants definition
# constants for phone types
# Validation patterns keys constants
# General pattern for country key
@@ -32,14 +36,10 @@
VOICEMAIL = :voicemail
# Fixed line pattern key
FIXED_LINE = :fixedLine
# Mobile phone number pattern key
MOBILE = :mobile
- # Emergency phone number pattern key
- EMERGENCY = :emergency
- # Short code phone number pattern key
- SHORT_CODE = :shortCode
# In case MOBILE and FIXED patterns are the same, this type is returned
FIXED_OR_MOBILE = :fixedOrMobile
# Internal use keys for validations
# Valid regex pattern key
@@ -70,36 +70,31 @@
pager: 'Pager',
uan: 'UAN',
voicemail: 'VoiceMail',
fixedLine: 'Fixed Line',
mobile: 'Mobile',
- emergency: 'Emergency',
- shortCode: 'Short Code',
fixedOrMobile: 'Fixed Line or Mobile'
}
# array of types not included for validation check in cycle
- NOT_FOR_CHECK = [
- :generalDesc, :emergency, :shortCode, :fixedLine, :mobile, :fixedOrMobile
- ]
+ NOT_FOR_CHECK = [ :generalDesc, :fixedLine, :mobile, :fixedOrMobile ]
# method for parsing phone number.
# On first run fills @@phone_data with data present in yaml file
- def parse(phone, country = nil)
- require 'yaml'
- data_file = File.dirname(__FILE__) + '/../../data/phone_data.yml'
- @@phone_data ||= YAML.load_file(data_file)
+ def parse(phone, passed_country = nil)
+ load_data
+
+ country = country_or_default_country(passed_country)
if country.nil?
Phonelib::Phone.new(phone, @@phone_data)
else
- detected = @@phone_data.detect { |data| data[:id] == country }
- if !!detected
- phone = convert_phone_to_e164(phone,
- detected[:countryCode],
- detected[:nationalPrefix])
+ detected = detect_and_parse_by_country(phone, country)
+ if passed_country.nil? && @@default_country && detected.impossible?
+ Phonelib::Phone.new(phone, @@phone_data)
+ else
+ detected
end
- Phonelib::Phone.new(phone, [detected])
end
end
# method checks if passed phone number is valid
def valid?(phone_number)
@@ -132,11 +127,36 @@
country = country.to_s.upcase
parse(phone_number, country).invalid_for_country?(country)
end
private
+ # Load data file into memory
+ def load_data
+ require 'yaml'
+ data_file = File.dirname(__FILE__) + '/../../data/phone_data.yml'
+ @@phone_data ||= YAML.load_file(data_file)
+ end
+
+ # Get country that was provided or default country in needable format
+ def country_or_default_country(country)
+ country = country || @@default_country
+ country.to_s.upcase unless country.nil?
+ end
+
+ # Get Phone instance for provided phone with country specified
+ def detect_and_parse_by_country(phone, country)
+ detected = @@phone_data.detect { |data| data[:id] == country }
+ if !!detected
+ phone = convert_phone_to_e164(phone,
+ detected[:countryCode],
+ detected[:nationalPrefix])
+ end
+ Phonelib::Phone.new(phone, [detected])
+ end
+
+ # Create phone representation in e164 format
def convert_phone_to_e164(phone, prefix, national_prefix)
- return phone if phone.start_with?(prefix)
+ return phone if phone.gsub('+','').start_with?(prefix)
if !!national_prefix && phone.start_with?(national_prefix)
phone = phone[1..phone.length]
end
prefix + phone
end