cep_facil 0.1.2 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -1,3 +1,4 @@
1
+ .DS_Store
1
2
  *.gem
2
3
  .bundle
3
4
  Gemfile.lock
data/README.md CHANGED
@@ -1,117 +1,110 @@
1
- CepFácil
2
- ===========
1
+ # CepFacil
3
2
 
4
- Wrapper Ruby for the service at cepfacil.com.br
3
+ Ruby wrapper para o serviço em cepfacil.com.br
5
4
 
6
- Installation
7
- ------------
5
+ ## Instalação
8
6
 
9
- In the `Gemfile`
7
+ NOTA: Esse projeto encontra-se na versão 1.x, cuja API difere completamente das versões 0.x. Se você está procurando a versão mais antiga, veja a branch [0x], que se mantém intacta.
8
+
9
+ No `Gemfile`:
10
10
 
11
11
  ```ruby
12
12
  gem "cep_facil"
13
+
13
14
  ```
14
15
 
15
- Or via RubyGems, directly:
16
+ Ou via RubyGems, diretamente:
16
17
 
17
- `gem install cep_facil`
18
+ `$ gem install cep_facil`
18
19
 
19
- CepFacil works only in Ruby 1.9.2 by default. If you need it to work with 1.8.7 you must do the following:
20
+ ## Uso
20
21
 
21
- In the `Gemfile`
22
+ ### Obtendo um endereço pelo CEP
23
+
24
+ Para usar a API do [CepFácil], você precisa obter um token do serviço. Você obtém esse token gratuitamente em cepfacil.com.br
22
25
 
23
26
  ```ruby
24
- gem "cep_facil", :git => "git://github.com/rodrigoalvesvieira/cep_facil.git", :branch => "1.8.7"
27
+
28
+ require "cep_facil"
29
+
30
+ cep = "53417-540"
31
+
32
+ token = "1234567890"
33
+
34
+ address = CepFacil::API.new(cep, token)
35
+
25
36
  ```
26
37
 
27
- Usage
28
- -----
38
+ #### Retorna o endereço referente àquele CEP
29
39
 
30
- ### Fetching an address by zip code
40
+ O retorno é um objeto `CepFacil::API` que contem 6 métodos (propriedades): cep, type, state, city, neighborhood, street. Assim você os acessa:
31
41
 
32
42
  ```ruby
33
- require "cep_facil"
34
- cep = "53417-540"
35
- token = "1234567890" # get yours at cepfacil.com.br
36
- address = CepFacil.get_address(cep, token)
37
- # Returns the address referent to that zip code.
38
- address[:city]
39
- # Returns the city referent to that zip code.
43
+
44
+ address.cep # => "53417540"
45
+
46
+ address.type # => "Rua"
47
+
48
+ address.state # => "PE"
49
+
50
+ address.city # => "Paulista"
51
+
52
+ address.neighborhood # => "Artur Lundgren II"
53
+
54
+ address.street # => "Panelas"
55
+
40
56
  ```
41
57
 
42
- Alternatively, you can do:
58
+ Embora isso deva parecer óbvio, informo que essas propriedades são todas **READONLY**.
59
+
60
+ Adicionalmente, seu objeto `CepFacil::API` possui um método `full` que o descreve por extenso:
43
61
 
44
62
  ```ruby
45
- require "cep_facil"
46
- include CepFacil
47
- address = get_address cep, token
48
- ```
49
63
 
50
- The response is a Hash object that contains 6 keys: cep, type, state, city, neighborhood, description.
64
+ address.full_format # => "Rua Panelas, Paulista - PE, Brasil"
65
+
66
+ ```
51
67
 
52
- There are three formats for one to store Brazilian zip codes (CEPs):
68
+ Existem três formatos possíveis para se armazenar CEPs:
53
69
 
54
70
  ```ruby
55
71
  "12345-678"
56
72
  "12345678"
