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 +4 -4
- data/bin/zipcode +22 -0
- data/lib/via_cep/address.rb +9 -10
- data/lib/via_cep/errors/invalid_zipcode_format.rb +11 -0
- data/lib/via_cep/errors/zipcode_not_found.rb +11 -0
- data/lib/via_cep/validators/zipcode.rb +18 -0
- data/lib/via_cep/version.rb +1 -1
- data/lib/via_cep.rb +11 -2
- data/via_cep.gemspec +2 -3
- metadata +9 -18
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f9fea70f5ce5d76c4cdcf6b2ad7c02946ea77c24
|
4
|
+
data.tar.gz: 5991fcd4f164cccbf9fdb35b83a9948e2a8e426d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
data/lib/via_cep/address.rb
CHANGED
@@ -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
|
-
|
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
|
-
|
13
|
-
|
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,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
|
data/lib/via_cep/version.rb
CHANGED
data/lib/via_cep.rb
CHANGED
@@ -1,4 +1,13 @@
|
|
1
|
+
require 'httparty'
|
1
2
|
require 'via_cep/version'
|
2
|
-
|
3
|
+
|
4
|
+
# Core
|
3
5
|
require 'via_cep/address'
|
4
|
-
require '
|
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 =
|
17
|
-
s.executables =
|
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.
|
4
|
+
version: 0.2.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Marcelo Barreto
|
8
8
|
autorequire:
|
9
|
-
bindir:
|
9
|
+
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-05-
|
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
|