Sha256: 3b183ba9f74db85663a86ea682ca967838a904087aac36321355fc758ec01e74

Contents?: true

Size: 1.08 KB

Versions: 7

Compression:

Stored size: 1.08 KB

Contents

require 'excon'
require 'json'

module Featureflow
  class PollingClient
    DEFAULT_OPTIONS = {
      delay: 30,
      timeout: 30
    }.freeze
    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

    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
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
featureflow-0.3.7 lib/featureflow/polling_client.rb
featureflow-0.3.6 lib/featureflow/polling_client.rb
featureflow-0.3.5 lib/featureflow/polling_client.rb
featureflow-0.3.4 lib/featureflow/polling_client.rb
featureflow-0.3.2 lib/featureflow/polling_client.rb
featureflow-0.3.1 lib/featureflow/polling_client.rb
featureflow-0.3.0 lib/featureflow/polling_client.rb