cep_facil 1.0.3 → 1.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -93,6 +93,20 @@ address.valid? # true
93
93
 
94
94
  ```
95
95
 
96
+ Você também pode checar se o endereço foi encontrado com o CEP passado da seguinte forma:
97
+
98
+ ```ruby
99
+
100
+ address = CepFacil::API.new "53417-540", token
101
+
102
+ address.found? # true
103
+
104
+ address = CepFacil::API.new "00000000", token
105
+
106
+ address.valid? # false
107
+
108
+ ```
109
+
96
110
  Adicionalmente, seu objeto `CepFacil::API` possui um método `full_format` e seu alias (`full_address`) que o descreve por extenso:
97
111
 
98
112
  ```ruby
data/lib/cep_facil/api.rb CHANGED
@@ -4,56 +4,63 @@ require "net/http"
4
4
  module CepFacil
5
5
  class API
6
6
  attr_reader :read
7
-
7
+
8
8
  BASE_URL = "http://www.cepfacil.com.br/service/?filiacao=%s&cep=%s&formato=%s"
9
9
  METHODS_PLACEHOLDER = {estado: :uf , type: :tipo , state: :uf , city: :cidade , neighborhood: :bairro , street: :descricao , rua: :descricao}
10
-
10
+
11
11
  class << self
12
12
  ##
13
13
  # Removes all non-numeric caracters and limit to 8 caracters
14
14
  def parse_zip_code(zip_code)
15
- return zip_code.to_s.gsub(/([^0-9]+)/,"")[0...8]
15
+ return zip_code.to_s.gsub(/([^0-9]+)/,"")[0...8]
16
16
  end
17
17
  end
18
-
18
+
19
19
  def initialize(*args)
20
20
  args = (args[0].is_a?(Hash) ? args[0] : Hash[[:cep,:token,:format].zip(args)])
21
21
  args[:format] ||= "texto"
22
22
  args[:cep] = CepFacil::API.parse_zip_code(args[:cep])
23
23
 
24
- uri = URI(BASE_URL % [args[:token], args[:cep], args[:format]])
24
+ uri = URI(BASE_URL % [args[:token], args[:cep], args[:format]])
25
25
  @read = Net::HTTP.get(uri)
26
- @data = args[:format].to_s == "texto" ? Hash[@read.split("&").map { |i| i.split("=") } ] : @read
27
-
26
+ @data = args[:format].to_s == "texto" ? Hash[@read.split("&").map { |i| i.split("=") } ] : @read
27
+
28
28
  # OPTIMIZE: Need a better implementation
29
29
  return self
30
30
  end
31
-
31
+
32
32
  ##
33
33
  # Returns true if the address in question is a valid one
34
34
  # returns false otherwise
35
35
  def valid?
36
36
  @data.is_a?(Hash) and @data.size > 1
37
37
  end
38
-
38
+
39
39
  #
40
40
  # Returns the address in its extense format
41
41
  def full_format
42
42
  "#{type} #{street}, #{city} - #{state}, Brasil"
43
43
  end
44
-
44
+
45
+ #
46
+ # Returns true if the address was found with the given zip code (CEP)
47
+ # returns false otherwise
48
+ def found?
49
+ self.status == "encontrado"
50
+ end
51
+
45
52
  def self.get(*args)
46
53
  self.new(*args)
47
54
  end
48
-
55
+
49
56
  private
50
57
  #
51
58
  # Define placeholder methods for the address attributes
52
59
  # in portuguese
53
60
  def method_missing(*args)
54
- return (@data[METHODS_PLACEHOLDER[args[0].to_sym].to_s] || @data[args[0].to_s]).force_encoding(Encoding::UTF_8) || super(*args)
61
+ return (@data[METHODS_PLACEHOLDER[args[0].to_sym].to_s] || @data[args[0].to_s]).force_encoding(Encoding::UTF_8) || super(*args)
55
62
  end
56
-
63
+
57
64
  alias :address :full_format
58
65
  end
59
66
  end
@@ -1,3 +1,3 @@
1
1
  module CepFacil
2
- VERSION = "1.0.3" #:nodoc:
2
+ VERSION = "1.0.4" #:nodoc:
3
3
  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: 1.0.3
4
+ version: 1.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-08-16 00:00:00.000000000 Z
12
+ date: 2012-08-28 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake