Class: Muchkeys::ConsulClient

Inherits:
Object
  • Object
show all
Defined in:
lib/muchkeys/consul_client.rb

Defined Under Namespace

Classes: SafetyViolation

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(application) ⇒ ConsulClient

Returns a new instance of ConsulClient.



7
8
9
# File 'lib/muchkeys/consul_client.rb', line 7

def initialize(application)
  @application = application
end

Instance Attribute Details

#applicationObject

Returns the value of attribute application.



5
6
7
# File 'lib/muchkeys/consul_client.rb', line 5

def application
  @application
end

Instance Method Details

#consul_insert_key_url(key_name, **query) ⇒ Object



48
49
50
# File 'lib/muchkeys/consul_client.rb', line 48

def consul_insert_key_url(key_name, **query)
  URI("#{config.consul_url}/v1/kv/#{key_name}?#{query.to_query}")
end

#consul_key_url(key_name) ⇒ Object



40
41
42
# File 'lib/muchkeys/consul_client.rb', line 40

def consul_key_url(key_name)
  URI("#{config.consul_url}/v1/kv/#{key_name}?raw")
end

#consul_recurse_url(path) ⇒ Object



44
45
46
# File 'lib/muchkeys/consul_client.rb', line 44

def consul_recurse_url(path)
  URI("#{config.consul_url}/v1/kv/#{path}?recurse")
end

#delete(key) ⇒ Object

Raises:



34
35
36
37
38
# File 'lib/muchkeys/consul_client.rb', line 34

def delete(key)
  url = consul_key_url(key)
  raise SafetyViolation unless @unsafe
  Net::HTTP.new(url.host, url.port).send_request('DELETE', url.request_uri)
end

#get(key, recursive: false) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/muchkeys/consul_client.rb', line 15

def get(key, recursive: false)
  url = recursive ? consul_recurse_url(key) : consul_key_url(key)
  response = Net::HTTP.get_response(url)

  if response.code == "200"
    response.body
  else
    nil
  end
rescue Errno::ECONNREFUSED
  nil
end

#put(value, key, **options) ⇒ Object

Raises:



28
29
30
31
32
# File 'lib/muchkeys/consul_client.rb', line 28

def put(value, key, **options)
  url = consul_insert_key_url(key, **options)
  raise SafetyViolation unless @unsafe
  Net::HTTP.new(url.host, url.port).send_request('PUT', url.request_uri, value)
end

#unsafe=(toggle) ⇒ Object



11
12
13
# File 'lib/muchkeys/consul_client.rb', line 11

def unsafe=(toggle)
  @unsafe = toggle
end