cep_facil 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source "http://rubygems.org"
2
+ gemspec
data/README.md ADDED
@@ -0,0 +1,69 @@
1
+ CepFácil
2
+ ===========
3
+
4
+ Wrapper Ruby for the service at cepfacil.com.br
5
+
6
+ Installation
7
+ ------------
8
+
9
+ In the `Gemfile`
10
+
11
+ ```ruby
12
+
13
+ gem `cep_facil`
14
+
15
+ ```
16
+
17
+ Or via RubyGems, directly:
18
+
19
+ gem install cep_facil
20
+
21
+
22
+ Usage
23
+ -----
24
+
25
+ ```ruby
26
+ require "cep_facil"
27
+ cep = "5417-540"
28
+ token = "1234567890" # get yours at cepfacil.com.br
29
+ result = CepFacil.get_address(cep, token)
30
+ # Returns the address referent to that zip code.
31
+ result[:city]
32
+ # Returns the city referent to that zip code.
33
+ ```
34
+
35
+ The response is a Hash object that contains 6 keys: cep, type, state, city, neighborhood, description.
36
+
37
+ There are three formats for one to store Brazilian zip codes (CEPS):
38
+
39
+ * "12345-678"
40
+ * "12345678"
41
+ * 12345678 # Don't use this one, really.
42
+
43
+ CepFácil works with the three of them.
44
+
45
+ AUTHOR
46
+ ------
47
+
48
+ * Rodrigo Vieira - rodrigovieira1994@gmail.com - http://www.rodrigoalvesvieira.com
49
+
50
+ LICENCE
51
+ -------
52
+
53
+ Copyright (c) 2012 Rodrigo Vieira. http://www.rodrigoalvesvieira.com/
54
+
55
+ Permission is hereby granted, free of charge, to any person obtaining
56
+ a copy of this software and associated documentation files (the
57
+ "Software"), to use, copy and modify copies of the Software, subject
58
+ to the following conditions:
59
+
60
+ The above copyright notice and this permission notice shall be
61
+ included in all copies or substantial portions of the Software.
62
+
63
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
64
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
65
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
66
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
67
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
68
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
69
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ #require "bundler/gem_tasks"
3
+
4
+ require 'rake'
5
+ require 'rdoc/task'
6
+ require 'fileutils'
7
+
8
+ task :test => :spec
9
+
10
+ task :default => :spec
11
+
12
+ require "rspec/core/rake_task"
13
+ RSpec::Core::RakeTask.new(:spec) do |t|
14
+ t.rspec_opts = '--backtrace --color'
15
+ end
16
+
17
+ desc 'Generate documentation for CepFacil.'
18
+ Rake::RDocTask.new(:rdoc) do |rdoc|
19
+ rdoc.rdoc_dir = 'rdoc'
20
+ rdoc.title = 'CepFácil'
21
+ rdoc.options << '--line-numbers' << '--inline-source'
22
+ rdoc.rdoc_files.include('README.md')
23
+ rdoc.rdoc_files.include('lib/**/*.rb')
24
+ end
data/cep_facil.gemspec ADDED
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "cep_facil/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "cep_facil"
7
+ s.version = CepFacil::VERSION
8
+ s.authors = ["Rodrigo Vieira"]
9
+ s.email = ["rodrigo@atela.com.br"]
10
+ s.homepage = "https://github.com/rodrigoalvesvieira/cep_facil"
11
+ s.summary = %q{Wrapper para o serviço cepfacil.com.br}
12
+ s.description = %q{Wrapper Ruby para o serviço cepfacil.com.br}
13
+ s.rubyforge_project = "cep_facil"
14
+ s.files = `git ls-files`.split("\n")
15
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
16
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
17
+ s.require_paths = ["lib"]
18
+ s.add_dependency("rspec", ">=2")
19
+ end
@@ -0,0 +1,3 @@
1
+ module CepFacil
2
+ VERSION = "0.0.1"
3
+ end
data/lib/cep_facil.rb ADDED
@@ -0,0 +1,57 @@
1
+ # coding: utf-8
2
+ require "cep_facil/version"
3
+ require "net/http"
4
+
5
+ module CepFacil
6
+
7
+ # Recebe o CEP e o Token (obtenha o seu em http://cepfacil.com.br) e
8
+ # retorna o endereço reference ao CEP passado.
9
+ #
10
+ # == Exemplo
11
+ # require "cep_facil"
12
+ # cep = "5417-540"
13
+ # token = "1234567890"
14
+ # CepFacil.get_address(cep, token)
15
+ # Retorna o endereço daquele cep em forma de Hash.
16
+ # Esse hash tem 6 chaves: cep, type, state, city, neighborhood, description
17
+
18
+ # Algumas pessoas guardam o CEP na forma "22222222", outras como "22222-222"
19
+ # outras até como inteiro.
20
+ # O método CepFacil.get_address aceita as três formas.
21
+
22
+ def self.get_address(zip_code, token)
23
+ zip_code = zip_code.to_s
24
+
25
+ if zip_code.match(/^[0-9]{5}[-]?[0-9]{3}/)
26
+ zip_code.gsub!("-", "")
27
+ @uri = URI.parse "http://www.cepfacil.com.br/service/?filiacao=#{token}&cep=#{zip_code}&formato=texto"
28
+ else
29
+ @uri = URI.parse "http://www.cepfacil.com.br/service/?filiacao=#{token}&cep=#{zip_code}&formato=texto"
30
+ end
31
+
32
+ http = Net::HTTP.new(@uri.host, @uri.port)
33
+ call = Net::HTTP::Get.new(@uri.request_uri)
34
+
35
+ response = http.request(call)
36
+ response.body
37
+
38
+ pattern = /^([a-z]+)\=/
39
+ result = response.body.split("&")
40
+
41
+ type = result[2].gsub!(pattern, "")
42
+ state = result[3].gsub!(pattern, "")
43
+ city = result[4].gsub!(pattern, "")
44
+ neighborhood = result[5].gsub!(pattern, "")
45
+ description = result[6].gsub!(pattern, "")
46
+
47
+ address = {
48
+ cep: zip_code,
49
+ type: type,
50
+ state: state,
51
+ city: city,
52
+ neighborhood: neighborhood,
53
+ description: description
54
+ }
55
+
56
+ end
57
+ end
@@ -0,0 +1,9 @@
1
+ require File.expand_path('spec/spec_helper')
2
+
3
+ describe CepFacil do
4
+
5
+ it "has a version" do
6
+ CepFacil::VERSION.should =~ /^\d+\.\d+\.\d+$/
7
+ end
8
+
9
+ end
@@ -0,0 +1,2 @@
1
+ $LOAD_PATH.unshift 'lib'
2
+ require 'cep_facil'
metadata ADDED
@@ -0,0 +1,67 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cep_facil
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Rodrigo Vieira
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-01-02 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: &70128918947180 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '2'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70128918947180
25
+ description: Wrapper Ruby para o serviço cepfacil.com.br
26
+ email:
27
+ - rodrigo@atela.com.br
28
+ executables: []
29
+ extensions: []
30
+ extra_rdoc_files: []
31
+ files:
32
+ - .gitignore
33
+ - Gemfile
34
+ - README.md
35
+ - Rakefile
36
+ - cep_facil.gemspec
37
+ - lib/cep_facil.rb
38
+ - lib/cep_facil/version.rb
39
+ - spec/cep_facil_spec.rb
40
+ - spec/spec_helper.rb
41
+ homepage: https://github.com/rodrigoalvesvieira/cep_facil
42
+ licenses: []
43
+ post_install_message:
44
+ rdoc_options: []
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ none: false
49
+ requirements:
50
+ - - ! '>='
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ! '>='
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ requirements: []
60
+ rubyforge_project: cep_facil
61
+ rubygems_version: 1.8.13
62
+ signing_key:
63
+ specification_version: 3
64
+ summary: Wrapper para o serviço cepfacil.com.br
65
+ test_files:
66
+ - spec/cep_facil_spec.rb
67
+ - spec/spec_helper.rb