Module: Cepbrasil

Defined in:
lib/cepbrasil.rb,
lib/cepbrasil/version.rb

Constant Summary collapse

VERSION =
"0.0.2"

Class Method Summary collapse

Class Method Details

.cepObject



62
63
64
# File 'lib/cepbrasil.rb', line 62

def self.cep
  return @table.css('td')[4].text
end

.cityObject



56
57
58
# File 'lib/cepbrasil.rb', line 56

def self.city
  return @table.css('td')[2].text
end

.doRequest(cep) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/cepbrasil.rb', line 13

def self.doRequest(cep)
  client = HTTPClient.new

  response = client.post "http://www.buscacep.correios.com.br/servicos/dnec/consultaLogradouroAction.do",{
      "relaxation" => cep,
      "Metodo" => "listaLogradouro",
      "TipoConsulta" => "relaxation",
      "StartRow" => "1",
      "EndRow" => "10"
  }
  @doc = Nokogiri::HTML(response.body)
end

.firstObject



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/cepbrasil.rb', line 37

def self.first

  return false if @table_tr.empty?
   @result = {
      street: @table.css('td')[0].text,
      neightborhood: @table.css('td')[1].text,
      city: @table.css('td')[2].text,
      uf: @table.css('td')[3].text,
      cep: @table.css('td')[4].text
   }
   return @result
end

.neightborhoodObject



53
54
55
# File 'lib/cepbrasil.rb', line 53

def self.neightborhood
  return @table.css('td')[1].text
end

.new(cep) ⇒ Object



7
8
9
10
11
# File 'lib/cepbrasil.rb', line 7

def self.new(cep)
  self.search(cep)
  self.parser
  return self
end

.parserObject



29
30
31
32
33
34
35
# File 'lib/cepbrasil.rb', line 29

def self.parser

  @table = @doc.xpath('/html/body/div/div/div/div/div/div/div/div/div/table')
  @table_tr = @doc.xpath('/html/body/div/div/div/div/div/div/div/div/div/table/tr')
  @quantity = @doc.xpath('/html/body/div/div/div/div/div/div/div/div/b[2]').text

end

.quantityObject



66
67
68
# File 'lib/cepbrasil.rb', line 66

def self.quantity
  return @quantity.split(' Logradouro(s) ')[0].to_i
end

.search(cep) ⇒ Object



25
26
27
# File 'lib/cepbrasil.rb', line 25

def self.search(cep)
  self.doRequest(cep)
end

.streetObject



50
51
52
# File 'lib/cepbrasil.rb', line 50

def self.street
  return @table.css('td')[0].text
end

.ufObject



59
60
61
# File 'lib/cepbrasil.rb', line 59

def self.uf
  return @table.css('td')[3].text
end