telefon 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +2 -0
- data/Gemfile +3 -0
- data/LICENSE +21 -0
- data/README.md +27 -0
- data/lib/telefon.rb +71 -0
- data/lib/telefon/version.rb +3 -0
- data/makefile +2 -0
- data/telefon.gemspec +17 -0
- data/test/telefon_test.rb +24 -0
- metadata +81 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: bbc27a2c5e7a975f01977a4a681b8ab147621975
|
4
|
+
data.tar.gz: d9625d9e99846a4e292bf5592dcbfec5319b28b4
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: da12497bcb56aba350462effe8e1a9a2c3d3f49b893080615b96c2fdc87e8c927ca982b1c77ba4fa9b6ddda62f570ff8830a30ce960798bb8eb230f0ff0843f9
|
7
|
+
data.tar.gz: 3b8190e8d8cba4a6cd0e792f4a3ab0d28479466752e2ffbbddaf5c6b59a4071cf255ee006dbfdd7f0a0ea94507f08fbf302f0a7dedd574152d563bb41f5294be
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Mayn Kjær
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# telefon
|
2
|
+
|
3
|
+
Formats a number into a US phone number (e.g: (555) 123-9876).
|
4
|
+
You can customize the format in the options hash.
|
5
|
+
|
6
|
+
## Usage
|
7
|
+
|
8
|
+
```ruby
|
9
|
+
require "telefon"
|
10
|
+
|
11
|
+
Telefon.convert(5551234) # => 555-1234
|
12
|
+
Telefon.convert("5551234") # => 555-1234
|
13
|
+
Telefon.convert(1235551234) # => 123-555-1234
|
14
|
+
Telefon.convert(1235551234, area_code: true) # => (123) 555-1234
|
15
|
+
Telefon.convert(1235551234, delimiter: " ") # => 123 555 1234
|
16
|
+
Telefon.convert(1235551234, area_code: true, extension: 555) # => (123) 555-1234 x 555
|
17
|
+
Telefon.convert(1235551234, country_code: 1) # => +1-123-555-1234
|
18
|
+
|
19
|
+
Telefon.convert(1235551234, country_code: 1, extension: 1343, delimiter: '.')
|
20
|
+
# => +1.123.555.1234 x 1343
|
21
|
+
```
|
22
|
+
|
23
|
+
## Installation
|
24
|
+
|
25
|
+
```
|
26
|
+
$ gem install telefon
|
27
|
+
```
|
data/lib/telefon.rb
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
require_relative "telefon/version"
|
2
|
+
|
3
|
+
class Telefon
|
4
|
+
WITH_AREA_CODE = /(\d{1,3})(\d{3})(\d{4}$)/
|
5
|
+
WITHOUT_AREA_CODE = /(\d{0,3})(\d{3})(\d{4})$/
|
6
|
+
|
7
|
+
DEFAULTS = {
|
8
|
+
area_code: false,
|
9
|
+
country_code: nil,
|
10
|
+
delimiter: "-",
|
11
|
+
extension: nil
|
12
|
+
}
|
13
|
+
|
14
|
+
def self.convert(number, options = {})
|
15
|
+
new(options).convert(number)
|
16
|
+
end
|
17
|
+
|
18
|
+
def initialize(options = {})
|
19
|
+
@options = DEFAULTS.merge(options)
|
20
|
+
end
|
21
|
+
|
22
|
+
def convert(number)
|
23
|
+
code = @options[:country_code]
|
24
|
+
ext = @options[:extension]
|
25
|
+
|
26
|
+
result = ""
|
27
|
+
|
28
|
+
result << country_code(code) if code
|
29
|
+
result << convert_to_phone_number(number.to_s)
|
30
|
+
result << extension(ext) if ext
|
31
|
+
|
32
|
+
return result
|
33
|
+
end
|
34
|
+
|
35
|
+
private
|
36
|
+
|
37
|
+
def country_code(code)
|
38
|
+
return "+#{ code }#{ delimiter }"
|
39
|
+
end
|
40
|
+
|
41
|
+
def convert_to_phone_number(number)
|
42
|
+
if @options[:area_code]
|
43
|
+
return convert_with_area_code(number)
|
44
|
+
else
|
45
|
+
return convert_without_area_code(number)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def convert_with_area_code(number)
|
50
|
+
return number.gsub(WITH_AREA_CODE, "(\\1) \\2#{ delimiter }\\3")
|
51
|
+
end
|
52
|
+
|
53
|
+
def convert_without_area_code(number)
|
54
|
+
result = number.gsub(WITHOUT_AREA_CODE, "\\1#{ delimiter }\\2#{ delimiter }\\3")
|
55
|
+
result.slice!(0, delimiter.size) if start_with_delimiter?(result)
|
56
|
+
|
57
|
+
return result
|
58
|
+
end
|
59
|
+
|
60
|
+
def extension(ext)
|
61
|
+
return " x #{ ext }"
|
62
|
+
end
|
63
|
+
|
64
|
+
def start_with_delimiter?(number)
|
65
|
+
delimiter && number.start_with?(delimiter)
|
66
|
+
end
|
67
|
+
|
68
|
+
def delimiter
|
69
|
+
return @options[:delimiter]
|
70
|
+
end
|
71
|
+
end
|
data/makefile
ADDED
data/telefon.gemspec
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
require_relative "lib/telefon/version"
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = "telefon"
|
5
|
+
s.version = Telefon::VERSION
|
6
|
+
s.summary = "Formats a number into a US phone number (e.g: (555) 123-9876)."
|
7
|
+
s.description = s.summary
|
8
|
+
s.authors = ["Mayn Ektvedt Kjær"]
|
9
|
+
s.email = ["mayn.kjaer@gmail.com"]
|
10
|
+
s.homepage = "https://github.com/harmoni/telefon"
|
11
|
+
s.license = "MIT"
|
12
|
+
|
13
|
+
s.files = `git ls-files`.split("\n")
|
14
|
+
|
15
|
+
s.add_development_dependency "bundler", "~> 1.10"
|
16
|
+
s.add_development_dependency "cutest", "~> 1.2"
|
17
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require "bundler/setup"
|
2
|
+
require "cutest"
|
3
|
+
require_relative "../lib/telefon"
|
4
|
+
|
5
|
+
scope do
|
6
|
+
test "convert without area code" do
|
7
|
+
result = Telefon.convert(1235551234)
|
8
|
+
|
9
|
+
assert_equal("123-555-1234", result)
|
10
|
+
end
|
11
|
+
|
12
|
+
test "covert with area code" do
|
13
|
+
result = Telefon.convert(1235551234, area_code: true)
|
14
|
+
|
15
|
+
assert_equal("(123) 555-1234", result)
|
16
|
+
end
|
17
|
+
|
18
|
+
test "with additional options" do
|
19
|
+
result = Telefon.convert(1235551234, country_code: 1,
|
20
|
+
extension: 1343, delimiter: ".")
|
21
|
+
|
22
|
+
assert_equal("+1.123.555.1234 x 1343", result)
|
23
|
+
end
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: telefon
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Mayn Ektvedt Kjær
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-08-25 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.10'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.10'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: cutest
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.2'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.2'
|
41
|
+
description: 'Formats a number into a US phone number (e.g: (555) 123-9876).'
|
42
|
+
email:
|
43
|
+
- mayn.kjaer@gmail.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- ".gitignore"
|
49
|
+
- Gemfile
|
50
|
+
- LICENSE
|
51
|
+
- README.md
|
52
|
+
- lib/telefon.rb
|
53
|
+
- lib/telefon/version.rb
|
54
|
+
- makefile
|
55
|
+
- telefon.gemspec
|
56
|
+
- test/telefon_test.rb
|
57
|
+
homepage: https://github.com/harmoni/telefon
|
58
|
+
licenses:
|
59
|
+
- MIT
|
60
|
+
metadata: {}
|
61
|
+
post_install_message:
|
62
|
+
rdoc_options: []
|
63
|
+
require_paths:
|
64
|
+
- lib
|
65
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '0'
|
75
|
+
requirements: []
|
76
|
+
rubyforge_project:
|
77
|
+
rubygems_version: 2.4.5.1
|
78
|
+
signing_key:
|
79
|
+
specification_version: 4
|
80
|
+
summary: 'Formats a number into a US phone number (e.g: (555) 123-9876).'
|
81
|
+
test_files: []
|