Module: BrZipCode

Defined in:
lib/br_zip_code.rb,
lib/br_zip_code/config.rb,
lib/br_zip_code/engine.rb,
lib/br_zip_code/service.rb,
lib/br_zip_code/version.rb,
lib/br_zip_code/service/viacep.rb,
lib/br_zip_code/service/postmon.rb,
lib/br_zip_code/service/cep_livre.rb,
lib/br_zip_code/service/correio_control.rb,
lib/br_zip_code/service/republica_virtual.rb,
app/controllers/br_zip_code/zip_code_controller.rb

Defined Under Namespace

Classes: Config, Engine, Service, ZipCodeController

Constant Summary collapse

VERSION =
"1.0.5"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configObject

Returns the value of attribute config.



19
20
21
# File 'lib/br_zip_code.rb', line 19

def config
  @config
end

.servicesObject

Returns the value of attribute services.



20
21
22
# File 'lib/br_zip_code.rb', line 20

def services
  @services
end

Class Method Details

.attemptsObject



35
36
37
# File 'lib/br_zip_code.rb', line 35

def self.attempts
  self.configuration.attempts || 5
end

.attempts=(value) ⇒ Object



43
44
45
# File 'lib/br_zip_code.rb', line 43

def self.attempts= value
  self.configuration.attempts = value
end

.configurationObject



23
24
25
# File 'lib/br_zip_code.rb', line 23

def self.configuration
  self.config ||= BrZipCode::Config.new
end

.configure {|configuration| ... } ⇒ Object

Yields:



27
28
29
# File 'lib/br_zip_code.rb', line 27

def self.configure &block
  yield configuration
end

.formatted(zip_code) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/br_zip_code.rb', line 51

def self.formatted zip_code
  numbers = self.strip zip_code

  if numbers.length == 8
    numbers.insert(2, '.').insert(6, '-')
  else
    nil
  end
end

.get(zip_code) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/br_zip_code.rb', line 83

def self.get zip_code
  counter = 0

  self.services.each do |s|
    counter += 1
    begin
      response = s.new(zip_code)
      if response.valid?
        return response.to_hash
      end
    rescue Net::OpenTimeout
      if counter == attempts
        raise Net::OpenTimeout
      else
        next
      end
    end
  end
end

.strip(zip_code) ⇒ Object



47
48
49
# File 'lib/br_zip_code.rb', line 47

def self.strip zip_code
  zip_code.to_s.gsub(/[^0-9]/, '')
end

.timeoutObject



31
32
33
# File 'lib/br_zip_code.rb', line 31

def self.timeout
  self.configuration.timeout || 3
end

.timeout=(value) ⇒ Object



39
40
41
# File 'lib/br_zip_code.rb', line 39

def self.timeout= value
  self.configuration.timeout = value
end