57
- 12345678 # I wouldn't use this one, really.
73
+ 12345678 # Eu realmente não usaria esse
58
74
  ```
59
75
 
60
- CepFácil works with the three of them.
76
+ CepFacil funciona com esses três formatos.
61
77
 
62
- ### Displaying an address in full mode
78
+ ## Integração com a gem Geocoder
63
79
 
64
- ```ruby
65
- address = CepFacil.get_address(cep, token)
66
- CepFacil.full address
67
- ```
80
+ A gem [Geocoder] é ótima para uso e manipulação de dados geográficos em projetos Ruby. Para integrá-la com o CepFacil, faça assim:
68
81
 
69
- Or:
82
+ ```ruby
70
83
 
71
- ````ruby
72
- require "cep_facil"
73
- include CepFacil
84
+ geocoded_by address.full_format
74
85
 
75
- address = get_address("50050-000", token)
76
- full(address) # => Rua da Aurora, Boa Vista, Recife-PE, Brasil.
77
86
  ```
78
87
 
79
- Author
80
- ------
88
+ ## Autor
81
89
 
82
- * Rodrigo Vieira - rodrigovieira1994@gmail.com - http://www.rodrigoalvesvieira.com
90
+ * Rodrigo Alves Vieira - rodrigovieira1994@gmail.com - http://www.rodrigoalvesvieira.com
83
91
 
84
- Contributors
85
- ------------
92
+ ## Contribuidores
86
93
 
87
94
  * Adriano Bacha - abacha@gmail.com
88
95
 
89
- Thanks
90
- -------
91
-
92
- Huge thanks and cheers to CépFácil (http://cepfacil.com.br) of course. Thanks for the great service that you provide!
93
-
94
- Also, thanks to the [Contributors] of this project.
95
-
96
- Licence
97
- -------
96
+ ## Agradecimentos
98
97
 
99
- Copyright (c) 2012 Rodrigo Vieira. http://www.rodrigoalvesvieira.com/
98
+ Obrigado pelas pessoas que oferecem o serviço [CepFácil], sem o qual esse projeto não seria possível.
100
99
 
101
- Permission is hereby granted, free of charge, to any person obtaining
102
- a copy of this software and associated documentation files (the
103
- "Software"), to use, copy and modify copies of the Software, subject
104
- to the following conditions:
100
+ Obrigado também aos [Contribuidores] desse projeto.
105
101
 
106
- The above copyright notice and this permission notice shall be
107
- included in all copies or substantial portions of the Software.
102
+ ## Licença
108
103
 
109
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
110
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
111
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
112
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
113
- LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
114
- OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
115
- WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
104
+ CepFacil é liberado sob a [licença do MIT] com atribuições a Rodrigo Alves Vieira.
116
105
 
117
- [Contributors]: #ontributors
106
+ [0x]: https://github.com/rodrigoalvesvieira/cep_facil/tree/0x
107
+ [Geocoder]: https://github.com/alexreisner/geocoder
108
+ [CepFácil]: http://cepfacil.com.br
109
+ [Contribuidores]: #contribuidores
110
+ [licença do MIT]: http://pt.wikipedia.org/wiki/Licen%C3%A7a_MIT#Texto_da_licen.C3.A7a
data/Rakefile CHANGED
@@ -5,9 +5,9 @@ require 'rake'
5
5
  require 'rdoc/task'
6
6
  require 'fileutils'
7
7
 
8
- task :test => :spec
8
+ task test: :spec
9
9
 
10
- task :default => :spec
10
+ task default: :spec
11
11
 
12
12
  require "rspec/core/rake_task"
13
13
  RSpec::Core::RakeTask.new(:spec) do |t|
data/cep_facil.gemspec CHANGED
@@ -7,7 +7,7 @@ Gem::Specification.new do |s|
7
7
  s.version = CepFacil::VERSION
8
8
  s.authors = ["Rodrigo Alves Vieira"]
9
9
  s.email = ["rodrigovieira1994@gmail.com"]
10
- s.homepage = "https://github.com/rodrigoalvesvieira/cep_facil"
10
+ s.homepage = "http://github.com/rodrigoalvesvieira/cep_facil"
11
11
  s.summary = %q{Wrapper para o serviço cepfacil.com.br}
12
12
  s.description = %q{Wrapper Ruby para o serviço cepfacil.com.br}
13
13
  s.rubyforge_project = "cep_facil"
@@ -15,5 +15,5 @@ Gem::Specification.new do |s|
15
15
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
16
16
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
17
17
  s.require_paths = ["lib"]
18
- s.add_dependency("rspec", ">=2")
18
+ s.add_dependency("rspec", "~> 2.11.0")
19
19
  end
@@ -0,0 +1,53 @@
1
+ # coding: utf-8
2
+
3
+ require "cgi"
4
+ require "net/http"
5
+
6
+ module CepFacil
7
+ class API
8
+ attr_reader :cep, :type, :state, :city, :neighborhood, :street
9
+ ## Obtenção de endereço atráves do CEP
10
+ # retorna um objeto `CepFacil::API` que contem 6 métodos (propriedades):
11
+ # cep, type, state, city, neighborhood e street.
12
+
13
+ def initialize(cep, token)
14
+ @cep = cep
15
+ @token = token
16
+
17
+ zip_code = @cep.to_s
18
+
19
+ if zip_code.match(/^[0-9]{5}[-]?[0-9]{3}/)
20
+ zip_code.gsub!("-", "")
21
+ @uri = URI.parse "http://www.cepfacil.com.br/service/?filiacao=#{token}&cep=#{zip_code}&formato=texto"
22
+ else
23
+ @uri = URI.parse "http://www.cepfacil.com.br/service/?filiacao=#{token}&cep=#{zip_code}&formato=texto"
24
+ end
25
+
26
+ http = Net::HTTP.new(@uri.host, @uri.port)
27
+ call = Net::HTTP::Get.new(@uri.request_uri)
28
+
29
+ response = http.request(call)
30
+
31
+ pattern = /^([a-z]+)\=/
32
+
33
+ result = response.body.split("&")
34
+
35
+ if result[2]
36
+ @type = result[2].gsub!(pattern, "")
37
+ @state = result[3].gsub!(pattern, "")
38
+ @city = result[4].gsub!(pattern, "")
39
+ @neighborhood = result[5].gsub!(pattern, "")
40
+ @street = result[6].gsub!(pattern, "")
41
+ else
42
+ return false
43
+ end
44
+ end
45
+
46
+ ##
47
+ # Descreve o endereço obtido, por extenso. i.e: "Rua Panelas, Paulista - PE, Brasil"
48
+ #
49
+ def full_format
50
+ "#{self.type} #{self.street}, #{self.city} - #{self.state}, Brasil"
51
+ end
52
+ end
53
+ end
@@ -1,3 +1,3 @@
1
1
  module CepFacil
2
- VERSION = "0.1.2" #:nodoc:
2
+ VERSION = "1.0.0" #:nodoc:
3
3
  end
data/lib/cep_facil.rb CHANGED
@@ -1,75 +1,6 @@
1
- # coding: utf-8
2
- require "cgi"
1
+ require "cep_facil/api"
3
2
  require "cep_facil/version"
4
- require "net/http"
5
3
 
6
4
  module CepFacil
7
-
8
- module_function
9
-
10
- # Receives the zip code (CEP) and the Token (get yours at http://cepfacil.com.br) and
11
- # returns the address referent to that zip code as a hash object.
12
- #
13
- # == Example
14
- # require "cep_facil"
15
- # cep = "5417-540"
16
- # token = "1234567890"
17
- # address = CepFacil.get_address(cep, token)
18
- #
19
- # == You can also do:
20
- # require "cep_facil"
21
- # include CepFacil
22
- # get_address cep, token
23
- #
24
- # # address is a hash with 6 keys: cep, type, state, city, neighborhood, description
25
- #
26
- # Reading them:
27
- # address[:city]
28
- # address[:state]
29
- #
30
- # Some applications store Brazilian zip codes (CEPs) in formats like "22222222", or "22222-222"
31
- # or even as an integer. This method accept these three possible formats so you don´t need to format it yourself.
32
-
33
- def get_address(zip_code, token, dictionary = {})
34
- dictionary = {
35
- "tipo" => :type,
36
- "cidade" => :city,
37
- "bairro" => :neighborhood,
38
- "cep" => :cep,
39
- "descricao" => :description,
40
- "uf" => :state
41
- }.merge(dictionary)
42
-
43
- zip_code = zip_code.to_s
44
-
45
- if zip_code.match(/^[0-9]{5}[-]?[0-9]{3}/)
46
- zip_code.gsub!("-", "")
47
- @uri = URI.parse "http://www.cepfacil.com.br/service/?filiacao=#{token}&cep=#{zip_code}&formato=texto"
48
- else
49
- @uri = URI.parse "http://www.cepfacil.com.br/service/?filiacao=#{token}&cep=#{zip_code}&formato=texto"
50
- end
51
-
52
- http = Net::HTTP.new(@uri.host, @uri.port)
53
- call = Net::HTTP::Get.new(@uri.request_uri)
54
-
55
- response = http.request(call)
56
- address = Hash[* CGI::parse(response.body).map {|key, value| [dictionary[key],value[0]]}.flatten]
57
- end
58
-
59
- # Receives and address hash and returns its extense version
60
- # == Example
61
- # address = CepFacil.get_address "53417-540", "1234456677886545"
62
- # CepFacil.full address
63
- # # => Rua Panelas, Artur Lundgren II, Paulista-PE, Brasil.
64
- #
65
- # == You can also do:
66
- # require "cep_facil"
67
- # include CepFacil
68
- # address = CepFacil.get_address "53417-540", "1234456677886545"
69
- # full address
70
-
71
- def full(address)
72
- "#{address[:type]} #{address[:description]}, #{address[:neighborhood]}, #{address[:city]}-#{address[:state]}, Brasil."
73
- end
74
-
5
+ autoload :API, "cep_facil/api"
75
6
  end
@@ -13,80 +13,55 @@ describe CepFacil do
13
13
  it "can fetch an address from a CEP" do
14
14
  cep = "53417-540"
15
15
 
16
- address = CepFacil.get_address cep, @token
16
+ address = CepFacil::API.new(cep, @token)
17
17
  address.should_not be_nil
18
18
 
19
- address[:type].should eql("Rua")
20
- address[:city].should eql("Paulista")
19
+ address.type.should eql("Rua")
20
+ address.city.should eql("Paulista")
21
21
  end
22
22
 
23
23
  it "works with non formatted zip codes (CEPs)" do
24
24
  cep = "53417540"
25
- address = CepFacil.get_address cep, @token
25
+ address = CepFacil::API.new(cep, @token)
26
26
  address.should_not be_nil
27
27
  end
28
28
 
29
29
  it "works even with zip codes (CEPs) stored as integers" do
30
30
  cep = 53417540
31
- address = CepFacil.get_address cep, @token
31
+ address = CepFacil::API.new(cep, @token)
32
32
  address.should_not be_nil
33
33
  end
34
34
 
35
- it "can get all 6 attributes (cep, type, state, city, neighborhood, description) from a given CEP" do
35
+ it "can get all 6 attributes (cep, type, state, city, neighborhood, street) from a given CEP" do
36
36
  cep = "52060-010"
37
- address = CepFacil.get_address cep, @token
38
-
39
- address[:cep].should eql("52060010")
40
- address[:type].should eql("Rua")
41
- address[:state].should eql("PE")
42
- address[:city].should eql("Recife")
43
- address[:neighborhood].should eql("Parnamirim")
44
- address[:description].should include("Tude de Melo")
45
-
46
- address[:cep].should_not be_nil
47
- address[:type].should_not be_nil
48
- address[:state].should_not be_nil
49
- address[:city].should_not be_nil
50
- address[:neighborhood].should_not be_nil
51
- address[:description].should_not be_nil
52
-
53
- address.should have_key(:cep)
54
- address.should have_key(:type)
55
- address.should have_key(:state)
56
- address.should have_key(:city)
57
- address.should have_key(:neighborhood)
58
- address.should have_key(:description)
37
+ address = CepFacil::API.new(cep, @token)
38
+
39
+ address.cep.should eql("52060010")
40
+ address.type.should eql("Rua")
41
+ address.state.should eql("PE")
42
+ address.city.should eql("Recife")
43
+ address.neighborhood.should eql("Parnamirim")
44
+ address.street.should include("Tude de Melo")
59
45
  end
60
46
 
61
47
  it "can return a string with the full address" do
62
48
  cep = "53417-540"
63
- address = CepFacil.get_address cep, @token
64
- full_version = CepFacil.full address
49
+
50
+ address = CepFacil::API.new(cep, @token)
51
+
52
+ full_version = address.full_format
53
+
65
54
  full_version.should_not be_nil
66
- full_version.should eql("Rua Panelas, Artur Lundgren II, Paulista-PE, Brasil.")
55
+ full_version.should eql("Rua Panelas, Paulista - PE, Brasil")
67
56
  end
68
57
 
69
58
  it "handles correct encoding" do
70
59
  cep = "79005-340"
71
- address = CepFacil.get_address cep, @token
72
- address[:neighborhood].should eql("Amambaí")
73
- address[:description].should eql("Coronel Camisão")
74
- end
75
60
 
76
- it "support a different dictionary" do
77
- cep = "79005-340"
78
- dictionary = {
79
- "tipo" => :type,
80
- "cidade" => :city,
81
- "bairro" => :district,
82
- "cep" => :zipcode,
83
- "descricao" => :street,
84
- "uf" => :uf
85
- }
86
- address = CepFacil.get_address cep, @token, dictionary
87
- address[:zipcode].should eql("79005340")
88
- address[:uf].should eql("MS")
89
- address[:district].should eql("Amambaí")
61
+ address = CepFacil::API.new(cep, @token)
62
+
63
+ address.neighborhood.should eql("Amambaí")
64
+ address.street.should eql("Coronel Camisão")
90
65
  end
91
66
 
92
67
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cep_facil
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 1.0.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,24 +9,24 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-06-13 00:00:00.000000000 Z
12
+ date: 2012-08-05 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
16
16
  requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
- - - ! '>='
19
+ - - ~>
20
20
  - !ruby/object:Gem::Version
21
- version: '2'
21
+ version: 2.11.0
22
22
  type: :runtime
23
23
  prerelease: false
24
24
  version_requirements: !ruby/object:Gem::Requirement
25
25
  none: false
26
26
  requirements:
27
- - - ! '>='
27
+ - - ~>
28
28
  - !ruby/object:Gem::Version
29
- version: '2'
29
+ version: 2.11.0
30
30
  description: Wrapper Ruby para o serviço cepfacil.com.br
31
31
  email:
32
32
  - rodrigovieira1994@gmail.com
@@ -40,10 +40,11 @@ files:
40
40
  - Rakefile
41
41
  - cep_facil.gemspec
42
42
  - lib/cep_facil.rb
43
+ - lib/cep_facil/api.rb
43
44
  - lib/cep_facil/version.rb
44
45
  - spec/cep_facil_spec.rb
45
46
  - spec/spec_helper.rb
46
- homepage: https://github.com/rodrigoalvesvieira/cep_facil
47
+ homepage: http://github.com/rodrigoalvesvieira/cep_facil
47
48
  licenses: []
48
49
  post_install_message:
49
50
  rdoc_options: []