via_cep 0.2.2 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6e1dc00866f51e1cbbd2f57373d807b9357f6ec0
4
- data.tar.gz: c47a8cb976b3015985af14ba2f9ff04aa1eebf5d
3
+ metadata.gz: f9fea70f5ce5d76c4cdcf6b2ad7c02946ea77c24
4
+ data.tar.gz: 5991fcd4f164cccbf9fdb35b83a9948e2a8e426d
5
5
  SHA512:
6
- metadata.gz: 741d6e79f8422f0c9f1b45e108a023ba203c3586c212b22672431b8b29772d658732a8af96114945b58a4a39dd8da3ebd66cd55dc1d6e6c42759012531633203
7
- data.tar.gz: bccf709d95d3dccd96021f73fa7b6f1dc68a81312dc81daceaadc1f1eead0698041e0aa7a27ce87edb2e6ecec00ebda1cbf21a1a8c6d6a77ca7224a8b56790fd
6
+ metadata.gz: ca7584afa6251255a2214229f96b3f7e4fee10cb6e36b9cc66195121ac68b80dc9c5921e55fa141f24e771ae456e491c926e439543e39ddba2c447ec577ee371
7
+ data.tar.gz: 8b1728062681d03e38da3048a398ae149fd343fa4866a10dde4a2f2f8a8f9c2e43afdc37636805b3168f202ed0749b2d3b9cdb65da11dd0a8a30a215460ecc30
data/bin/zipcode ADDED
@@ -0,0 +1,22 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__) + "/../lib"))
4
+ require 'via_cep'
5
+
6
+ if ARGV.count > 0
7
+ ARGV.each do |zipcode|
8
+ address = ViaCep::Address.new(zipcode)
9
+ puts "#{zipcode}:"
10
+ puts " #{address.street}"
11
+ puts " #{address.neighborhood}"
12
+ puts " #{address.city}"
13
+ puts " #{address.state}"
14
+ puts
15
+ end
16
+ else
17
+ puts
18
+ puts 'How to use'
19
+ puts ' zipcode 01001-000'
20
+ puts ' zipcode 01001-000 01406-000'
21
+ puts
22
+ end
@@ -1,21 +1,20 @@
1
+ require_relative 'methods'
2
+
1
3
  module ViaCep
2
4
  class Address
3
- attr_reader :response
4
-
5
5
  def initialize(zipcode)
6
- begin
7
- @response = HTTParty.get("https://viacep.com.br/ws/#{zipcode}/json/")
8
- raise ArgumentError, 'Invalid zipcode format' unless has_valid_format?(zipcode)
9
- end
10
- end
6
+ @response = HTTParty.get("https://viacep.com.br/ws/#{zipcode}/json/")
11
7
 
12
- def has_valid_format?(zipcode)
13
- zipcode.to_s.match(/\d{5}(-)\d{3}\z/) || zipcode.to_s.match(/\d{8}/)
8
+ if !ViaCep::Validators::Zipcode.valid?(zipcode)
9
+ raise ViaCep::Errors::InvalidZipcodeFormat
10
+ elsif @response['erro']
11
+ raise ViaCep::Errors::ZipcodeNotFound
12
+ end
14
13
  end
15
14
 
16
15
  ViaCep::METHODS.each do |method_name, response_method_name|
17
16
  define_method(method_name) do
18
- response[response_method_name]
17
+ @response[response_method_name]
19
18
  end
20
19
  end
21
20
  end
@@ -0,0 +1,11 @@
1
+ module ViaCep
2
+ module Errors
3
+ class InvalidZipcodeFormat < ArgumentError
4
+ attr_reader :message
5
+
6
+ def initialize
7
+ @message = 'Zipcode has an invalid format'
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ module ViaCep
2
+ module Errors
3
+ class ZipcodeNotFound < StandardError
4
+ attr_reader :message
5
+
6
+ def initialize
7
+ @message = "Not found anything for that zipcode"
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,18 @@
1
+ module ViaCep
2
+ module Validators
3
+ module Zipcode
4
+ class << self
5
+ def valid?(zipcode)
6
+ zipcode = zipcode.to_s
7
+
8
+ return true if self.match_regex?(zipcode)
9
+ end
10
+
11
+ def match_regex?(zipcode)
12
+ zipcode = zipcode.to_s
13
+ zipcode.match(/^[0-9]{5}(-)[0-9]{3}$/) || zipcode.match(/^[0-9]{8}$/)
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -1,3 +1,3 @@
1
1
  module ViaCep
2
- VERSION = '0.2.2'
2
+ VERSION = '0.2.3'
3
3
  end
data/lib/via_cep.rb CHANGED
@@ -1,4 +1,13 @@
1
+ require 'httparty'
1
2
  require 'via_cep/version'
2
- require 'via_cep/methods'
3
+
4
+ # Core
3
5
  require 'via_cep/address'
4
- require 'httparty'
6
+ require 'via_cep/methods'
7
+
8
+ # Validators
9
+ require 'via_cep/validators/zipcode'
10
+
11
+ # Errors
12
+ require 'via_cep/errors/invalid_zipcode_format'
13
+ require 'via_cep/errors/zipcode_not_found'
data/via_cep.gemspec CHANGED
@@ -13,14 +13,13 @@ Gem::Specification.new do |s|
13
13
  s.license = "MIT"
14
14
 
15
15
  s.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
16
- s.bindir = 'exe'
17
- s.executables = s.files.grep(%r{^exe/}) { |f| File.basename(f) }
16
+ s.bindir = "bin"
17
+ s.executables = ["zipcode"]
18
18
  s.require_paths = ["lib"]
19
19
 
20
20
  s.add_development_dependency 'bundler', '~> 1.10'
21
21
  s.add_development_dependency 'rake', '~> 10.0'
22
22
  s.add_development_dependency 'rspec'
23
23
  s.add_development_dependency 'codeclimate-test-reporter'
24
- s.add_development_dependency 'pry'
25
24
  s.add_dependency 'httparty'
26
25
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: via_cep
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marcelo Barreto
8
8
  autorequire:
9
- bindir: exe
9
+ bindir: bin
10
10
  cert_chain: []
11
- date: 2016-05-12 00:00:00.000000000 Z
11
+ date: 2016-05-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -66,20 +66,6 @@ dependencies:
66
66
  - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
- - !ruby/object:Gem::Dependency
70
- name: pry
71
- requirement: !ruby/object:Gem::Requirement
72
- requirements:
73
- - - ">="
74
- - !ruby/object:Gem::Version
75
- version: '0'
76
- type: :development
77
- prerelease: false
78
- version_requirements: !ruby/object:Gem::Requirement
79
- requirements:
80
- - - ">="
81
- - !ruby/object:Gem::Version
82
- version: '0'
83
69
  - !ruby/object:Gem::Dependency
84
70
  name: httparty
85
71
  requirement: !ruby/object:Gem::Requirement
@@ -97,7 +83,8 @@ dependencies:
97
83
  description:
98
84
  email:
99
85
  - marcelobarretojunior@gmail.com
100
- executables: []
86
+ executables:
87
+ - zipcode
101
88
  extensions: []
102
89
  extra_rdoc_files: []
103
90
  files:
@@ -112,9 +99,13 @@ files:
112
99
  - Rakefile
113
100
  - bin/console
114
101
  - bin/setup
102
+ - bin/zipcode
115
103
  - lib/via_cep.rb
116
104
  - lib/via_cep/address.rb
105
+ - lib/via_cep/errors/invalid_zipcode_format.rb
106
+ - lib/via_cep/errors/zipcode_not_found.rb
117
107
  - lib/via_cep/methods.rb
108
+ - lib/via_cep/validators/zipcode.rb
118
109
  - lib/via_cep/version.rb
119
110
  - via_cep.gemspec
120
111
  homepage: http://www.github.com/marcelobarreto/via_cep