cpa 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. data/Rakefile +20 -0
  2. data/bin/cpa +90 -0
  3. data/cpa.gemspec +23 -0
  4. data/test/cpa.rb +45 -0
  5. metadata +81 -0
data/Rakefile ADDED
@@ -0,0 +1,20 @@
1
+ # encoding: UTF-8
2
+
3
+ task :db do
4
+ require "net/http/persistent"
5
+
6
+ http = Net::HTTP::Persistent.new
7
+
8
+ %w[A B C D E F G H J K L M N P Q R S T U V W X Y Z].each do |provincia|
9
+ res = http.request(URI.parse("http://www.correoargentino.com.ar/consultas/cpa/obtener_localidades/#{provincia}")).body
10
+
11
+ File.open("db/#{provincia}", "w") do |file|
12
+ res.scan(/option value=.(\d+).+>(.+?)</) do |id, name|
13
+ file.write(id)
14
+ file.write(" ")
15
+ file.write(name.gsub("&Ntilde;", "Ñ"))
16
+ file.write("\n")
17
+ end
18
+ end
19
+ end
20
+ end
data/bin/cpa ADDED
@@ -0,0 +1,90 @@
1
+ #! /usr/bin/env ruby
2
+ # encoding: UTF-8
3
+
4
+ PROVINCIAS = {
5
+ "capital federal" => "C",
6
+ "caba" => "C",
7
+ "ciudad autonoma de buenos aires" => "C",
8
+ "buenos aires" => "B",
9
+ "catamarca" => "K",
10
+ "chaco" => "H",
11
+ "chubut" => "U",
12
+ "cordoba" => "X",
13
+ "corrientes" => "W",
14
+ "entre rios" => "E",
15
+ "formosa" => "P",
16
+ "jujuy" => "Y",
17
+ "la pampa" => "L",
18
+ "la rioja" => "F",
19
+ "mendoza" => "M",
20
+ "misiones" => "N",
21
+ "neuquen" => "Q",
22
+ "rio negro" => "R",
23
+ "salta" => "A",
24
+ "san juan" => "J",
25
+ "san luis" => "D",
26
+ "santa cruz" => "Z",
27
+ "santa fe" => "S",
28
+ "santiago del estero" => "G",
29
+ "tierra del fuego" => "V",
30
+ "tucuman" => "T"
31
+ }
32
+
33
+ trap(:INT) { exit 1 }
34
+
35
+ def flunk(message)
36
+ $stderr.puts message
37
+ exit 1
38
+ end
39
+
40
+ def simplify(str)
41
+ str.downcase.tr("áéíóú", "aeiou")
42
+ end
43
+
44
+ def parse(line)
45
+ flunk("Error: nada que buscar.") if line.nil? || line.empty?
46
+
47
+ parts = line.split(/,\s*/)
48
+
49
+ flunk("Error: no se encontró la provincia.") if parts.size < 2
50
+
51
+ if parts[0] =~ /^(?:Av |Avenida )?(.+?) (\d+)$/
52
+ calle = $1
53
+ altura = $2
54
+ end
55
+
56
+ provincia = PROVINCIAS[simplify(parts[-1])] ||
57
+ flunk("Error: no se encontró la provincia '#{parts[-1]}'.")
58
+
59
+ if provincia == "C"
60
+ localidad = "5001"
61
+ else
62
+ localidad = File.open("db/#{provincia}") do |io|
63
+ while line = io.gets
64
+ id, name = line.split(" ", 2)
65
+
66
+ break id if name.start_with?(parts[-2].upcase)
67
+ end
68
+ end
69
+ end
70
+
71
+ require "net/http/persistent"
72
+
73
+ http = Net::HTTP::Persistent.new
74
+
75
+ post = Net::HTTP::Post.new("/consultas/cpa/obtener_cpa")
76
+ post.body = "localidad=#{localidad}&calle=#{http.escape(simplify(calle))}&altura=#{http.escape(altura)}"
77
+ http.request(URI.parse("http://www.correoargentino.com.ar"), post).body[/[A-Z]\d{4}[A-Z]{3}/]
78
+ end
79
+
80
+ ###
81
+
82
+ if ARGV.first == "-"
83
+ $stdout.sync = true
84
+
85
+ while line = gets
86
+ puts parse(line.chomp)
87
+ end
88
+ else
89
+ puts parse(ARGV.first)
90
+ end
data/cpa.gemspec ADDED
@@ -0,0 +1,23 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "cpa"
3
+ s.version = "0.0.1"
4
+ s.summary = "A CLI tool to query Argentinean zip codes."
5
+ s.authors = ["Damian Janowski"]
6
+ s.email = ["djanowski@dimaion.com"]
7
+ s.homepage = "http://github.com/djanowski/cpa"
8
+
9
+ s.add_dependency("net-http-persistent")
10
+ s.add_development_dependency("cutest")
11
+
12
+ s.executables << "cpa"
13
+
14
+ s.files = Dir[
15
+ "*.gemspec",
16
+ "LICENSE",
17
+ "README*",
18
+ "Rakefile",
19
+ "bin/*",
20
+ "lib/**/*.rb",
21
+ "test/*.*",
22
+ ]
23
+ end
data/test/cpa.rb ADDED
@@ -0,0 +1,45 @@
1
+ # encoding: UTF-8
2
+
3
+ require "cutest"
4
+ require "open3"
5
+
6
+ test "CABA" do
7
+ assert_equal `./bin/cpa 'Lavalleja 795, Capital Federal'`, "C1414DTO\n"
8
+ assert_equal `./bin/cpa 'Lavalleja 795, CABA'`, "C1414DTO\n"
9
+ assert_equal `./bin/cpa 'Lavalleja 795, Ciudad Autónoma de Buenos Aires'`, "C1414DTO\n"
10
+ end
11
+
12
+ test "Interior" do
13
+ assert_equal `./bin/cpa 'Independencia 333, Sunchales, Santa Fe'`, "S2322AWD\n"
14
+ assert_equal `./bin/cpa 'Av Independencia 333, Sunchales, Santa Fe'`, "S2322AWD\n"
15
+ assert_equal `./bin/cpa 'Avenida Independencia 333, Sunchales, Santa Fe'`, "S2322AWD\n"
16
+ end
17
+
18
+ test "stdin" do
19
+ Open3.popen3("./bin/cpa -") do |stdin, stdout|
20
+ stdin.puts "Amenábar 1294, Capital Federal"
21
+ assert_equal "C1426AJV\n", stdout.gets
22
+ stdin.puts "Aguirre 230, Capital Federal"
23
+ assert_equal "C1414ASF\n", stdout.gets
24
+ end
25
+ end
26
+
27
+ test "errors" do
28
+ Open3.popen3("./bin/cpa 'Lavalleja 795'") do |_, stdout, stderr, wait|
29
+ assert stdout.read.empty?
30
+ assert stderr.read =~ /Error: no se encontró la provincia/
31
+ assert !wait.value.success?
32
+ end
33
+
34
+ Open3.popen3("./bin/cpa 'Lavalleja 795, New York'") do |_, stdout, stderr, wait|
35
+ assert stdout.read.empty?
36
+ assert stderr.read =~ /Error: no se encontró la provincia 'New York'/
37
+ assert !wait.value.success?
38
+ end
39
+
40
+ Open3.popen3("./bin/cpa") do |_, stdout, stderr, wait|
41
+ assert stdout.read.empty?
42
+ assert stderr.read =~ /Error: nada que buscar/
43
+ assert !wait.value.success?
44
+ end
45
+ end
metadata ADDED
@@ -0,0 +1,81 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cpa
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.1
6
+ platform: ruby
7
+ authors:
8
+ - Damian Janowski
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-06-19 00:00:00 -03:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: net-http-persistent
18
+ prerelease: false
19
+ requirement: &id001 !ruby/object:Gem::Requirement
20
+ none: false
21
+ requirements:
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
24
+ version: "0"
25
+ type: :runtime
26
+ version_requirements: *id001
27
+ - !ruby/object:Gem::Dependency
28
+ name: cutest
29
+ prerelease: false
30
+ requirement: &id002 !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: "0"
36
+ type: :development
37
+ version_requirements: *id002
38
+ description:
39
+ email:
40
+ - djanowski@dimaion.com
41
+ executables:
42
+ - cpa
43
+ extensions: []
44
+
45
+ extra_rdoc_files: []
46
+
47
+ files:
48
+ - cpa.gemspec
49
+ - Rakefile
50
+ - bin/cpa
51
+ - test/cpa.rb
52
+ has_rdoc: true
53
+ homepage: http://github.com/djanowski/cpa
54
+ licenses: []
55
+
56
+ post_install_message:
57
+ rdoc_options: []
58
+
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: "0"
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: "0"
73
+ requirements: []
74
+
75
+ rubyforge_project:
76
+ rubygems_version: 1.6.2
77
+ signing_key:
78
+ specification_version: 3
79
+ summary: A CLI tool to query Argentinean zip codes.
80
+ test_files: []
81
+