cep_facil 0.0.3 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +36 -7
- data/lib/cep_facil/version.rb +1 -1
- data/lib/cep_facil.rb +32 -15
- metadata +4 -4
data/README.md
CHANGED
@@ -19,27 +19,56 @@ Or via RubyGems, directly:
|
|
19
19
|
|
20
20
|
Usage
|
21
21
|
-----
|
22
|
+
|
23
|
+
### Fetching an address by zip code
|
22
24
|
|
23
25
|
```ruby
|
24
26
|
require "cep_facil"
|
25
|
-
cep = "
|
27
|
+
cep = "53417-540"
|
26
28
|
token = "1234567890" # get yours at cepfacil.com.br
|
27
|
-
|
29
|
+
address = CepFacil.get_address(cep, token)
|
28
30
|
# Returns the address referent to that zip code.
|
29
|
-
|
31
|
+
address[:city]
|
30
32
|
# Returns the city referent to that zip code.
|
31
33
|
```
|
32
34
|
|
35
|
+
Alternatively, you can do:
|
36
|
+
|
37
|
+
```ruby
|
38
|
+
require "cep_facil"
|
39
|
+
include CepFacil
|
40
|
+
address = get_address cep, token
|
41
|
+
```
|
42
|
+
|
33
43
|
The response is a Hash object that contains 6 keys: cep, type, state, city, neighborhood, description.
|
34
44
|
|
35
|
-
There are three formats for one to store Brazilian zip codes (
|
45
|
+
There are three formats for one to store Brazilian zip codes (CEPs):
|
36
46
|
|
37
|
-
|
38
|
-
|
39
|
-
|
47
|
+
```ruby
|
48
|
+
"12345-678"
|
49
|
+
"12345678"
|
50
|
+
12345678 # I wouldn't use this one, really.
|
51
|
+
```
|
40
52
|
|
41
53
|
CepFácil works with the three of them.
|
42
54
|
|
55
|
+
### Displaying an address in full mode
|
56
|
+
|
57
|
+
```ruby
|
58
|
+
address = CepFacil.get_address(cep, token)
|
59
|
+
CepFacil.full address
|
60
|
+
```
|
61
|
+
|
62
|
+
Or:
|
63
|
+
|
64
|
+
````ruby
|
65
|
+
require "cep_facil"
|
66
|
+
include CepFacil
|
67
|
+
|
68
|
+
address = get_address("50050-000", token)
|
69
|
+
full(address) # => Rua da Aurora, Boa Vista, Recife-PE, Brasil.
|
70
|
+
```
|
71
|
+
|
43
72
|
Author
|
44
73
|
------
|
45
74
|
|
data/lib/cep_facil/version.rb
CHANGED
data/lib/cep_facil.rb
CHANGED
@@ -3,23 +3,33 @@ require "cep_facil/version"
|
|
3
3
|
require "net/http"
|
4
4
|
|
5
5
|
module CepFacil
|
6
|
+
|
7
|
+
module_function
|
6
8
|
|
7
9
|
# Receives the zip code (CEP) and the Token (get yours at http://cepfacil.com.br) and
|
8
10
|
# returns the address referent to that zip code as a hash object.
|
9
11
|
#
|
10
|
-
# ==
|
11
|
-
#
|
12
|
-
#
|
13
|
-
#
|
14
|
-
#
|
15
|
-
#
|
12
|
+
# == Example
|
13
|
+
# require "cep_facil"
|
14
|
+
# cep = "5417-540"
|
15
|
+
# token = "1234567890"
|
16
|
+
# address = CepFacil.get_address(cep, token)
|
17
|
+
#
|
18
|
+
# == You can also do:
|
19
|
+
# require "cep_facil"
|
20
|
+
# include CepFacil
|
21
|
+
# get_address cep, token
|
22
|
+
#
|
23
|
+
# # address is a hash with 6 keys: cep, type, state, city, neighborhood, description
|
24
|
+
#
|
16
25
|
# Reading them:
|
17
|
-
#
|
18
|
-
#
|
26
|
+
# address[:city]
|
27
|
+
# address[:state]
|
28
|
+
#
|
19
29
|
# Some applications store Brazilian zip codes (CEPs) in formats like "22222222", or "22222-222"
|
20
30
|
# or even as an integer. This method accept these three possible formats so you don´t need to format it yourself.
|
21
31
|
|
22
|
-
def
|
32
|
+
def get_address(zip_code, token)
|
23
33
|
zip_code = zip_code.to_s
|
24
34
|
|
25
35
|
if zip_code.match(/^[0-9]{5}[-]?[0-9]{3}/)
|
@@ -56,12 +66,19 @@ module CepFacil
|
|
56
66
|
end
|
57
67
|
|
58
68
|
# Receives and address hash and returns its extense version
|
59
|
-
#
|
60
|
-
#
|
61
|
-
#
|
62
|
-
# => Rua Panelas, Artur Lundgren II, Paulista-PE, Brasil.
|
63
|
-
|
64
|
-
|
69
|
+
# == Example
|
70
|
+
# address = CepFacil.get_address "53417-540", "1234456677886545"
|
71
|
+
# CepFacil.full address
|
72
|
+
# # => Rua Panelas, Artur Lundgren II, Paulista-PE, Brasil.
|
73
|
+
#
|
74
|
+
# == You can also do:
|
75
|
+
# require "cep_facil"
|
76
|
+
# include CepFacil
|
77
|
+
# address = CepFacil.get_address "53417-540", "1234456677886545"
|
78
|
+
# full address
|
79
|
+
|
80
|
+
def full(address)
|
65
81
|
"#{address[:type]} #{address[:description]}, #{address[:neighborhood]}, #{address[:city]}-#{address[:state]}, Brasil."
|
66
82
|
end
|
83
|
+
|
67
84
|
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.0
|
4
|
+
version: 0.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-01-
|
12
|
+
date: 2012-01-04 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
16
|
-
requirement: &
|
16
|
+
requirement: &70307525443140 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,7 +21,7 @@ dependencies:
|
|
21
21
|
version: '2'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70307525443140
|
25
25
|
description: Wrapper Ruby para o serviço cepfacil.com.br
|
26
26
|
email:
|
27
27
|
- rodrigo@atela.com.br
|