cep_facil 1.0.2 → 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -2,6 +2,9 @@
2
2
 
3
3
  Ruby wrapper para o serviço em [cepfacil.com.br]
4
4
 
5
+ [![Code Climate](https://codeclimate.com/badge.png)](https://codeclimate.com/github/rodrigoalvesvieira/cep_facil)
6
+ [![Build Status](https://secure.travis-ci.org/rodrigoalvesvieira/cep_facil.png?branch=master)](http://travis-ci.org/rodrigoalvesvieira/cep_facil)
7
+
5
8
  ## Instalação
6
9
 
7
10
  **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.
data/cep_facil.gemspec CHANGED
@@ -15,5 +15,6 @@ 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("rake", "0.9.2.2")
18
19
  s.add_development_dependency("rspec", "~> 2.10.0")
19
20
  end
data/lib/cep_facil/api.rb CHANGED
@@ -6,31 +6,54 @@ module CepFacil
6
6
  attr_reader :read
7
7
 
8
8
  BASE_URL = "http://www.cepfacil.com.br/service/?filiacao=%s&cep=%s&formato=%s"
9
- METHODS_PLACEHOLDER = {:estado => :uf , :type => :tipo , :state => :uf , :city => :cidade , :neighborhood => :bairro , :street => :descricao , :rua => :descricao}
10
-
9
+ METHODS_PLACEHOLDER = {estado: :uf , type: :tipo , state: :uf , city: :cidade , neighborhood: :bairro , street: :descricao , rua: :descricao}
10
+
11
+ class << self
12
+ ##
13
+ # Removes all non-numeric caracters and limit to 8 caracters
14
+ def parse_zip_code(zip_code)
15
+ return zip_code.to_s.gsub(/([^0-9]+)/,"")[0...8]
16
+ end
17
+ end
18
+
11
19
  def initialize(*args)
12
20
  args = (args[0].is_a?(Hash) ? args[0] : Hash[[:cep,:token,:format].zip(args)])
13
21
  args[:format] ||= "texto"
14
- args[:cep] = args[:cep].to_s.gsub(/([^0-9]+)/,"")[0...8] # Removing all non-numeric caracters and limit to 8 caracters
22
+ args[:cep] = CepFacil::API.parse_zip_code(args[:cep])
15
23
 
16
- uri = URI(BASE_URL % [args[:token],args[:cep],args[:format]])
24
+ uri = URI(BASE_URL % [args[:token], args[:cep], args[:format]])
17
25
  @read = Net::HTTP.get(uri)
18
- @data = args[:format].to_s == "texto" ? Hash[@read.split("&").map { |i| i.split("=") } ] : @read # Need a better implementation
26
+ @data = args[:format].to_s == "texto" ? Hash[@read.split("&").map { |i| i.split("=") } ] : @read
27
+
28
+ # OPTIMIZE: Need a better implementation
19
29
  return self
20
30
  end
31
+
32
+ ##
33
+ # Returns true if the address in question is a valid one
34
+ # returns false otherwise
21
35
  def valid?
22
36
  @data.is_a?(Hash) and @data.size > 1
23
37
  end
38
+
39
+ #
40
+ # Returns the address in its extense format
24
41
  def full_format
25
42
  "#{type} #{street}, #{city} - #{state}, Brasil"
26
43
  end
44
+
27
45
  def self.get(*args)
28
46
  self.new(*args)
29
47
  end
48
+
30
49
  private
50
+ #
51
+ # Define placeholder methods for the address attributes
52
+ # in portuguese
31
53
  def method_missing(*args)
32
54
  return (@data[METHODS_PLACEHOLDER[args[0].to_sym].to_s] || @data[args[0].to_s]).force_encoding(Encoding::UTF_8) || super(*args)
33
55
  end
56
+
34
57
  alias :address :full_format
35
58
  end
36
59
  end
@@ -1,3 +1,3 @@
1
1
  module CepFacil
2
- VERSION = "1.0.2" #:nodoc:
2
+ VERSION = "1.0.3" #: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.2
4
+ version: 1.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,8 +9,24 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-08-12 00:00:00.000000000 Z
12
+ date: 2012-08-16 00:00:00.000000000 Z
13
13
  dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rake
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - '='
20
+ - !ruby/object:Gem::Version
21
+ version: 0.9.2.2
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - '='
28
+ - !ruby/object:Gem::Version
29
+ version: 0.9.2.2
14
30
  - !ruby/object:Gem::Dependency
15
31
  name: rspec
16
32
  requirement: !ruby/object:Gem::Requirement