Class: Featureflow::PollingClient

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

Constant Summary collapse

DEFAULT_OPTIONS =
{
  delay: 30,
  timeout: 30
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(url, api_key, options = {}, &set_features) ⇒ PollingClient

Returns a new instance of PollingClient.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/featureflow/polling_client.rb', line 10

def initialize(url, api_key, options = {}, &set_features)
  @etag = ''
  @url = url
  @api_key = api_key
  @options = DEFAULT_OPTIONS.merge(options)
  @set_features = set_features

  load_features
  Thread.new do
    loop do
      sleep @options[:delay]
      load_features
    end
  end
end

Instance Method Details

#load_featuresObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/featureflow/polling_client.rb', line 26

def load_features
  response = Excon.get(@url, headers: {
    'Authorization' => "Bearer #{@api_key}",
    'If-None-Match' => @etag
  }, omit_default_port: true, read_timeout: @options[:timeout])
  if response.status == 200
    @etag = response.headers['ETag']
    @set_features.call(JSON.parse(response.body))
  elsif response.status >= 400
    Featureflow.logger.error "request for features failed with response status #{response.status}"
    Featureflow.logger.error response.to_s
  end
rescue => e
  Featureflow.logger.error e.inspect
end