spain_zip_codes 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +22 -0
- data/.travis.yml +5 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +104 -0
- data/Rakefile +2 -0
- data/lib/spain_zip_codes.rb +29 -0
- data/lib/spain_zip_codes/data/slug_locations_es.yml +2049 -0
- data/lib/spain_zip_codes/data/zip_locations_es.yml +20722 -0
- data/lib/spain_zip_codes/data/zip_provinces_es.yml +53 -0
- data/lib/spain_zip_codes/locationer.rb +47 -0
- data/lib/spain_zip_codes/provincer.rb +21 -0
- data/lib/spain_zip_codes/version.rb +3 -0
- data/spain_zip_codes.gemspec +22 -0
- data/spec/locationer_spec.rb +79 -0
- data/spec/provincer_spec.rb +42 -0
- data/spec/spain_zip_codes_spec.rb +68 -0
- data/spec/spec_helper.rb +4 -0
- metadata +124 -0
@@ -0,0 +1,53 @@
|
|
1
|
+
provinces:
|
2
|
+
'01': Álava
|
3
|
+
'02': Albacete
|
4
|
+
'03': Alicante
|
5
|
+
'04': Almería
|
6
|
+
'05': Ávila
|
7
|
+
'06': Badajoz
|
8
|
+
'07': Baleares (Palma de Mallorca)
|
9
|
+
'08': Barcelona
|
10
|
+
'09': Burgos
|
11
|
+
'10': Cáceres
|
12
|
+
'11': Cádiz
|
13
|
+
'12': Castellón
|
14
|
+
'13': Ciudad Real
|
15
|
+
'14': Córdoba
|
16
|
+
'15': Coruña
|
17
|
+
'16': Cuenca
|
18
|
+
'17': Gerona
|
19
|
+
'18': Granada
|
20
|
+
'19': Guadalajara
|
21
|
+
'20': Guipúzcoa (San Sebastián)
|
22
|
+
'21': Huelva
|
23
|
+
'22': Huesca
|
24
|
+
'23': Jaén
|
25
|
+
'24': León
|
26
|
+
'25': Lérida
|
27
|
+
'26': La Rioja (Logroño)
|
28
|
+
'27': Lugo
|
29
|
+
'28': Madrid
|
30
|
+
'29': Málaga
|
31
|
+
'30': Murcia
|
32
|
+
'31': Navarra (Pamplona)
|
33
|
+
'32': Orense
|
34
|
+
'33': Asturias
|
35
|
+
'34': Palencia
|
36
|
+
'35': Las Palmas
|
37
|
+
'36': Pontevedra
|
38
|
+
'37': Salamanca
|
39
|
+
'38': Santa Cruz de Tenerife
|
40
|
+
'39': Cantabria
|
41
|
+
'40': Segovia
|
42
|
+
'41': Sevilla
|
43
|
+
'42': Soria
|
44
|
+
'43': Tarragona
|
45
|
+
'44': Teruel
|
46
|
+
'45': Toledo
|
47
|
+
'46': Valencia
|
48
|
+
'47': Valladolid
|
49
|
+
'48': Vizcaya
|
50
|
+
'49': Zamora
|
51
|
+
'50': Zaragoza
|
52
|
+
'51': Ceuta
|
53
|
+
'52': Melilla
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'yaml'
|
2
|
+
|
3
|
+
# This class has duplicated code some way, but I prefer to keep the methods
|
4
|
+
# separated even if they are the same so they are easier to change in the future
|
5
|
+
|
6
|
+
module SpainZipCodes
|
7
|
+
class Locationer
|
8
|
+
SOURCE_YAML = %w(lib spain_zip_codes data zip_locations_es.yml).join('/')
|
9
|
+
LOCATIONS = YAML.load_file(SOURCE_YAML)['locations']
|
10
|
+
SLUGS_YAML = %w(lib spain_zip_codes data slug_locations_es.yml).join('/')
|
11
|
+
SLUGS_LOCS = YAML.load_file(SLUGS_YAML)['locations']
|
12
|
+
|
13
|
+
def self.to_location(zip)
|
14
|
+
location = false
|
15
|
+
|
16
|
+
LOCATIONS.keys.each do |key|
|
17
|
+
if LOCATIONS[key].include?(zip)
|
18
|
+
location = key
|
19
|
+
break
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
location
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.to_zip(location)
|
27
|
+
LOCATIONS[location] || []
|
28
|
+
end
|
29
|
+
|
30
|
+
def self.slug_to_zip(location_slug)
|
31
|
+
SLUGS_LOCS[location_slug] || []
|
32
|
+
end
|
33
|
+
|
34
|
+
def self.zip_to_slug(zip)
|
35
|
+
slug = false
|
36
|
+
|
37
|
+
SLUGS_LOCS.keys.each do |key|
|
38
|
+
if SLUGS_LOCS[key].include?(zip)
|
39
|
+
slug = key
|
40
|
+
break
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
slug
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'yaml'
|
2
|
+
|
3
|
+
module SpainZipCodes
|
4
|
+
class Provincer
|
5
|
+
SOURCE_YAML = %w(lib spain_zip_codes data zip_provinces_es.yml).join('/')
|
6
|
+
PROVINCES = YAML.load_file(SOURCE_YAML).fetch('provinces')
|
7
|
+
|
8
|
+
def self.to_province(zip)
|
9
|
+
return false unless zip.is_a?(String) && zip.length == 5
|
10
|
+
|
11
|
+
truncated_zip = zip[0, 2]
|
12
|
+
PROVINCES[truncated_zip]
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.to_zip(province)
|
16
|
+
zip_prefix = PROVINCES.key(province)
|
17
|
+
return false unless province.length > 0
|
18
|
+
zip_prefix
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
require File.expand_path('../lib/spain_zip_codes/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |spec|
|
5
|
+
spec.name = 'spain_zip_codes'
|
6
|
+
spec.version = SpainZipCodes::VERSION
|
7
|
+
spec.authors = ['Luismi Ramírez', 'Gustavo Caso']
|
8
|
+
spec.email = ['luismir89@gmail.com', 'gustavocaso@gmail.com']
|
9
|
+
spec.summary = %q{Obtain provinces and locations based on zip code}
|
10
|
+
spec.homepage = 'https://github.com/luismiramirez/spain_zip_codes'
|
11
|
+
spec.license = 'MIT'
|
12
|
+
|
13
|
+
spec.files = `git ls-files -z`.gsub(/\x00/, ',').split(',')
|
14
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
15
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
16
|
+
spec.require_paths = ['lib']
|
17
|
+
|
18
|
+
spec.add_development_dependency 'rspec'
|
19
|
+
spec.add_development_dependency 'bundler', '~> 1.6'
|
20
|
+
spec.add_development_dependency 'rake'
|
21
|
+
spec.add_development_dependency 'simplecov'
|
22
|
+
end
|
@@ -0,0 +1,79 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'Locationer' do
|
4
|
+
before(:each) do
|
5
|
+
@locationer = SpainZipCodes::Locationer
|
6
|
+
end
|
7
|
+
|
8
|
+
describe '#to_location' do
|
9
|
+
context 'with existing zip code' do
|
10
|
+
it 'returns the correct location' do
|
11
|
+
expect(@locationer.to_location('28922')).to eq('Alcorcón')
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
context 'with integer zip code' do
|
16
|
+
it 'returns false' do
|
17
|
+
expect(@locationer.to_location(22244)).to be false
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
context 'with unexisting zip code' do
|
22
|
+
it 'returns false' do
|
23
|
+
expect(@locationer.to_location(222999)).to be false
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
describe '#to_zip' do
|
29
|
+
context 'with existing location' do
|
30
|
+
it 'returns an array of zip codes' do
|
31
|
+
expected_result = %w(28920 28921 28922 28923 28924 28925)
|
32
|
+
|
33
|
+
expect(@locationer.to_zip('Alcorcón')).to eq(expected_result)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
context 'with unexisting location' do
|
38
|
+
it 'returns an empty array' do
|
39
|
+
expect(@locationer.to_zip('Jamón Serrano')).to eq([])
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
describe '#slug_to_zip' do
|
45
|
+
context 'with existing slug' do
|
46
|
+
it 'returns an array of zip codes' do
|
47
|
+
expected_result = %w(28920 28921 28922 28923 28924 28925)
|
48
|
+
|
49
|
+
expect(@locationer.slug_to_zip('alcorcon')).to eq(expected_result)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
context 'with unexisting slug' do
|
54
|
+
it 'returns an empty array' do
|
55
|
+
expect(@locationer.slug_to_zip('el-tiroriro')).to eq([])
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
describe '#zip_to_slug' do
|
61
|
+
context 'with existing zip code' do
|
62
|
+
it 'returns the correct slug' do
|
63
|
+
expect(@locationer.zip_to_slug('28922')).to eq('alcorcon')
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
context 'with integer zip code' do
|
68
|
+
it 'returns false' do
|
69
|
+
expect(@locationer.zip_to_slug(66998)).to be false
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
context 'with unexisting zip code' do
|
74
|
+
it 'returns false' do
|
75
|
+
expect(@locationer.zip_to_slug('6969699')).to be false
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'Provincer' do
|
4
|
+
before(:each) do
|
5
|
+
@provincer = SpainZipCodes::Provincer
|
6
|
+
end
|
7
|
+
|
8
|
+
describe '#to_province' do
|
9
|
+
context 'with existing zip code' do
|
10
|
+
it 'returns the correct province' do
|
11
|
+
expect(@provincer.to_province('28047')).to eq('Madrid')
|
12
|
+
expect(@provincer.to_province('08033')).to eq('Barcelona')
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
context 'with integer zip code' do
|
17
|
+
it 'returns false' do
|
18
|
+
expect(@provincer.to_province(28047)).to be false
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
context 'with unexisting string zip code' do
|
23
|
+
it 'returns false' do
|
24
|
+
expect(@provincer.to_province(975890)).to be false
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
describe '#to_zip' do
|
30
|
+
context 'with existing province' do
|
31
|
+
it 'returns the correct zip prefix' do
|
32
|
+
expect(@provincer.to_zip('Madrid')).to eq('28')
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
context 'with unexisting province' do
|
37
|
+
it 'returns false' do
|
38
|
+
expect(@provincer.to_province('Gallifrey')).to be false
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'SpainZipCodes' do
|
4
|
+
before(:each) do
|
5
|
+
@provincer = SpainZipCodes::Provincer
|
6
|
+
@locationer = SpainZipCodes::Locationer
|
7
|
+
end
|
8
|
+
|
9
|
+
describe '#zip_to_province' do
|
10
|
+
it 'calls Provincer#to_province with params' do
|
11
|
+
zip = '28047'
|
12
|
+
|
13
|
+
expect(@provincer).to receive(:to_province).with(zip)
|
14
|
+
|
15
|
+
SpainZipCodes.zip_to_province(zip)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
describe '#province_to_zip' do
|
20
|
+
it 'calls Provincer#to_zip with params' do
|
21
|
+
province = 'Madrid'
|
22
|
+
|
23
|
+
expect(@provincer).to receive(:to_zip).with(province)
|
24
|
+
|
25
|
+
SpainZipCodes.province_to_zip(province)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
describe '#zip_to_location' do
|
30
|
+
it 'calls Locationer#to_zip with params' do
|
31
|
+
zip = '28047'
|
32
|
+
|
33
|
+
expect(@locationer).to receive(:to_location).with(zip)
|
34
|
+
|
35
|
+
SpainZipCodes.zip_to_location(zip)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
describe '#location_to_zip' do
|
40
|
+
it 'calls Locationer#to_zip with params' do
|
41
|
+
location = 'Fuenlabrada'
|
42
|
+
|
43
|
+
expect(@locationer).to receive(:to_zip).with(location)
|
44
|
+
|
45
|
+
SpainZipCodes.location_to_zip(location)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
describe '#location_slug_to_zip' do
|
50
|
+
it 'calls Locationer#slug_to_zip with params' do
|
51
|
+
location_slug = 'alcorcon'
|
52
|
+
|
53
|
+
expect(@locationer).to receive(:slug_to_zip).with(location_slug)
|
54
|
+
|
55
|
+
SpainZipCodes.location_slug_to_zip(location_slug)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
describe '#zip_to_location_slug' do
|
60
|
+
it 'calls Locationer#zip_to_slug with params' do
|
61
|
+
zip = '28922'
|
62
|
+
|
63
|
+
expect(@locationer).to receive(:zip_to_slug).with(zip)
|
64
|
+
|
65
|
+
SpainZipCodes.zip_to_location_slug(zip)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,124 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: spain_zip_codes
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Luismi Ramírez
|
8
|
+
- Gustavo Caso
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2014-10-16 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - '>='
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: '0'
|
21
|
+
type: :development
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - '>='
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: '0'
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: bundler
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - ~>
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '1.6'
|
35
|
+
type: :development
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ~>
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '1.6'
|
42
|
+
- !ruby/object:Gem::Dependency
|
43
|
+
name: rake
|
44
|
+
requirement: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - '>='
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '0'
|
49
|
+
type: :development
|
50
|
+
prerelease: false
|
51
|
+
version_requirements: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - '>='
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
56
|
+
- !ruby/object:Gem::Dependency
|
57
|
+
name: simplecov
|
58
|
+
requirement: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - '>='
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '0'
|
63
|
+
type: :development
|
64
|
+
prerelease: false
|
65
|
+
version_requirements: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
description:
|
71
|
+
email:
|
72
|
+
- luismir89@gmail.com
|
73
|
+
- gustavocaso@gmail.com
|
74
|
+
executables: []
|
75
|
+
extensions: []
|
76
|
+
extra_rdoc_files: []
|
77
|
+
files:
|
78
|
+
- .gitignore
|
79
|
+
- .travis.yml
|
80
|
+
- Gemfile
|
81
|
+
- LICENSE.txt
|
82
|
+
- README.md
|
83
|
+
- Rakefile
|
84
|
+
- lib/spain_zip_codes.rb
|
85
|
+
- lib/spain_zip_codes/data/slug_locations_es.yml
|
86
|
+
- lib/spain_zip_codes/data/zip_locations_es.yml
|
87
|
+
- lib/spain_zip_codes/data/zip_provinces_es.yml
|
88
|
+
- lib/spain_zip_codes/locationer.rb
|
89
|
+
- lib/spain_zip_codes/provincer.rb
|
90
|
+
- lib/spain_zip_codes/version.rb
|
91
|
+
- spain_zip_codes.gemspec
|
92
|
+
- spec/locationer_spec.rb
|
93
|
+
- spec/provincer_spec.rb
|
94
|
+
- spec/spain_zip_codes_spec.rb
|
95
|
+
- spec/spec_helper.rb
|
96
|
+
homepage: https://github.com/luismiramirez/spain_zip_codes
|
97
|
+
licenses:
|
98
|
+
- MIT
|
99
|
+
metadata: {}
|
100
|
+
post_install_message:
|
101
|
+
rdoc_options: []
|
102
|
+
require_paths:
|
103
|
+
- lib
|
104
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
105
|
+
requirements:
|
106
|
+
- - '>='
|
107
|
+
- !ruby/object:Gem::Version
|
108
|
+
version: '0'
|
109
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
110
|
+
requirements:
|
111
|
+
- - '>='
|
112
|
+
- !ruby/object:Gem::Version
|
113
|
+
version: '0'
|
114
|
+
requirements: []
|
115
|
+
rubyforge_project:
|
116
|
+
rubygems_version: 2.2.2
|
117
|
+
signing_key:
|
118
|
+
specification_version: 4
|
119
|
+
summary: Obtain provinces and locations based on zip code
|
120
|
+
test_files:
|
121
|
+
- spec/locationer_spec.rb
|
122
|
+
- spec/provincer_spec.rb
|
123
|
+
- spec/spain_zip_codes_spec.rb
|
124
|
+
- spec/spec_helper.rb
|