Class: Doppler::Client

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

Instance Method Summary collapse

Constructor Details

#initialize(api_key, pipeline, environment, priority = Priority.remote, track_keys = [], ignore_keys = []) ⇒ Client

Returns a new instance of Client.

Raises:

  • (ArgumentError)


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/doppler.rb', line 22

def initialize(api_key, pipeline, environment, priority = Priority.remote, track_keys = [], ignore_keys = [])
    raise ArgumentError, 'api_key not string' unless api_key.is_a? String
    raise ArgumentError, 'pipeline not string' unless pipeline.is_a? String
    raise ArgumentError, 'api_key not string' unless environment.is_a? String
    raise ArgumentError, 'priority not numeric' unless priority.is_a? Numeric 
    raise ArgumentError, 'track_keys not array' unless track_keys.is_a? Array 
    raise ArgumentError, 'ignore_keys not array' unless ignore_keys.is_a? Array 

    @api_key = api_key
    @pipeline = pipeline
    @environment = environment
    @default_priority = priority
    @track_keys = track_keys.to_set
    @ignore_keys = ignore_keys.to_set
    @max_retries = 10
    @environ_segment = '/environments/'
    @default_host = 'https://api.doppler.com'
    @host = ENV['DOPPLER_HOST'].nil? ? @default_host : ENV['DOPPLER_HOST']
            
    startup()
end

Instance Method Details

#_request(endpoint, body, retry_count = 0) ⇒ Object

Raises:

  • (ArgumentError)


94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/doppler.rb', line 94

def _request(endpoint, body, retry_count=0)
    raise ArgumentError, 'endpoint not string' unless endpoint.is_a? String

    raw_url = @host + @environ_segment + @environment + endpoint
    uri = URI.parse(raw_url)
    header = {
        'Content-Type': 'application/json',
        'api-key': @api_key,
        'pipeline': @pipeline,
        'client-sdk': 'ruby',
        'client-version': Doppler::VERSION

    }
    http = Net::HTTP.new(uri.host, uri.port)
    http.use_ssl = true

    begin
        response = http.post(uri.path, body.to_json, header)
        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
            raise e
        else
            return _request(endpoint, body, retry_count)
        end
    end

   return response_data
end

#get(key_name, priority = nil) ⇒ Object



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
# File 'lib/doppler.rb', line 63

def get(key_name, priority = nil)
    priority = priority.nil? ? @default_priority : priority
    value = nil
    
    if priority == Priority.local
        value = ENV[key_name] ? ENV[key_name] : @remote_keys[key_name]
    else
        value = @remote_keys[key_name] ? @remote_keys[key_name] : ENV[key_name]
    end
    
    unless @ignore_keys.include?(key_name)
          if !value.nil?
            if ENV[key_name] != @remote_keys[key_name]
                local_keys = {}
                local_keys[key_name] = ENV[key_name]
                
                _request('/track_key', {
                    'local_keys' => local_keys
                })
            end
        else
            _request('/missing_key', {
                'key_name' => key_name
            })
            
        end
    end

    return value
end

#startupObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/doppler.rb', line 44

def startup
    keys_to_send = {}
    local_keys = ENV.to_hash
    
    if @send_local_keys
        local_keys.each do |key, value|                
            if @track_keys.include?(key)
                keys_to_send[key] = value
            end  
        end
    end
    
    resp = self._request('/fetch_keys', {
        'local_keys' => keys_to_send
    })

    @remote_keys = resp['keys']
end