-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvia_cep_service.rb
51 lines (38 loc) · 1.21 KB
/
via_cep_service.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# frozen_string_literal: true
require 'viacep'
class ViaCepService
def self.call(zip_code)
new(zip_code).check_zip_code
end
private_class_method :new
def initialize(zip_code)
@zip_code = zip_code.to_s
end
def check_zip_code
circuit.run(circuitbox_exceptions: false) do
begin
return ViaCep::Address.new(@zip_code, timeout: 1).is_a? ViaCep::Address
rescue ViaCep::ApiRequestError => e
puts "erro no CEP #{e}"
return false
end
end
end
private
def circuit
@circuit ||= Circuitbox.circuit(:viacep, {
# exceptions circuitbox tracks for counting failures (required)
exceptions: [SocketError, Timeout::Error],
# seconds the circuit stays open once it has passed the error threshold
sleep_window: 10,
# length of interval (in seconds) over which it calculates the error rate
time_window: 2,
# number of requests within `time_window` seconds before it calculates error rates (checked on failures)
volume_threshold: 1,
# exceeding this rate will open the circuit (checked on failures)
error_threshold: 5,
# Logger to use
logger: Logger.new(STDOUT)
})
end
end