Class: ConfigHub::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/config_hub/client.rb,
lib/config_hub/client/version.rb

Constant Summary collapse

VERSION =
'0.1.3'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(server_url, token, context, options = {}) ⇒ Client

Returns a new instance of Client.



8
9
10
11
12
13
14
15
# File 'lib/config_hub/client.rb', line 8

def initialize(server_url, token, context, options = {})
  @token = token
  @context = context
  @options = options
  env = options[:environment] || defined?(Rails) ? Rails.env : 'development'
  faraday_opts = options[:faraday] || {}
  @conn = Faraday.new(server_url, ssl: { verify: env == 'production' }.merge(faraday_opts), headers: headers)
end

Instance Method Details

#config_pulled?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/config_hub/client.rb', line 21

def config_pulled?
  !@data.nil?
end

#fetch(key) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/config_hub/client.rb', line 25

def fetch(key)
  if has?(key)
    item = @data['properties'][key.to_s]
    cast item['val'], item['type']
  elsif block_given?
    yield
  end
end

#fetch_file(key) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/config_hub/client.rb', line 34

def fetch_file(key)
  if config_pulled? && @data['files'].present?
    @data.dig('files', key, 'content')
  else
    retrieve_remote_file(key)
  end
end

#has?(key) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
55
56
57
58
59
# File 'lib/config_hub/client.rb', line 52

def has?(key)
  if config_pulled?
    props = @data['properties']
    props && props.key?(key.to_s)
  else
    raise ConfigNotPulledError
  end
end

#pullObject



17
18
19
# File 'lib/config_hub/client.rb', line 17

def pull
  @data = retrieve_remote_config
end

#to_hObject



42
43
44
45
46
47
48
49
50
# File 'lib/config_hub/client.rb', line 42

def to_h
  if config_pulled?
    @data['properties'].reduce({}) do |hash, (k, v)|
      hash.merge(k => v['val'])
    end
  else
    raise ConfigNotPulledError
  end
end