Class: Apollo::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/apollo/client.rb

Overview

Get configuration data from Apollo server.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configuration) ⇒ Client

Returns a new instance of Client.



7
8
9
# File 'lib/apollo/client.rb', line 7

def initialize(configuration)
  @configuration = configuration
end

Instance Attribute Details

#configurationObject

Returns the value of attribute configuration.



5
6
7
# File 'lib/apollo/client.rb', line 5

def configuration
  @configuration
end

Instance Method Details

#build_http_headers(url) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/apollo/client.rb', line 39

def build_http_headers(url)
  timestamp = Time.now.utc.strftime("%s%L")
  uri = Utils.safe_uri_parse(url)
  path_with_query = uri.path
  path_with_query += "?#{uri.query}" unless uri.query.nil?
  signature = Auth.signature(configuration.secret, timestamp, path_with_query)
  {
    "Authorization" => "Apollo #{configuration.appid}:#{signature}",
    "Timestamp" => timestamp
  }
end

#cached_http_get(key) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/apollo/client.rb', line 19

def cached_http_get(key)
  url = "#{configuration.server}/configfiles/json/#{configuration.appid}/#{configuration.cluster}/#{configuration.namespace}"
  headers = build_http_headers(url)
  code, data, _headers = HTTP.get(url, headers)
  return unless HTTP.response_ok?(code)

  configurations = Utils.safe_json_parse(data)
  configurations[key]
end

#fetch(key, disable_cache) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/apollo/client.rb', line 11

def fetch(key, disable_cache)
  if disable_cache
    uncached_http_get(key)
  else
    cached_http_get(key)
  end
end

#uncached_http_get(key) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/apollo/client.rb', line 29

def uncached_http_get(key)
  url = "#{configuration.server}/configs/#{configuration.appid}/#{configuration.cluster}/#{configuration.namespace}"
  headers = build_http_headers(url)
  code, data, _headers = HTTP.get(url, headers)
  return unless HTTP.response_ok?(code)

  configurations = Utils.safe_json_parse(data)["configurations"]
  configurations[key]
end