Class: Consulkit::Client

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

Overview

Client for the Consul API.

Defined Under Namespace

Modules: 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 Configurable

#options, #setup!

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



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

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



59
60
61
# File 'lib/consulkit/client.rb', line 59

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

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



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

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

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



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

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

#httpObject



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

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



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

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

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



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

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

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



39
40
41
42
43
# File 'lib/consulkit/client.rb', line 39

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