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.
7 8 9 |
# File 'lib/apollo/client.rb', line 7 def initialize(configuration) @configuration = configuration end |
Instance Attribute Details
#configuration ⇒ Object
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) = 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
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 |