republica_virtual 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env rake
2
+
3
+ require "rake/testtask"
4
+
5
+ Rake::TestTask.new do |t|
6
+ t.libs << "lib"
7
+ t.test_files = Dir["test/**/*_test.rb"]
8
+ end
@@ -0,0 +1,4 @@
1
+ module RepublicaVirtual
2
+
3
+ VERSION = "1.0.0"
4
+ end
@@ -0,0 +1,49 @@
1
+ #encoding: UTF-8
2
+
3
+ # BUSCA DE CEP UTILIZANDO O SITE REPUBLICA VIRTUAL
4
+ # http://republicavirtual.com.br/cep
5
+
6
+ module RepublicaVirtual
7
+
8
+ class Retorno
9
+ attr_accessor :sucesso, :endereco, :bairro, :cidade, :estado
10
+ end
11
+
12
+ def self.pesquisa_cep(cep)
13
+ if(valida_cep(cep))
14
+ busca(cep)
15
+ else
16
+ raise("O #{cep} informado não é valido.")
17
+ end
18
+ end
19
+
20
+ private
21
+ def self.busca (cep)
22
+ require "net/http"
23
+ require "uri"
24
+ require "nokogiri"
25
+
26
+ url = URI.parse("http://cep.republicavirtual.com.br")
27
+ res = Net::HTTP.start(url.host, url.port) {|http|
28
+ http.get("/web_cep.php?cep=#{cep}&formato=xml")
29
+ }
30
+ res = Nokogiri::XML(res.body)
31
+ retorno = Retorno.new
32
+ if !res.xpath("//logradouro").text.to_s.empty?
33
+ retorno.sucesso = true
34
+ retorno.endereco = res.xpath("//tipo_logradouro").text+" "+res.xpath("//logradouro").text
35
+ retorno.bairro = res.xpath("//bairro").text
36
+ retorno.cidade = res.xpath("//cidade").text
37
+ retorno.estado = res.xpath("//uf").text
38
+ else
39
+ retorno.sucesso = false
40
+ end
41
+ retorno
42
+ end
43
+
44
+ def self.valida_cep(cep)
45
+ cep = cep.to_s.gsub("-","")
46
+ return cep.to_i.to_s.length == 8
47
+ end
48
+
49
+ end
@@ -0,0 +1,15 @@
1
+ $:.push File.expand_path("../lib", __FILE__)
2
+
3
+ require "republica_virtual/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "republica_virtual"
7
+ s.version = RepublicaVirtual::VERSION
8
+ s.description = "Busca de CEP Online"
9
+ s.summary = "Realiza a busca do cep online, atraves do webservice do site http://republicavirtual.com.br/cep"
10
+ s.author = "claudsan"
11
+ s.email = "claudsan@gmail.com"
12
+ s.homepage = "http://republicavirtual.com.br/cep"
13
+ s.files = Dir["{lib/**/*.rb,README.rdoc,test/**/*.rb,Rakefile,*.gemspec}"]
14
+ s.add_dependency 'nokogiri' unless Proc.new { install_flags('no-nokogiri') }
15
+ end
@@ -0,0 +1,19 @@
1
+ #encoding: UTF-8
2
+
3
+ require "test/unit"
4
+ require "republica_virtual"
5
+
6
+ class RepublicaVirtualTest < Test::Unit::TestCase
7
+
8
+ def test_busca_cep_correto
9
+ retorno = RepublicaVirtual.pesquisa_cep(38400456)
10
+ assert_equal "Uberlândia", retorno.cidade
11
+ end
12
+
13
+ def test_busca_cep_gsub
14
+ retorno = RepublicaVirtual.pesquisa_cep("38400-456")
15
+ assert_equal "Uberlândia", retorno.cidade
16
+ end
17
+
18
+ end
19
+
metadata ADDED
@@ -0,0 +1,49 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: republica_virtual
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - claudsan
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-01 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Busca de CEP Online
15
+ email: claudsan@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/republica_virtual.rb
21
+ - lib/republica_virtual/version.rb
22
+ - test/republica_virtual_test.rb
23
+ - Rakefile
24
+ - republica_virtual.gemspec
25
+ homepage: http://republicavirtual.com.br/cep
26
+ licenses: []
27
+ post_install_message:
28
+ rdoc_options: []
29
+ require_paths:
30
+ - lib
31
+ required_ruby_version: !ruby/object:Gem::Requirement
32
+ none: false
33
+ requirements:
34
+ - - ! '>='
35
+ - !ruby/object:Gem::Version
36
+ version: '0'
37
+ required_rubygems_version: !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ! '>='
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ requirements: []
44
+ rubyforge_project:
45
+ rubygems_version: 1.8.24
46
+ signing_key:
47
+ specification_version: 3
48
+ summary: Realiza a busca do cep online, atraves do webservice do site http://republicavirtual.com.br/cep
49
+ test_files: []