Last active
June 23, 2016 11:35
-
-
Save mtelis/490501 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Hash; alias :+ :merge; end; Home,Mobile,Work,Gizmo = 1,2,3,7 | |
class VSP | |
@vsptab = {} | |
def self.tab; @vsptab; end | |
attr_reader :fmt, :name, :repeat, :tmo | |
def initialize *args | |
attrib = args.pop | |
args << attrib and attrib = {} unless attrib.class == Hash | |
@pfx, @fmt, @name = args | |
attrib.empty? || %w{pfx name tmo fmt repeat rand}.each do |attr| | |
ivar = "@#{attr}"; val = attrib.delete(attr.to_sym) | |
instance_variable_set(ivar,val) if val && !instance_variable_get(ivar) | |
end | |
@repeat ||= 1; @fmt ||= '+${EXTEN}' if is_gv? | |
unless @pfx.to_s.empty? | |
raise "Duplicated dial prefix '#{@pfx}'" if VSP.tab.has_key?(@pfx) | |
VSP.tab[@pfx] = self | |
end | |
@attrib = attrib if is_gv? | |
end | |
def is_gv?; self.class == GV; end | |
end | |
class GV < VSP | |
attr_reader :rand | |
def getparams num, i=0 | |
acnt = @attrib[:account] || Array[@attrib] | |
a = {:num => num, :tmo => @tmo || 10, :match => '.*'} # init with default values | |
a.update(acnt[i % acnt.length]) # add other params | |
a[:cb].gsub!(/\D/,'') # Delete all but digits | |
a[:type] ||= (a[:cb] =~ /^1?747/) ? Gizmo : Home # if no type, define it depending on area code | |
a.values_at(:usr, :pwd, :cb, :num, :match, :type, :tmo) | |
end | |
end | |
def Provider(*args) | |
a = args.last; a.class == Hash && (a[:usr] || a[:account]) ? GV.new(*args) : VSP.new(*args) | |
end | |
def formatNum(num,exact=false) | |
case num | |
when /^([17])(\d{3})(\d{3})(\d{4})$/, # USA, Russia | |
/^(61)(\d)(\d{4})(\d{4})/, # Australia | |
/^(380|375|41|48|998)(\d{2})(\d{3})(\d{4})$/, # Ukraine, Belarus, Swiss, Poland, Uzbekistan | |
/^(972)(\d{1,2})(\d{3})(\d{4})$/, # Israel | |
/^(36)(1|\d\d)(\d{3})(\d{3,4})$/, # Hungary | |
/^(34)(9[1-9]|[5-9]\d\d)(\d{3})(\d{3,4})$/, # Spain | |
/^(86)(10|2\d|\d{3})(\d{3,4})(\d{4})$/ # China: Beijing, 2x, others 3-dig area code | |
"+#$1 (#$2) #$3-#$4" | |
when /^(33)(\d)(\d{2})(\d{2})(\d{2})(\d{2})$/ # France | |
"+#$1 (#$2) #$3 #$4 #$5 #$6" | |
when /^(44)(11\d|1\d1|2\d|[389]\d\d)(\d{3,4})(\d{4})$/ # UK 2- and 3-digit area codes | |
"+#$1 (#$2) #$3 #$4" # 11x, 1x1, 2x, 3xx, 8xx, 9xx | |
when /^(44)(\d{4})(\d{6,7})$/ # UK 4-digit area codes | |
"+#$1 (#$2) #$3" | |
when /^(39)(0[26]|0\d[0159]|0\d{3}|3\d\d)(\d*)(\d{4})$/, # Italy: Milan, Rome, 0x0, 0x1, 0x5, 0x9, 4-digit | |
/^(49)(1[5-7]\d|30|40|69|89|\d\d1|\d{4})(\d*)(\d{4})$/,# Germany: (mobile|2dig|3dig|4dig) area | |
/^(370)(469|528|37|45|46|57|5|\d{3})(\d*)(\d{4})$/, # Lithuania | |
/^(32)(4[789]\d|[89]0\d|[2-4]|[15-8]\d)(\d*)(\d{4})$/, # Belgium | |
/^(91)(800|11|20|33|40|44|[789]\d|\d{3})(\d*)(\d{4})$/, # India | |
/^(886)(37|49|89|\d)(\d*)(\d{4})$/ # Taiwan | |
sep = $3[2] ? ' ' : '' # separator if $3 group has 3 or more digits | |
"+#$1 #$2 #$3#{sep}#$4" | |
when /^(420)(\d{3})(\d{3})(\d{3})$/ # Czech Republic | |
"+#$1 #$2 #$3 #$4" | |
when /^(37[12])(\d{3,4})(\d{4})$/ # Latvia, Estonia | |
"+#$1 #$2-#$3" | |
when /^(373)([67]\d{2})(\d{2})(\d{3})$/ # Moldova mobile | |
"+#$1 #$2-#$3-#$4" | |
when /^(373)(22|\d{3})(\d{1,2})(\d{2})(\d{2})$/ # Moldova landline | |
"+#$1 (#$2) #$3-#$4-#$5" | |
when /^(1|2[07]|3[0-469]|4[^2]|5[1-8]|6[0-6]|7|8[1246]|9[0-58]|\d{3})/ # all country codes | |
exact ? num : "+#$1 #$'" # the pattern used only if exact == false | |
else num # No match - skip formatting | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment