Module: CountryCodeLite

Defined in:
lib/country_code_lite.rb,
lib/country_code_lite/version.rb

Defined Under Namespace

Classes: Entry, Error

Constant Summary collapse

DICTIONARY =
YAML.load_file(File.expand_path(File.join("..", "country_code_lite", "country_codes.yml"), __FILE__)).freeze
VERSION =
"0.1.1"

Class Method Summary collapse

Class Method Details

.countriesObject



61
62
63
# File 'lib/country_code_lite.rb', line 61

def countries
  DICTIONARY.sort { |a, b| b["number"] <=> a["number"] }
end

.find_by_code(code) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/country_code_lite.rb', line 27

def find_by_code(code)
  entry = DICTIONARY.select do |e|
    code.upcase == e["code"]
  end.first

  if entry
    Entry.new(entry["name"], entry["en_name"], entry["number"], entry["code"])
  else
    nil
  end
end

.find_by_name(name) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/country_code_lite.rb', line 13

def find_by_name(name)
  entries = DICTIONARY.sort { |a, b| b["name"] <=> a["name"] }

  entry = entries.select do |e|
    name == e["name"]
  end.first

  if entry
    Entry.new(entry["name"], entry["en_name"], entry["number"], entry["code"])
  else
    nil
  end
end

.find_by_number(number) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/country_code_lite.rb', line 39

def find_by_number(number)
  entries = DICTIONARY.sort { |a, b| b["number"] <=> a["number"] }

  entry = entries.select do |e|
    number == e["number"]
  end.first

  if entry
    Entry.new(entry["name"], entry["en_name"], entry["number"], entry["code"])
  else
    nil
  end
end

.to_number(phone, code, with_plus = true) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/country_code_lite.rb', line 53

def to_number(phone, code, with_plus = true)
  if with_plus
    "+#{code}#{phone}"
  else
    "#{code}#{phone}"
  end
end