Class: ConsulSyncer::Wrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/consul_syncer/wrapper.rb

Defined Under Namespace

Classes: ConsulError

Constant Summary collapse

BACKOFF =
[0.1, 0.5, 1.0, 2.0].freeze

Instance Method Summary collapse

Constructor Details

#initialize(consul) ⇒ Wrapper

Returns a new instance of Wrapper.



11
12
13
# File 'lib/consul_syncer/wrapper.rb', line 11

def initialize(consul)
  @consul = consul
end

Instance Method Details

#request(method, path, payload = nil) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/consul_syncer/wrapper.rb', line 15

def request(method, path, payload = nil)
  retry_on_error do
    args = [path]
    args << payload.to_json if payload
    response = @consul.send(method, *args)
    if response.status == 200
      if method == :get
        JSON.parse(response.body)
      else
        true
      end
    else
      raise(
        ConsulError,
        "Failed to request #{response.env.method} #{response.env.url}: #{response.status} -- #{response.body}"
      )
    end
  end
end