Module: I18nPhoneNumbers::Metadata::ClassMethods

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

Instance Method Summary collapse

Instance Method Details

#countryCodeToRegionCodeMapObject



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/i18n_phone_numbers/metadata.rb', line 164

def countryCodeToRegionCodeMap
  
  return @countryCodeToRegionCodeMap if @countryCodeToRegionCodeMap
  
  @countryCodeToRegionCodeMap = {}
  
  territories.each do |id, t|
    
    if !t.country_code.blank?
      
      # main country should be first in the list
      if t.main_country_for_code
        (@countryCodeToRegionCodeMap[t.country_code] ||= []).unshift(id)
      else
        (@countryCodeToRegionCodeMap[t.country_code] ||= []) << id
      end
      
    end
    
  end
  
  @countryCodeToRegionCodeMap
end

#countryToMetadataObject



158
159
160
161
162
# File 'lib/i18n_phone_numbers/metadata.rb', line 158

def 
  
  territories
  
end

#dump_territoriesObject

code to generate a YAML dump of @territories, generated file is used to quickly load territories next time



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/i18n_phone_numbers/metadata.rb', line 191

def dump_territories
  
  # .yml file location
  file_path = File.join(File.dirname(__FILE__), "data", "quick_load.yml")
  
  # remove previous dump if exists
  File.delete(file_path) if File.exists?(file_path)
  
  # generate territories from xml files
  t = territories
  
  # quick fix multiline regexp strings that are not correctly dumped in yaml
  t.each { |id, territory|
    territory.national_prefix_for_parsing = territory.national_prefix_for_parsing.blank? ? territory.national_prefix_for_parsing :
                                                                                           territory.national_prefix_for_parsing.gsub(/\r?\n */, '')
  }
  
  # and dump into .yml file !
  
  # Following code equivalent to :
  #
  #     destFile = File.new(destPath,"w")
  #     destFile.print sql
  #
  # But when switching to ruby 1.9, we will be able to force output encoding to UTF-8 :
  # (see : http://blog.grayproductions.net/articles/ruby_19s_three_default_encodings )
  #
  #     File.open(destPath, "w:UTF-8") do |file|  ...
  #
  File.open(file_path, "w") do |file|
    file << "# ClicRDV comment :\n"
    file << "#\n"
    file << "#    This is an auto-generated file, DON'T MODIFY IT MANUALLY !\n"
    file << "#\n"
    file << "#    Call I18nPhoneNumbers::Metadata.dump_territories to generate\n"
    file << "#    it from xml files instead.\n"
    file << "#\n"
    file << YAML.dump(t)
  end
  
end

#load_from_xml(filename) ⇒ Object



114
115
116
117
118
119
120
# File 'lib/i18n_phone_numbers/metadata.rb', line 114

def load_from_xml(filename)
  
  file = File.open(File.join(File.dirname(__FILE__), "data", filename), "rb")
  contents = file.read
  Hash.from_xml(contents)
  
end

#quick_load_territoriesObject

helper to quickly load territories from YAML dump instead of parsing XML files



234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/i18n_phone_numbers/metadata.rb', line 234

def quick_load_territories
  
  if File.exists?(File.join(File.dirname(__FILE__), "data", "quick_load.yml"))
    
    @territories = File.open( File.join(File.dirname(__FILE__), "data", "quick_load.yml") ) { |infile|
      YAML::load( infile )
    }
    
    return true
  end
  
  return false
  
end

#territoriesObject

hash indexed by country alpha2 code (ex: “FR”, “CH”, “MA”, a.s.o)



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/i18n_phone_numbers/metadata.rb', line 123

def territories
  
  # territories are cached
  return @territories if @territories
  
  
  # if no yaml file to quickly load territories from,
  # generate @territories from xml files
  if !quick_load_territories
    
    @territories = {}
    
    files = [
      "PhoneNumberMetaData.xml"
    ]
    
    files.each { |filename|
      
      data = load_from_xml(filename)
      
      raw_territories = data["phoneNumberMetadata"]["territories"]["territory"]
      raw_territories = [raw_territories] if !(Array === raw_territories) # if only 1 territory in xml file
      
      raw_territories.each { |territory|
        @territories[territory["id"]] = self.new(territory)
      }
    
    }
    
  end
  
  @territories
  
end