Class: Configly::Client
- Inherits:
-
Object
- Object
- Configly::Client
- Defined in:
- lib/configly/client.rb
Class Method Summary collapse
- .fetch(keys, use_ttls) ⇒ Object
- .generate_qs(keys_to_preload) ⇒ Object
- .get(key) ⇒ Object
- .get_api_key ⇒ Object
- .get_keys_to_preload ⇒ Object
- .init ⇒ Object
- .load_initial_data ⇒ Object
- .start_web_socket_client ⇒ Object
Class Method Details
.fetch(keys, use_ttls) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/configly/client.rb', line 65 def self.fetch(keys, use_ttls) current_time = Time.now.to_i # If we aren't using the TTLs, fetch all keys if !use_ttls keys_to_fetch = keys else # Otherwise, fetch the keys that are unfetched or expired keys_to_fetch = [] keys.each do |key| if !@ttl_expirations.has_key?(key) || @ttl_expirations[key] < current_time keys_to_fetch << key end end end # Only actually execute the request if there are keys to fetch if keys_to_fetch.length > 0 uri = URI("https://#{CONFIGLY_SERVER}#{CONFIGLY_VALUE_URL}?#{generate_qs(keys_to_fetch)}") Net::HTTP.start(uri.host, uri.port, :use_ssl => true) do |http| request = Net::HTTP::Get.new uri request['X-Lib-Version'] = "configly-ruby/#{Configly::VERSION}" request.basic_auth get_api_key, '' response = http.request request data = JSON.parse(response.body)['data'] data.keys.each do |key| # Set the key and the expiration @keys[key] = data[key]['value'] @ttl_expirations[key] = current_time + data[key]['ttl'] end end end end |
.generate_qs(keys_to_preload) ⇒ Object
29 30 31 |
# File 'lib/configly/client.rb', line 29 def self.generate_qs(keys_to_preload) return keys_to_preload.map { |key| "keys[]=#{key}" }.join("&") end |
.get(key) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/configly/client.rb', line 100 def self.get(key) if @use_ws if @keys.has_key? key return @keys[key] else raise KeyError.new(key) end else fetch([key], true) if @keys.has_key? key return @keys[key] else raise KeyError.new(key) end end end |
.get_api_key ⇒ Object
21 22 23 |
# File 'lib/configly/client.rb', line 21 def self.get_api_key return ENV['CONFIGLY_API_KEY'] end |
.get_keys_to_preload ⇒ Object
25 26 27 |
# File 'lib/configly/client.rb', line 25 def self.get_keys_to_preload return ENV['CONFIGLY_KEYS_TO_PRELOAD'].split(',') end |
.init ⇒ Object
13 14 15 16 17 18 19 |
# File 'lib/configly/client.rb', line 13 def self.init @use_ws = true if get_api_key load_initial_data Thread.new { EM.run { start_web_socket_client }} end end |
.load_initial_data ⇒ Object
33 34 35 |
# File 'lib/configly/client.rb', line 33 def self.load_initial_data fetch(get_keys_to_preload, false) end |
.start_web_socket_client ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/configly/client.rb', line 37 def self.start_web_socket_client ws = Faye::WebSocket::Client.new("ws://configly.herokuapp.com", nil, {:ping => 30}) ws.on :open do |event| Rails.logger.debug :open ws.send(JSON.generate({"apiKey" => get_api_key, "type" => "handshake"})) end ws.on :message do |event| Rails.logger.debug :message data = JSON.parse(event.data) if data["type"] == "configUpdate" payload = data["payload"]["payload"] keyToUpdate = payload['key'] value = payload['value'] @keys[keyToUpdate] = value end Rails.logger.debug @keys end ws.on :close do |event| Rails.logger.debug :close Rails.logger.debug event.code start_web_socket_client end end |