Class: Doppler::Client

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

Constant Summary collapse

@@host_key =
'DOPPLER_HOST'
@@default_host =
'https://api.doppler.market'
@@environ_segment =
'/environments/'
@@max_retries =
0

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)


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

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
    @host = ENV[@@host_key] ? ENV[@@host_key] : @@default_host
    
    startup()
end

Instance Method Details

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

Raises:

  • (ArgumentError)


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
118
119
120
121
122
123
124
# File 'lib/doppler/client.rb', line 93

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

    }
    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



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

def get(key_name, priority = nil)
    priority = priority != nil ? priority : @default_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
    
    if not @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



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

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