postmon_ruby 1.0.1 → 2.0.0
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.
- checksums.yaml +4 -4
- data/.rspec +1 -1
- data/README.md +10 -2
- data/Rakefile +10 -0
- data/lib/postmon_ruby.rb +7 -1
- data/lib/postmon_ruby/city.rb +17 -0
- data/lib/postmon_ruby/client.rb +4 -2
- data/lib/postmon_ruby/finders/cep_finder.rb +18 -0
- data/lib/postmon_ruby/finders/city_finder.rb +18 -0
- data/lib/postmon_ruby/finders/finder.rb +24 -0
- data/lib/postmon_ruby/finders/finders.rb +15 -0
- data/lib/postmon_ruby/version.rb +1 -1
- data/spec/postmon_ruby/cep_finder_spec.rb +52 -0
- data/spec/postmon_ruby/city_finder_spec.rb +54 -0
- data/spec/postmon_ruby/client_spec.rb +6 -0
- metadata +13 -4
- data/spec/postmon_ruby/postmon_ruby_spec.rb +0 -45
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ff5b09bd792c1b13f2df53c3a5e500c06ca3eff9
|
4
|
+
data.tar.gz: 8e6400cf6705e415d68e1fe5b958e670d5ab7240
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 693631c654886c4595a88e8d685ba24e05a63a0a7b691d4c90e209cbab147b7b13cac951ef3a75a2e22b9fdb412e5feddccf04e9416436dd347d9f1a430f7eff
|
7
|
+
data.tar.gz: bdcb4c8e14479ff2985dc0ec37fcfdee0e4d4d8b8d9546c23f83467facdc35e902b840ed25aa34ac76eb2e1122788912a52e57bf0ad4a33acb4c3d2bd35ac97a
|
data/.rspec
CHANGED
@@ -1,2 +1,2 @@
|
|
1
1
|
--color
|
2
|
-
--format
|
2
|
+
--format documentation
|
data/README.md
CHANGED
@@ -17,16 +17,24 @@ Or install it yourself as:
|
|
17
17
|
$ gem install postmon_ruby
|
18
18
|
|
19
19
|
## Usage
|
20
|
-
|
20
|
+
####Consultar CEP:
|
21
21
|
```ruby
|
22
22
|
require 'postmon_ruby'
|
23
|
-
resultado = PostmonRuby::Client.search "01330000"
|
23
|
+
resultado = PostmonRuby::Client.search :cep, "01330000"
|
24
24
|
puts resultado.logradouro
|
25
25
|
puts resultado.bairro
|
26
26
|
puts resultado.cidade
|
27
27
|
puts resultado.estado
|
28
28
|
```
|
29
29
|
|
30
|
+
####Consultar informações de uma cidade:
|
31
|
+
```ruby
|
32
|
+
require 'postmon_ruby'
|
33
|
+
resultado = PostmonRuby::Client.search :cidade, "SP", "Araraquara"
|
34
|
+
puts resultado.area_km2
|
35
|
+
puts resultado.codigo_ibge
|
36
|
+
```
|
37
|
+
|
30
38
|
## Contributing
|
31
39
|
|
32
40
|
1. Fork it
|
data/Rakefile
CHANGED
data/lib/postmon_ruby.rb
CHANGED
@@ -1,8 +1,14 @@
|
|
1
1
|
require "postmon_ruby/version"
|
2
2
|
require "postmon_ruby/client"
|
3
3
|
require "postmon_ruby/address"
|
4
|
+
require "postmon_ruby/city"
|
5
|
+
require "postmon_ruby/finders/finder"
|
6
|
+
require "postmon_ruby/finders/cep_finder"
|
7
|
+
require "postmon_ruby/finders/city_finder"
|
8
|
+
require "postmon_ruby/finders/finders"
|
9
|
+
|
4
10
|
require "httparty"
|
5
11
|
|
6
12
|
module PostmonRuby
|
7
|
-
ENDPOINT = "http://api.postmon.com.br"
|
13
|
+
ENDPOINT = "http://api.postmon.com.br/v1"
|
8
14
|
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module PostmonRuby
|
2
|
+
class City
|
3
|
+
@@city_attributes = [ :area_km2, :codigo_ibge ]
|
4
|
+
|
5
|
+
attr_reader :not_found, *@@city_attributes
|
6
|
+
|
7
|
+
def initialize(options={})
|
8
|
+
@not_found = true if options.nil?
|
9
|
+
@@city_attributes.each do |attribute|
|
10
|
+
send(:"#{attribute}=", options[attribute.to_s] || "")
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
attr_writer *@@city_attributes
|
16
|
+
end
|
17
|
+
end
|
data/lib/postmon_ruby/client.rb
CHANGED
@@ -1,7 +1,9 @@
|
|
1
1
|
module PostmonRuby
|
2
2
|
class Client
|
3
|
-
def self.search(
|
4
|
-
|
3
|
+
def self.search(finder, *arguments)
|
4
|
+
arguments.flatten!
|
5
|
+
PostmonRuby::Finders::Finder.new.search(finder, arguments)
|
6
|
+
# PostmonRuby::Finders::Finders.get_finder(finder).search(arguments)
|
5
7
|
end
|
6
8
|
end
|
7
9
|
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module PostmonRuby
|
2
|
+
module Finders
|
3
|
+
class CepFinder < PostmonRuby::Finders::Finder
|
4
|
+
def endpoint
|
5
|
+
"/cep"
|
6
|
+
end
|
7
|
+
|
8
|
+
def arguments_size
|
9
|
+
1
|
10
|
+
end
|
11
|
+
|
12
|
+
def search(*arguments)
|
13
|
+
arguments.flatten!
|
14
|
+
PostmonRuby::Address.new( HTTParty.get(self.arguments_uri(arguments)) )
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module PostmonRuby
|
2
|
+
module Finders
|
3
|
+
class CityFinder < PostmonRuby::Finders::Finder
|
4
|
+
def endpoint
|
5
|
+
"/cidade"
|
6
|
+
end
|
7
|
+
|
8
|
+
def arguments_size
|
9
|
+
2
|
10
|
+
end
|
11
|
+
|
12
|
+
def search(*arguments)
|
13
|
+
arguments.flatten!
|
14
|
+
PostmonRuby::City.new( HTTParty.get(self.arguments_uri(arguments)) )
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module PostmonRuby
|
2
|
+
module Finders
|
3
|
+
class Finder
|
4
|
+
def endpoint
|
5
|
+
raise NotImplementedError
|
6
|
+
end
|
7
|
+
|
8
|
+
def arguments_size
|
9
|
+
raise NotImplementedError
|
10
|
+
end
|
11
|
+
|
12
|
+
def search(finder, *arguments)
|
13
|
+
arguments.flatten!
|
14
|
+
finder = PostmonRuby::Finders::Finders.get_finder(finder)
|
15
|
+
raise ArgumentError.new("wrong number of arguments (#{arguments.size} for #{finder.arguments_size})") if finder.arguments_size != arguments.size
|
16
|
+
finder.search(arguments)
|
17
|
+
end
|
18
|
+
|
19
|
+
def arguments_uri(arguments)
|
20
|
+
URI::encode("#{ENDPOINT}/#{self.endpoint}/#{arguments.join("/")}")
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module PostmonRuby
|
2
|
+
module Finders
|
3
|
+
class Finders
|
4
|
+
@finders = {
|
5
|
+
cep: PostmonRuby::Finders::CepFinder.new,
|
6
|
+
cidade: PostmonRuby::Finders::CityFinder.new
|
7
|
+
}
|
8
|
+
|
9
|
+
def self.get_finder(finder)
|
10
|
+
@finders[finder.to_sym]
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
data/lib/postmon_ruby/version.rb
CHANGED
@@ -0,0 +1,52 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
module PostmonRuby
|
4
|
+
module Finders
|
5
|
+
describe CepFinder do
|
6
|
+
describe "#search" do
|
7
|
+
context "with too many arguments" do
|
8
|
+
it "raise an ArgumentError" do
|
9
|
+
expect{ PostmonRuby::Client.search(:cep, "01330000", "xxxx") }.to raise_error(ArgumentError)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
context "with valid cep" do
|
14
|
+
context "address with street" do
|
15
|
+
let(:address) { PostmonRuby::Client.search(:cep, "01330000") }
|
16
|
+
it "returns a PostmonRuby::Address object" do
|
17
|
+
expect(address).to be_a(PostmonRuby::Address)
|
18
|
+
end
|
19
|
+
it "returns a complete address" do
|
20
|
+
expect(address.bairro).to eq "Bela Vista"
|
21
|
+
expect(address.cidade).to eq "São Paulo"
|
22
|
+
expect(address.logradouro).to eq "Rua Rocha"
|
23
|
+
expect(address.estado).to eq "SP"
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
context "address without street" do
|
28
|
+
let(:address) { PostmonRuby::Client.search(:cep, "85100000") }
|
29
|
+
it "returns a PostmonRuby::Address object" do
|
30
|
+
expect(address).to be_a(PostmonRuby::Address)
|
31
|
+
end
|
32
|
+
it "returns a empty 'logradouro' " do
|
33
|
+
expect(address.logradouro).to be_empty
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
context "with invalid cep" do
|
39
|
+
let(:address) { PostmonRuby::Client.search(:cep, "99999999") }
|
40
|
+
it "returns a PostmonRuby::Address object" do
|
41
|
+
expect(address).to be_a(PostmonRuby::Address)
|
42
|
+
end
|
43
|
+
|
44
|
+
it "returns true in 'not_found'" do
|
45
|
+
expect(address.not_found).to be_true
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
module PostmonRuby
|
4
|
+
module Finders
|
5
|
+
describe CityFinder do
|
6
|
+
describe "#search" do
|
7
|
+
context "with too many arguments" do
|
8
|
+
it "raise an ArgumentError" do
|
9
|
+
expect{ PostmonRuby::Client.search(:cidade, "SP", "Araraquara", "xxxxx") }.to raise_error(ArgumentError)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
context "with less arguments" do
|
14
|
+
it "raise an ArgumentError" do
|
15
|
+
expect{ PostmonRuby::Client.search(:cidade, "SP") }.to raise_error(ArgumentError)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
context "with valid city" do
|
20
|
+
let(:city) { PostmonRuby::Client.search(:cidade, "SP", "Araraquara") }
|
21
|
+
it "returns a PostmonRuby::City object" do
|
22
|
+
expect(city).to be_a(PostmonRuby::City)
|
23
|
+
end
|
24
|
+
it "returns a complete city info" do
|
25
|
+
expect(city.area_km2).to eq "1003,674"
|
26
|
+
expect(city.codigo_ibge).to eq "3503208"
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
context "with valid city with special characters" do
|
31
|
+
let(:city) { PostmonRuby::Client.search(:cidade, "SP", "São Paulo") }
|
32
|
+
it "returns a PostmonRuby::City object" do
|
33
|
+
expect(city).to be_a(PostmonRuby::City)
|
34
|
+
end
|
35
|
+
it "returns a complete city info" do
|
36
|
+
expect(city.area_km2).to eq "1521,101"
|
37
|
+
expect(city.codigo_ibge).to eq "3550308"
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
context "with invalid city" do
|
42
|
+
let(:city) { PostmonRuby::Client.search(:cidade, "SP", "Xxxx") }
|
43
|
+
it "returns a PostmonRuby::City object" do
|
44
|
+
expect(city).to be_a(PostmonRuby::City)
|
45
|
+
end
|
46
|
+
it "returns true in 'not_found'" do
|
47
|
+
expect(city.not_found).to be_true
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: postmon_ruby
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Carlos Ribeiro
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-04-
|
11
|
+
date: 2014-04-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: httparty
|
@@ -95,10 +95,17 @@ files:
|
|
95
95
|
- Rakefile
|
96
96
|
- lib/postmon_ruby.rb
|
97
97
|
- lib/postmon_ruby/address.rb
|
98
|
+
- lib/postmon_ruby/city.rb
|
98
99
|
- lib/postmon_ruby/client.rb
|
100
|
+
- lib/postmon_ruby/finders/cep_finder.rb
|
101
|
+
- lib/postmon_ruby/finders/city_finder.rb
|
102
|
+
- lib/postmon_ruby/finders/finder.rb
|
103
|
+
- lib/postmon_ruby/finders/finders.rb
|
99
104
|
- lib/postmon_ruby/version.rb
|
100
105
|
- postmon_ruby.gemspec
|
101
|
-
- spec/postmon_ruby/
|
106
|
+
- spec/postmon_ruby/cep_finder_spec.rb
|
107
|
+
- spec/postmon_ruby/city_finder_spec.rb
|
108
|
+
- spec/postmon_ruby/client_spec.rb
|
102
109
|
- spec/spec_helper.rb
|
103
110
|
homepage: https://github.com/PostmonAPI/postmon-ruby
|
104
111
|
licenses:
|
@@ -125,5 +132,7 @@ signing_key:
|
|
125
132
|
specification_version: 4
|
126
133
|
summary: A rubygem to access the Postmon API
|
127
134
|
test_files:
|
128
|
-
- spec/postmon_ruby/
|
135
|
+
- spec/postmon_ruby/cep_finder_spec.rb
|
136
|
+
- spec/postmon_ruby/city_finder_spec.rb
|
137
|
+
- spec/postmon_ruby/client_spec.rb
|
129
138
|
- spec/spec_helper.rb
|
@@ -1,45 +0,0 @@
|
|
1
|
-
require "spec_helper"
|
2
|
-
|
3
|
-
module PostmonRuby
|
4
|
-
describe Client do
|
5
|
-
describe "#find" do
|
6
|
-
context "with valid cep" do
|
7
|
-
context "address with street" do
|
8
|
-
let(:address) { PostmonRuby::Client.search("01330000") }
|
9
|
-
it "returns a PostmonRuby::Address object" do
|
10
|
-
expect(address).to be_a(PostmonRuby::Address)
|
11
|
-
end
|
12
|
-
it "returns a complete address" do
|
13
|
-
expect(address.bairro).to eq "Bela Vista"
|
14
|
-
expect(address.cidade).to eq "São Paulo"
|
15
|
-
expect(address.logradouro).to eq "Rua Rocha"
|
16
|
-
expect(address.estado).to eq "SP"
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
context "address without street" do
|
21
|
-
let(:address) { PostmonRuby::Client.search("85100000") }
|
22
|
-
it "returns a PostmonRuby::Address object" do
|
23
|
-
# binding.pry
|
24
|
-
expect(address).to be_a(PostmonRuby::Address)
|
25
|
-
end
|
26
|
-
it "returns a empty 'logradouro' " do
|
27
|
-
expect(address.logradouro).to be_empty
|
28
|
-
end
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
|
-
context "with invalid cep" do
|
33
|
-
let(:address) { PostmonRuby::Client.search("99999999") }
|
34
|
-
it "returns a PostmonRuby::Address object" do
|
35
|
-
expect(address).to be_a(PostmonRuby::Address)
|
36
|
-
end
|
37
|
-
|
38
|
-
it "returns true in 'not_found'" do
|
39
|
-
expect(address.not_found).to be_true
|
40
|
-
end
|
41
|
-
end
|
42
|
-
|
43
|
-
end
|
44
|
-
end
|
45
|
-
end
|