Class: Doppler::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/doppler/client.rb

Constant Summary collapse

MAX_RETRIES =
10

Instance Method Summary collapse

Constructor Details

#initializeClient

Returns a new instance of Client.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 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
     
  startup()
end

Instance Method Details

#_request(endpoint, retry_count = 0) ⇒ Object

Raises:

  • (ArgumentError)


61
62
63
64
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
99
100
101
102
103
104
105
106
# File 'lib/doppler/client.rb', line 61

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

#overwrite_envObject



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/doppler/client.rb', line 35

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

#startupObject



26
27
28
29
30
31
32
# File 'lib/doppler/client.rb', line 26

def startup
  resp = self._request('/v1/variables')
  @remote_keys = resp.fetch("variables")
  
  overwrite_env()
  write_to_backup()
end

#write_to_backupObject



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/doppler/client.rb', line 48

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