cep_facil 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -1,2 +1,7 @@
1
+ if RUBY_VERSION =~ /1.9/
2
+ Encoding.default_external = Encoding::UTF_8
3
+ Encoding.default_internal = Encoding::UTF_8
4
+ end
5
+
1
6
  source "http://rubygems.org"
2
7
  gemspec
data/README.md CHANGED
@@ -15,11 +15,18 @@ gem "cep_facil"
15
15
  Or via RubyGems, directly:
16
16
 
17
17
  `gem install cep_facil`
18
-
18
+
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
+
21
+ In the `Gemfile`
22
+
23
+ ```ruby
24
+ gem "cep_facil", :git => "git://github.com/rodrigoalvesvieira/cep_facil.git", :branch => "1.8.7"
25
+ ```
19
26
 
20
27
  Usage
21
28
  -----
22
-
29
+
23
30
  ### Fetching an address by zip code
24
31
 
25
32
  ```ruby
@@ -74,11 +81,18 @@ Author
74
81
 
75
82
  * Rodrigo Vieira - rodrigovieira1994@gmail.com - http://www.rodrigoalvesvieira.com
76
83
 
84
+ Contributors
85
+ ------------
86
+
87
+ * Adriano Bacha - abacha@gmail.com
88
+
77
89
  Thanks
78
90
  -------
79
91
 
80
92
  Huge thanks and cheers to CépFácil (http://cepfacil.com.br) of course. Thanks for the great service that you provide!
81
93
 
94
+ Also, thanks to the [Contributors] of this project.
95
+
82
96
  Licence
83
97
  -------
84
98
 
@@ -86,7 +100,7 @@ Copyright (c) 2012 Rodrigo Vieira. http://www.rodrigoalvesvieira.com/
86
100
 
87
101
  Permission is hereby granted, free of charge, to any person obtaining
88
102
  a copy of this software and associated documentation files (the
89
- "Software"), to use, copy and modify copies of the Software, subject
103
+ "Software"), to use, copy and modify copies of the Software, subject
90
104
  to the following conditions:
91
105
 
92
106
  The above copyright notice and this permission notice shall be
@@ -98,4 +112,6 @@ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
98
112
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
99
113
  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
100
114
  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
101
- WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
115
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
116
+
117
+ [Contributors]: #ontributors
data/cep_facil.gemspec CHANGED
@@ -5,8 +5,8 @@ require "cep_facil/version"
5
5
  Gem::Specification.new do |s|
6
6
  s.name = "cep_facil"
7
7
  s.version = CepFacil::VERSION
8
- s.authors = ["Rodrigo Vieira"]
9
- s.email = ["rodrigo@atela.com.br"]
8
+ s.authors = ["Rodrigo Alves Vieira"]
9
+ s.email = ["rodrigovieira1994@gmail.com"]
10
10
  s.homepage = "https://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}
@@ -1,3 +1,3 @@
1
1
  module CepFacil
2
- VERSION = "0.1.1" #:nodoc:
2
+ VERSION = "0.1.2" #:nodoc:
3
3
  end
data/lib/cep_facil.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  # coding: utf-8
2
+ require "cgi"
2
3
  require "cep_facil/version"
3
4
  require "net/http"
4
5
 
@@ -29,7 +30,16 @@ module CepFacil
29
30
  # Some applications store Brazilian zip codes (CEPs) in formats like "22222222", or "22222-222"
30
31
  # or even as an integer. This method accept these three possible formats so you don´t need to format it yourself.
31
32
 
32
- def get_address(zip_code, token)
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
+
33
43
  zip_code = zip_code.to_s
34
44
 
35
45
  if zip_code.match(/^[0-9]{5}[-]?[0-9]{3}/)
@@ -43,26 +53,7 @@ module CepFacil
43
53
  call = Net::HTTP::Get.new(@uri.request_uri)
44
54
 
45
55
  response = http.request(call)
46
- response.body
47
-
48
- pattern = /^([a-z]+)\=/
49
- result = response.body.split("&")
50
-
51
- type = result[2].gsub!(pattern, "")
52
- state = result[3].gsub!(pattern, "")
53
- city = result[4].gsub!(pattern, "")
54
- neighborhood = result[5].gsub!(pattern, "")
55
- description = result[6].gsub!(pattern, "")
56
-
57
- address = {
58
- cep: zip_code,
59
- type: type,
60
- state: state,
61
- city: city,
62
- neighborhood: neighborhood,
63
- description: description
64
- }
65
-
56
+ address = Hash[* CGI::parse(response.body).map {|key, value| [dictionary[key],value[0]]}.flatten]
66
57
  end
67
58
 
68
59
  # Receives and address hash and returns its extense version
@@ -5,51 +5,51 @@ describe CepFacil do
5
5
  before(:each) do
6
6
  @token = "0E2ACA03-FC7F-4E87-9046-A8C46637BA9D"
7
7
  end
8
-
8
+
9
9
  it "has a version" do
10
10
  CepFacil::VERSION.should =~ /^\d+\.\d+\.\d+$/
11
11
  end
12
-
12
+
13
13
  it "can fetch an address from a CEP" do
14
14
  cep = "53417-540"
15
-
15
+
16
16
  address = CepFacil.get_address cep, @token
17
17
  address.should_not be_nil
18
18
 
19
19
  address[:type].should eql("Rua")
20
- address[:city].should eql("Paulista")
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
25
  address = CepFacil.get_address 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
31
  address = CepFacil.get_address cep, @token
32
32
  address.should_not be_nil
33
33
  end
34
-
34
+
35
35
  it "can get all 6 attributes (cep, type, state, city, neighborhood, description) from a given CEP" do
36
36
  cep = "52060-010"
37
37
  address = CepFacil.get_address cep, @token
38
-
38
+
39
39
  address[:cep].should eql("52060010")
40
40
  address[:type].should eql("Rua")
41
41
  address[:state].should eql("PE")
42
42
  address[:city].should eql("Recife")
43
43
  address[:neighborhood].should eql("Parnamirim")
44
44
  address[:description].should include("Tude de Melo")
45
-
45
+
46
46
  address[:cep].should_not be_nil
47
47
  address[:type].should_not be_nil
48
48
  address[:state].should_not be_nil
49
49
  address[:city].should_not be_nil
50
50
  address[:neighborhood].should_not be_nil
51
51
  address[:description].should_not be_nil
52
-
52
+
53
53
  address.should have_key(:cep)
54
54
  address.should have_key(:type)
55
55
  address.should have_key(:state)
@@ -57,7 +57,7 @@ describe CepFacil do
57
57
  address.should have_key(:neighborhood)
58
58
  address.should have_key(:description)
59
59
  end
60
-
60
+
61
61
  it "can return a string with the full address" do
62
62
  cep = "53417-540"
63
63
  address = CepFacil.get_address cep, @token
@@ -66,22 +66,28 @@ describe CepFacil do
66
66
  full_version.should eql("Rua Panelas, Artur Lundgren II, Paulista-PE, Brasil.")
67
67
  end
68
68
 
69
- =begin
70
- it "has a working alternative usage" do
71
- include CepFacil
72
- a = RSpec::Core::ExampleGroup.new
73
- a.extend(CepFacil)
74
-
75
- cep = "50050-000"
76
- address = get_address cep, @token
77
- full_version = full address
78
-
79
- address.should_not be_nil
80
- full_version.should_not be_nil
81
-
82
- full_version.should eql("Rua da Aurora, Boa Vista, Recife-PE, Brasil.")
69
+ it "handles correct encoding" do
70
+ 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
+
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í")
83
90
  end
84
- =end
85
91
 
86
92
  end
87
93
 
metadata CHANGED
@@ -1,19 +1,19 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cep_facil
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
8
- - Rodrigo Vieira
8
+ - Rodrigo Alves Vieira
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-01-04 00:00:00.000000000 Z
12
+ date: 2012-06-13 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
16
- requirement: &70140278475580 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,15 @@ dependencies:
21
21
  version: '2'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70140278475580
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '2'
25
30
  description: Wrapper Ruby para o serviço cepfacil.com.br
26
31
  email:
27
- - rodrigo@atela.com.br
32
+ - rodrigovieira1994@gmail.com
28
33
  executables: []
29
34
  extensions: []
30
35
  extra_rdoc_files: []
@@ -58,7 +63,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
58
63
  version: '0'
59
64
  requirements: []
60
65
  rubyforge_project: cep_facil
61
- rubygems_version: 1.8.13
66
+ rubygems_version: 1.8.23
62
67
  signing_key:
63
68
  specification_version: 3
64
69
  summary: Wrapper para o serviço cepfacil.com.br