provincias 0.0.3 → 0.0.4
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.
- data/README.md +26 -17
- data/Rakefile +7 -0
- data/data/cities.csv +8116 -0
- data/data/provinces.csv +52 -0
- data/lib/provincias/version.rb +2 -1
- data/lib/provincias.rb +78 -69
- data/test/test_api.rb +61 -0
- metadata +7 -3
data/data/provinces.csv
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
02;Albacete
|
2
|
+
03;Alicante/Alacant
|
3
|
+
04;Almería
|
4
|
+
01;Araba/Álava
|
5
|
+
33;Asturias
|
6
|
+
05;Ávila
|
7
|
+
06;Badajoz
|
8
|
+
07;Balears, Illes
|
9
|
+
08;Barcelona
|
10
|
+
48;Bizkaia
|
11
|
+
09;Burgos
|
12
|
+
10;Cáceres
|
13
|
+
11;Cádiz
|
14
|
+
39;Cantabria
|
15
|
+
12;Castellón/Castelló
|
16
|
+
13;Ciudad Real
|
17
|
+
14;Córdoba
|
18
|
+
15;Coruña, A
|
19
|
+
16;Cuenca
|
20
|
+
20;Gipuzkoa
|
21
|
+
17;Girona
|
22
|
+
18;Granada
|
23
|
+
19;Guadalajara
|
24
|
+
21;Huelva
|
25
|
+
22;Huesca
|
26
|
+
23;Jaén
|
27
|
+
24;León
|
28
|
+
25;Lleida
|
29
|
+
27;Lugo
|
30
|
+
28;Madrid
|
31
|
+
29;Málaga
|
32
|
+
30;Murcia
|
33
|
+
31;Navarra
|
34
|
+
32;Ourense
|
35
|
+
34;Palencia
|
36
|
+
35;Palmas, Las
|
37
|
+
36;Pontevedra
|
38
|
+
26;Rioja, La
|
39
|
+
37;Salamanca
|
40
|
+
38;Santa Cruz de Tenerife
|
41
|
+
40;Segovia
|
42
|
+
41;Sevilla
|
43
|
+
42;Soria
|
44
|
+
43;Tarragona
|
45
|
+
44;Teruel
|
46
|
+
45;Toledo
|
47
|
+
46;Valencia/Valéncia
|
48
|
+
47;Valladolid
|
49
|
+
49;Zamora
|
50
|
+
50;Zaragoza
|
51
|
+
51;Ceuta
|
52
|
+
52;Melilla
|
data/lib/provincias/version.rb
CHANGED
data/lib/provincias.rb
CHANGED
@@ -1,92 +1,101 @@
|
|
1
1
|
# -*- encoding: utf-8 -*-
|
2
2
|
require "provincias/version"
|
3
|
+
require 'csv'
|
3
4
|
|
4
5
|
module Provincias
|
5
6
|
|
6
7
|
def self.find(id)
|
7
|
-
@@provincias
|
8
|
-
return Provincia.new(p) if id == p[:id]
|
9
|
-
end
|
10
|
-
nil
|
8
|
+
@@provincias[id]
|
11
9
|
end
|
12
10
|
|
13
11
|
def self.find_by_name(name)
|
14
|
-
@@provincias.
|
15
|
-
return Provincia.new(p) if name == p[:name]
|
16
|
-
end
|
17
|
-
nil
|
12
|
+
@@provincias.values.select { |p| p.name == name }.first
|
18
13
|
end
|
19
14
|
|
20
15
|
def self.all
|
21
|
-
@@provincias.
|
16
|
+
@@provincias.values
|
22
17
|
end
|
23
18
|
|
24
19
|
def self.all_for_select
|
25
|
-
@@provincias.map { |p| [p
|
20
|
+
@@provincias.values.sort { |a,b| a.name <=> b.name }.map { |p| [p.name, p.id] }
|
26
21
|
end
|
27
22
|
|
28
23
|
class Provincia
|
29
|
-
|
30
24
|
attr_reader :id, :name
|
25
|
+
def initialize(row)
|
26
|
+
id, @name = row
|
27
|
+
@id = id.to_i
|
28
|
+
end
|
29
|
+
|
30
|
+
def ciudades
|
31
|
+
Ciudades.find_by_provincia(@id)
|
32
|
+
end
|
33
|
+
|
34
|
+
def ciudades_for_select
|
35
|
+
ciudades.sort { |a,b| a.name <=> b.name }.map { |c| [c.name, c.id] }
|
36
|
+
end
|
31
37
|
|
32
|
-
def
|
33
|
-
|
34
|
-
@name = hash[:name]
|
38
|
+
def find_ciudad(id)
|
39
|
+
ciudades.select { |c| c.id == id }.first
|
35
40
|
end
|
41
|
+
|
42
|
+
def find_ciudad_by_name(name)
|
43
|
+
ciudades.select { |c| c.name == name }.first
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def self.load_data
|
48
|
+
@@provincias = {}
|
49
|
+
CSV.foreach(File.expand_path('../../data/provinces.csv', __FILE__), :col_sep => ';', :encoding => 'utf-8') do |row|
|
50
|
+
@@provincias[row[0].to_i] = Provincia.new(row)
|
51
|
+
end
|
52
|
+
Ciudades.load_data(@@provincias)
|
53
|
+
end
|
54
|
+
|
55
|
+
module Ciudades
|
56
|
+
|
57
|
+
def self.find(id)
|
58
|
+
@@ciudades[id]
|
59
|
+
end
|
60
|
+
|
61
|
+
def self.find_by_name(name)
|
62
|
+
@@ciudades.values.select { |c| c.name == name }.first
|
63
|
+
end
|
64
|
+
|
65
|
+
def self.find_by_provincia(provincia_id)
|
66
|
+
@@ciudades.values.select { |c| c.provincia_id == provincia_id }
|
67
|
+
end
|
68
|
+
|
69
|
+
def self.all
|
70
|
+
@@ciudades.values
|
71
|
+
end
|
72
|
+
|
73
|
+
def self.all_for_select
|
74
|
+
@@ciudades.values.sort { |a,b| a.name <=> b.name }.map { |c| [c.name, c.id] }
|
75
|
+
end
|
76
|
+
|
77
|
+
class Ciudad
|
78
|
+
attr_reader :provincia_id, :id, :dc, :name
|
79
|
+
def initialize(row)
|
80
|
+
provincia_id, number, @dc, @name = row
|
81
|
+
@provincia_id = provincia_id.to_i
|
82
|
+
@id = (provincia_id+number).to_i
|
83
|
+
end
|
84
|
+
|
85
|
+
def provincia
|
86
|
+
Provincias.find(@provincia_id)
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
def self.load_data(provincias)
|
91
|
+
@@ciudades = {}
|
92
|
+
CSV.foreach(File.expand_path('../../data/cities.csv', __FILE__), :col_sep => ';', :encoding => 'utf-8') do |row|
|
93
|
+
ciudad = Ciudades::Ciudad.new(row)
|
94
|
+
@@ciudades[(row[0]+row[1]).to_i] = ciudad
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
36
98
|
end
|
37
99
|
|
38
|
-
|
39
|
-
|
40
|
-
{:id => 1, :name => 'Álava'},
|
41
|
-
{:id => 2, :name => 'Albacete'},
|
42
|
-
{:id => 3, :name => 'Alicante'},
|
43
|
-
{:id => 4, :name => 'Almería'},
|
44
|
-
{:id => 5, :name => 'Avila'},
|
45
|
-
{:id => 6, :name => 'Badajoz'},
|
46
|
-
{:id => 7, :name => 'Illes Baleares'},
|
47
|
-
{:id => 8, :name => 'Barcelona'},
|
48
|
-
{:id => 9, :name => 'Burgos'},
|
49
|
-
{:id => 10, :name => 'Cáceres'},
|
50
|
-
{:id => 11, :name => 'Cádiz'},
|
51
|
-
{:id => 12, :name => 'Cástellón'},
|
52
|
-
{:id => 13, :name => 'Ciudad Real'},
|
53
|
-
{:id => 14, :name => 'Córdoba'},
|
54
|
-
{:id => 15, :name => 'Coruña, A'},
|
55
|
-
{:id => 16, :name => 'Cuenca'},
|
56
|
-
{:id => 17, :name => 'Girona'},
|
57
|
-
{:id => 18, :name => 'Granada'},
|
58
|
-
{:id => 19, :name => 'Guadalajara'},
|
59
|
-
{:id => 20, :name => 'Guipuzcoa'},
|
60
|
-
{:id => 21, :name => 'Huelva'},
|
61
|
-
{:id => 22, :name => 'Huesca'},
|
62
|
-
{:id => 23, :name => 'Jaén'},
|
63
|
-
{:id => 24, :name => 'León'},
|
64
|
-
{:id => 25, :name => 'Lleida'},
|
65
|
-
{:id => 26, :name => 'Rioja, La'},
|
66
|
-
{:id => 27, :name => 'Lugo'},
|
67
|
-
{:id => 28, :name => 'Madrid'},
|
68
|
-
{:id => 29, :name => 'Málaga'},
|
69
|
-
{:id => 30, :name => 'Murcia'},
|
70
|
-
{:id => 31, :name => 'Navarra'},
|
71
|
-
{:id => 32, :name => 'Ourense'},
|
72
|
-
{:id => 33, :name => 'Asturias'},
|
73
|
-
{:id => 34, :name => 'Palencia'},
|
74
|
-
{:id => 35, :name => 'Palmas, Las'},
|
75
|
-
{:id => 36, :name => 'Pontevedra'},
|
76
|
-
{:id => 37, :name => 'Salamanca'},
|
77
|
-
{:id => 38, :name => 'Santa Cruz de Tenerife'},
|
78
|
-
{:id => 39, :name => 'Cantabria'},
|
79
|
-
{:id => 40, :name => 'Segovia'},
|
80
|
-
{:id => 41, :name => 'Sevilla'},
|
81
|
-
{:id => 42, :name => 'Soria'},
|
82
|
-
{:id => 43, :name => 'Tarragona'},
|
83
|
-
{:id => 44, :name => 'Teruel'},
|
84
|
-
{:id => 45, :name => 'Toledo'},
|
85
|
-
{:id => 46, :name => 'Valencia'},
|
86
|
-
{:id => 47, :name => 'Valladolid'},
|
87
|
-
{:id => 48, :name => 'Vizcaya'},
|
88
|
-
{:id => 49, :name => 'Zamora'},
|
89
|
-
{:id => 50, :name => 'Zaragoza'},
|
90
|
-
{:id => 51, :name => 'Ceuta'},
|
91
|
-
{:id => 52, :name => 'Melilla'}]
|
92
|
-
end
|
100
|
+
end
|
101
|
+
Provincias.load_data
|
data/test/test_api.rb
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require "provincias"
|
3
|
+
require "test/unit"
|
4
|
+
|
5
|
+
class TestApi < Test::Unit::TestCase
|
6
|
+
|
7
|
+
def test_provincias
|
8
|
+
assert_not_nil Provincias
|
9
|
+
assert_not_nil Provincias.find(28)
|
10
|
+
assert_nil Provincias.find(100000)
|
11
|
+
assert_equal Provincias.find(28).id, 28
|
12
|
+
assert_equal Provincias.find(28).name, "Madrid"
|
13
|
+
assert_equal Provincias.find_by_name('Palmas, Las').id, 35
|
14
|
+
assert_equal Provincias.find_by_name('Palmas, Las').name, 'Palmas, Las'
|
15
|
+
assert_nil Provincias.find_by_name('wombat')
|
16
|
+
assert_instance_of Array, Provincias.all
|
17
|
+
assert_instance_of Provincias::Provincia, Provincias.all.first
|
18
|
+
assert_instance_of Array, Provincias.all_for_select
|
19
|
+
assert_instance_of Array, Provincias.all_for_select.first
|
20
|
+
assert_instance_of String, Provincias.all_for_select.first[0]
|
21
|
+
assert_instance_of Fixnum, Provincias.all_for_select.first[1]
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_ciudades
|
25
|
+
assert_not_nil Provincias::Ciudades
|
26
|
+
assert_nil Provincias::Ciudades.find(99999999)
|
27
|
+
assert_not_nil Provincias::Ciudades.find(35017)
|
28
|
+
assert_equal Provincias::Ciudades.find(35017).id, 35017
|
29
|
+
assert_equal Provincias::Ciudades.find(35017).name, 'Puerto del Rosario'
|
30
|
+
assert_nil Provincias::Ciudades.find_by_name('wombat')
|
31
|
+
assert_instance_of Provincias::Ciudades::Ciudad, Provincias::Ciudades.find_by_name('Palmas de Gran Canaria, Las')
|
32
|
+
assert_equal Provincias::Ciudades.find_by_name('Palmas de Gran Canaria, Las').id, 35016
|
33
|
+
assert_equal Provincias::Ciudades.find_by_name('Palmas de Gran Canaria, Las').name, 'Palmas de Gran Canaria, Las'
|
34
|
+
assert_instance_of Array, Provincias.find(28).ciudades
|
35
|
+
assert_instance_of Provincias::Ciudades::Ciudad, Provincias.find(28).ciudades.first
|
36
|
+
assert Provincias.find(28).ciudades.map { |c| c.name }.include?("Madrid")
|
37
|
+
assert !Provincias.find(35).ciudades.map { |c| c.name }.include?("Madrid")
|
38
|
+
assert_nil Provincias.find(28).find_ciudad(999999999)
|
39
|
+
assert_instance_of Provincias::Ciudades::Ciudad, Provincias.find(28).find_ciudad(28001)
|
40
|
+
assert_equal "Acebeda, La", Provincias.find(28).find_ciudad(28001).name
|
41
|
+
assert_equal 28001, Provincias.find(28).find_ciudad(28001).id
|
42
|
+
assert_instance_of Provincias::Ciudades::Ciudad, Provincias.find(28).find_ciudad_by_name("Alcobendas")
|
43
|
+
assert_nil Provincias.find(28).find_ciudad_by_name("wombat")
|
44
|
+
assert_equal "Alcobendas", Provincias.find(28).find_ciudad_by_name("Alcobendas").name
|
45
|
+
assert_equal 28006, Provincias.find(28).find_ciudad_by_name("Alcobendas").id
|
46
|
+
assert_instance_of Array, Provincias::Ciudades.all_for_select
|
47
|
+
assert_instance_of Array, Provincias::Ciudades.all_for_select.first
|
48
|
+
assert_instance_of String, Provincias::Ciudades.all_for_select.first[0]
|
49
|
+
assert_instance_of Fixnum, Provincias::Ciudades.all_for_select.first[1]
|
50
|
+
assert_instance_of Array, Provincias.find(35).ciudades_for_select
|
51
|
+
assert_instance_of Array, Provincias.find(35).ciudades_for_select.first
|
52
|
+
assert_instance_of String, Provincias.find(35).ciudades_for_select.first[0]
|
53
|
+
assert_instance_of Fixnum, Provincias.find(35).ciudades_for_select.first[1]
|
54
|
+
assert_same Provincias.find(35), Provincias.find(35).ciudades.first.provincia
|
55
|
+
assert_instance_of Provincias::Provincia, Provincias::Ciudades.all.first.provincia
|
56
|
+
assert_instance_of Provincias::Provincia, Provincias::Ciudades.find_by_name('Palmas de Gran Canaria, Las').provincia
|
57
|
+
assert_equal 35, Provincias::Ciudades.find_by_name('Palmas de Gran Canaria, Las').provincia.id
|
58
|
+
assert_equal 'Palmas, Las', Provincias::Ciudades.find_by_name('Palmas de Gran Canaria, Las').provincia.name
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: provincias
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-07-16 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: Provides a way to easily manage spanish provinces in your apps without
|
15
15
|
creating additional tables in your database.
|
@@ -24,10 +24,13 @@ files:
|
|
24
24
|
- LICENSE
|
25
25
|
- README.md
|
26
26
|
- Rakefile
|
27
|
+
- data/cities.csv
|
28
|
+
- data/provinces.csv
|
27
29
|
- init.rb
|
28
30
|
- lib/provincias.rb
|
29
31
|
- lib/provincias/version.rb
|
30
32
|
- provincias.gemspec
|
33
|
+
- test/test_api.rb
|
31
34
|
homepage: https://github.com/agilemonkeys/provincias
|
32
35
|
licenses: []
|
33
36
|
post_install_message:
|
@@ -53,5 +56,6 @@ signing_key:
|
|
53
56
|
specification_version: 3
|
54
57
|
summary: Stop storing spanish provinces in a static table in your database. Use this
|
55
58
|
gem and reclaim a few bytes of your storage quota.
|
56
|
-
test_files:
|
59
|
+
test_files:
|
60
|
+
- test/test_api.rb
|
57
61
|
has_rdoc:
|