Class: Apollo::Client
- Inherits:
-
Object
- Object
- Apollo::Client
- Defined in:
- lib/apollo/client.rb
Overview
Get configuration data from Apollo server.
Instance Attribute Summary collapse
-
#configuration ⇒ Object
Returns the value of attribute configuration.
Instance Method Summary collapse
- #build_http_headers(url) ⇒ Object
- #cached_http_get(key) ⇒ Object
- #fetch(key, disable_cache) ⇒ Object
-
#initialize(configuration) ⇒ Client
constructor
A new instance of Client.
- #uncached_http_get(key) ⇒ Object
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
#configuration ⇒ Object
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) = 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, , path_with_query) { "Authorization" => "Apollo #{configuration.appid}:#{signature}", "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 |