class Featureflow::PollingClient

Constants

DEFAULT_OPTIONS
LOCK

Public Class Methods

new(url, api_key, options = {}) click to toggle source
# File lib/featureflow/polling_client.rb, line 12
def initialize(url, api_key, options = {})
  @etag = ''
  @url = url
  @api_key = api_key
  @options = DEFAULT_OPTIONS.merge(options)
  @features = {}

  load_features

  @thread = Thread.new do
    loop do
      sleep @options[:poll_interval]
      load_features
    end
  end
end

Public Instance Methods

feature(key) click to toggle source
# File lib/featureflow/polling_client.rb, line 33
def feature(key)
  LOCK.synchronize { @features[key] }
end
finish() click to toggle source
# File lib/featureflow/polling_client.rb, line 29
def finish
  @thread.exit
end
load_features() click to toggle source
# File lib/featureflow/polling_client.rb, line 37
def load_features
  response = Excon.get(@url +  + '/api/sdk/v1/features', headers: {
    'Authorization' => "Bearer #{@api_key}",
    'Accept' => 'Application/Json',
    'If-None-Match' => @etag,
    'X-Featureflow-Client' => 'RubyClient/' + Featureflow::VERSION
  }, omit_default_port: true, read_timeout: @options[:timeout])

  if response.status == 200
    Featureflow.logger.debug "updating features"

    @etag = response.headers['ETag']

    features = JSON.parse(response.body)

    LOCK.synchronize { @features = features }
  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