Class: Consulkit::Client

Inherits:
Object
  • Object
show all
Includes:
Health, KV, Session, Configurable
Defined in:
lib/consulkit/client.rb,
lib/consulkit/client/kv.rb,
lib/consulkit/client/health.rb,
lib/consulkit/client/session.rb

Overview

Client for the Consul API.

Defined Under Namespace

Modules: Health, KV, Session

Constant Summary

Constants included from Configurable

Consulkit::Configurable::CONFIGURABLE_KEYS

Instance Attribute Summary

Attributes included from Configurable

#connection_options, #http_addr, #http_token, #middleware

Instance Method Summary collapse

Methods included from Session

#session_create, #session_delete, #session_read, #session_renew

Methods included from KV

#kv_acquire_lock, #kv_decode_response_body, #kv_delete, #kv_read, #kv_read!, #kv_read_blocking, #kv_read_recursive, #kv_read_recursive_as_hash, #kv_read_single, #kv_read_with_index, #kv_release_lock, #kv_write, #kv_write_cas

Methods included from Health

#health_list_service_instances

Methods included from Configurable

#options, #setup!

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



17
18
19
20
21
22
23
# File 'lib/consulkit/client.rb', line 17

def initialize(options = {})
  CONFIGURABLE_KEYS.each do |key|
    value = options[key].nil? ? Consulkit.instance_variable_get("@#{key}") : options[key]

    instance_variable_set("@#{key}", value)
  end
end

Instance Method Details

#camel_case_keys(hash) ⇒ Object



61
62
63
# File 'lib/consulkit/client.rb', line 61

def camel_case_keys(hash)
  hash.transform_keys { |k| k.to_s.split('_').collect(&:capitalize).join }
end

#delete(url, query_params = {}) ⇒ Object



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

def delete(url, query_params = {})
  request(:delete, url, query_params, nil)
end

#get(url, query_params = {}) ⇒ Object



29
30
31
# File 'lib/consulkit/client.rb', line 29

def get(url, query_params = {})
  request(:get, url, query_params, nil)
end

#httpObject



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/consulkit/client.rb', line 47

def http
  opts = @connection_options

  opts['builder'] = @middleware.dup if middleware

  opts['request'] ||= {}
  opts['request']['params_encoder'] = Faraday::FlatParamsEncoder

  opts['headers'] ||= {}
  opts['headers']['Authorization'] = "Bearer #{http_token}" if @http_token

  @http ||= Faraday.new(http_addr, opts)
end

#post(url, body = nil, query_params = {}) ⇒ Object



33
34
35
# File 'lib/consulkit/client.rb', line 33

def post(url, body = nil, query_params = {})
  request(:post, url, query_params, body)
end

#put(url, body = nil, query_params = {}) ⇒ Object



37
38
39
# File 'lib/consulkit/client.rb', line 37

def put(url, body = nil, query_params = {})
  request(:put, url, query_params, body)
end

#request(method, url, query_params, body) ⇒ Object



41
42
43
44
45
# File 'lib/consulkit/client.rb', line 41

def request(method, url, query_params, body)
  http.run_request(method, url, body, nil) do |request|
    request.params.update(query_params) if query_params
  end
end