Sha256: f8d2b8495490c2db5bb7727f9fc0910c058eee5dc94e4b37de2a04117ae8251b

Contents?: true

Size: 1.5 KB

Versions: 3

Compression:

Stored size: 1.5 KB

Contents

require 'forwardable'
require 'chillout/server_side'
require 'chillout/http_client'
require 'chillout/dispatcher'
require 'chillout/config'
require 'chillout/event_data_builder'
require 'chillout/prefixed_logger'
require 'chillout/worker'
require 'chillout/check_result'
require 'thread'

module Chillout
  class Client
    extend Forwardable

    def_delegators :@dispatcher, :send_creations, :check_api_connection

    attr_reader :config
    attr_reader :logger
    attr_reader :queue

    def initialize(config_or_api_key, options = {})
      build_config(config_or_api_key, options)

      yield @config if block_given?

      @logger = PrefixedLogger.new("Chillout", @config.logger)

      @http_client = HttpClient.new(@config, logger).freeze
      @event_data_builder = EventDataBuilder.new(@config).freeze
      @server_side = ServerSide.new(@event_data_builder, @http_client).freeze
      @dispatcher = Dispatcher.new(@filter, @server_side).freeze
      @queue = Queue.new
    end

    def enqueue(creations)
      @queue << creations
    end

    def start_worker
      thread = Thread.new do
        worker = Worker.new(@dispatcher, @queue, @logger)
        worker.run
      end
    end

    private
    def build_config(config_or_api_key, options)
      case config_or_api_key
      when Config
        @config = config_or_api_key
      when String
        @config = Config.new(config_or_api_key)
      else
        raise ArgumentError.new("Invalid config passed")
      end
      @config.update(options)
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
chillout-0.5.0 lib/chillout/client.rb
chillout-0.4.1 lib/chillout/client.rb
chillout-0.4.0 lib/chillout/client.rb