Class: Doppler::Client
- Inherits:
-
Object
- Object
- Doppler::Client
- Defined in:
- lib/doppler/client.rb
Constant Summary collapse
- MAX_RETRIES =
10
Instance Method Summary collapse
- #_request(endpoint, retry_count = 0) ⇒ Object
- #get(name) ⇒ Object
- #get_all ⇒ Object
-
#initialize ⇒ Client
constructor
A new instance of Client.
- #overwrite_env ⇒ Object
- #startup ⇒ Object
- #write_to_backup ⇒ Object
Constructor Details
#initialize ⇒ Client
Returns a new instance of Client.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/doppler/client.rb', line 9 def initialize() if Doppler.api_key.nil? raise "Please provide a api key" end if Doppler.pipeline.nil? raise "Please provide a pipeline" end if Doppler.environment.nil? raise "Please provide a environment" end print "DEPRECATED: Please use the new CLI at https://docs.doppler.com/docs/enclave-installation\n\n" startup() end |
Instance Method Details
#_request(endpoint, retry_count = 0) ⇒ Object
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 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/doppler/client.rb', line 72 def _request(endpoint, retry_count=0) raise ArgumentError, 'endpoint not string' unless endpoint.is_a? String uri = URI.parse(Doppler.host_url + endpoint) uri.query = URI.encode_www_form({ 'pipeline': Doppler.pipeline, 'environment': Doppler.environment }) header = { 'Content-Type': 'application/json', 'api-key': Doppler.api_key, 'client-sdk': 'ruby', 'client-version': Doppler::VERSION } begin request = Net::HTTP::Get.new(uri, header) response = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => true) {|http| http.request(request) } response_data = JSON.parse(response.body) if response_data['success'] == false raise RuntimeError, response_data["messages"].join(". ") end rescue => e retry_count += 1 if retry_count > MAX_RETRIES backup_env = Doppler.read_env(Doppler.backup_filepath) if backup_env.nil? raise e end data = {} data["variables"] = backup_env return data else return _request(endpoint, retry_count) end end return response_data end |
#get(name) ⇒ Object
64 65 66 |
# File 'lib/doppler/client.rb', line 64 def get(name) return @remote_keys.fetch(name) end |
#get_all ⇒ Object
68 69 70 |
# File 'lib/doppler/client.rb', line 68 def get_all return @remote_keys end |
#overwrite_env ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/doppler/client.rb', line 38 def overwrite_env if @remote_keys.nil? return end @remote_keys.each do |key, value| unless Doppler.ignore_variables.include?(key) ENV[key] = value end end end |
#startup ⇒ Object
27 28 29 30 31 32 33 34 35 |
# File 'lib/doppler/client.rb', line 27 def startup resp = self._request('/v1/variables') @remote_keys = resp.fetch("variables") write_to_backup() if Doppler.override overwrite_env() end end |
#write_to_backup ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/doppler/client.rb', line 51 def write_to_backup unless Doppler.backup_filepath.nil? file = File.open(Doppler.backup_filepath, "w") @remote_keys.each do |key, value| file.puts key + "=" + value end file.close end end |