Module: I18nPhoneNumbers::Metadata::InstanceMethods

Included in:
I18nPhoneNumbers::Metadata
Defined in:
lib/i18n_phone_numbers/metadata.rb

Instance Method Summary collapse

Instance Method Details

#initialize(hash = {}) ⇒ Object



254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
# File 'lib/i18n_phone_numbers/metadata.rb', line 254

def initialize(hash = {})
  
  self.country_code = hash["countryCode"].to_i
  self.id = hash["id"]
  self.main_country_for_code = hash["mainCountryForCode"] == "true"
  self.international_prefix = hash["internationalPrefix"]
  self.leading_digits = hash["leadingDigits"]
  self.leading_zero_possible = hash["leadingZeroPossible"] == "true"
  self.national_prefix = hash["nationalPrefix"] || ''
  self.national_prefix_for_parsing = hash["nationalPrefixForParsing"] || self.national_prefix
  self.national_prefix_formatting_rule = (hash["nationalPrefixFormattingRule"] || '').
                                          sub("$NP", self.national_prefix).
                                          sub("$FG", "\\\\1")
  
  self.national_prefix_transform_rule = hash["nationalPrefixTransformRule"].blank? ? nil :
                                        hash["nationalPrefixTransformRule"].gsub("$", '\\') # regexp substitution in ruby uses '\1' references instead of '$1'
  
  self.preferred_international_prefix = hash["preferredInternationalPrefix"]
  
  
  self.general_desc = Hash === hash["generalDesc"] ? I18nPhoneNumbers::PhoneNumberDesc.new(hash["generalDesc"]) : nil
  
  self.fixed_line = Hash === hash["fixedLine"] ? I18nPhoneNumbers::PhoneNumberDesc.new(hash["fixedLine"]) : nil
  if !self.fixed_line.nil? && self.fixed_line.possible_number_pattern.blank? && !self.general_desc.nil?
    self.fixed_line.possible_number_pattern = self.general_desc.possible_number_pattern
  end
  
  self.mobile = Hash === hash["mobile"] ? I18nPhoneNumbers::PhoneNumberDesc.new(hash["mobile"]) : nil
  if !self.mobile.nil? && self.mobile.possible_number_pattern.blank? && !self.general_desc.nil?
    self.mobile.possible_number_pattern = self.general_desc.possible_number_pattern
  end
  
  if !self.mobile.nil? && !self.fixed_line.nil?
    self.same_mobile_and_fixed_line_pattern = (self.mobile.national_number_pattern == self.fixed_line.national_number_pattern)
  else
    self.same_mobile_and_fixed_line_pattern = false # TODO : we don't know ?!?
  end
  
  
  # populate with available formats
  self.number_formats = []
  self.intl_number_formats = []
  
  if Hash === hash["availableFormats"]
    
    if !hash["availableFormats"]["numberFormat"].blank?
      
      # we get either a string or and array of strings (damn you XML !)
      numberFormatList = hash["availableFormats"]["numberFormat"].class == Array ?
                              hash["availableFormats"]["numberFormat"] :
                              [hash["availableFormats"]["numberFormat"]]
      
      numberFormatList.each { |numberFormat|
        
        
        # national format
        nf = I18nPhoneNumbers::NumberFormat.new(numberFormat, self)
        
        self.number_formats << nf
        
        
        # international format
        next if numberFormat["intlFormat"] == "NA" # skip national-only rule
        
        numberFormat["format"] = numberFormat["intlFormat"] || numberFormat["format"]
        
        nf = I18nPhoneNumbers::NumberFormat.new(numberFormat, self)
        
        self.intl_number_formats << nf
        
      }
      
    end
    
  end
  
end

#national_prefix_for_parsingObject



332
333
334
335
336
# File 'lib/i18n_phone_numbers/metadata.rb', line 332

def national_prefix_for_parsing
  
  @national_prefix_for_parsing.nil? ? self.national_prefix : @national_prefix_for_parsing
  
end