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.



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

def initialize(configuration)
  @configuration = configuration
end

Instance Attribute Details

#configurationObject

Returns the value of attribute configuration.



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

def configuration
  @configuration
end

Instance Method Details

#build_http_headers(url) ⇒ Object



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

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



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

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



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

def fetch(key, disable_cache)
  return nil if [configuration.server, configuration.appid, configuration.secret].any?(&:nil?)

  disable_cache ? uncached_http_get(key) : cached_http_get(key)
end

#uncached_http_get(key) ⇒ Object



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

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