Class: GlassOctopus::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/glass_octopus/configuration.rb

Overview

Configuration for the application.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration.



16
17
18
# File 'lib/glass_octopus/configuration.rb', line 16

def initialize
  self.logger = Logger.new(STDOUT).tap { |l| l.level = Logger::INFO }
end

Instance Attribute Details

#connection_adapterObject (readonly)

The configured connection adapter.

See Also:



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/glass_octopus/configuration.rb', line 12

class Configuration
  attr_accessor :logger
  attr_reader :connection_adapter

  def initialize
    self.logger = Logger.new(STDOUT).tap { |l| l.level = Logger::INFO }
  end

  # Configures a new adapter.
  #
  # When a class is passed as +type+ the class will be instantiated.
  #
  # @example Using a custom adapter class
  #   config.adapter(MyAdapter) do |c|
  #     c.bootstrap_servers = %w[localhost:9092]
  #     c.group_id = "mygroup"
  #     c.topic = "mytopic"
  #   end
  #
  #   class MyAdapter
  #     def initialize
  #       @options = OpenStruct.new
  #       yield @options
  #     end
  #
  #     def fetch_message
  #       @consumer.each do |fetched_message|
  #         message = Message.new(
  #           fetched_message.topic,
  #          fetched_message.partition,
  #          fetched_message.offset,
  #          fetched_message.key,
  #          fetched_message.value
  #        )
  #
  #         yield message
  #       end
  #     end
  #
  #     def connect
  #       # Connect to Kafka...
  #       @consumer = ...
  #       self
  #     end
  #
  #     def close
  #       @consumer.close
  #     end
  #   end
  #
  # @param type [:ruby_kafka, Class] type of the adapter to use
  # @yield a block to conigure the adapter
  # @yieldparam config configuration object
  #
  # @see RubyKafkaAdapter
  def adapter(type, &block)
    @connection_adapter = build_adapter(type, &block)
  end

  # @api private
  def build_adapter(type, &block)
    case type
    when :ruby_kafka
      require "glass_octopus/connection/ruby_kafka_adapter"
      RubyKafkaAdapter.new(logger, &block)
    when Class
      type.new(&block)
    else
      raise ArgumentError, "Unknown adapter: #{type}"
    end
  end
end

#loggerObject

A standard library compatible logger for the application. By default it logs to the STDOUT.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/glass_octopus/configuration.rb', line 12

class Configuration
  attr_accessor :logger
  attr_reader :connection_adapter

  def initialize
    self.logger = Logger.new(STDOUT).tap { |l| l.level = Logger::INFO }
  end

  # Configures a new adapter.
  #
  # When a class is passed as +type+ the class will be instantiated.
  #
  # @example Using a custom adapter class
  #   config.adapter(MyAdapter) do |c|
  #     c.bootstrap_servers = %w[localhost:9092]
  #     c.group_id = "mygroup"
  #     c.topic = "mytopic"
  #   end
  #
  #   class MyAdapter
  #     def initialize
  #       @options = OpenStruct.new
  #       yield @options
  #     end
  #
  #     def fetch_message
  #       @consumer.each do |fetched_message|
  #         message = Message.new(
  #           fetched_message.topic,
  #          fetched_message.partition,
  #          fetched_message.offset,
  #          fetched_message.key,
  #          fetched_message.value
  #        )
  #
  #         yield message
  #       end
  #     end
  #
  #     def connect
  #       # Connect to Kafka...
  #       @consumer = ...
  #       self
  #     end
  #
  #     def close
  #       @consumer.close
  #     end
  #   end
  #
  # @param type [:ruby_kafka, Class] type of the adapter to use
  # @yield a block to conigure the adapter
  # @yieldparam config configuration object
  #
  # @see RubyKafkaAdapter
  def adapter(type, &block)
    @connection_adapter = build_adapter(type, &block)
  end

  # @api private
  def build_adapter(type, &block)
    case type
    when :ruby_kafka
      require "glass_octopus/connection/ruby_kafka_adapter"
      RubyKafkaAdapter.new(logger, &block)
    when Class
      type.new(&block)
    else
      raise ArgumentError, "Unknown adapter: #{type}"
    end
  end
end

Instance Method Details

#adapter(type) {|config| ... } ⇒ Object

Configures a new adapter.

When a class is passed as type the class will be instantiated.

Examples:

Using a custom adapter class

config.adapter(MyAdapter) do |c|
  c.bootstrap_servers = %w[localhost:9092]
  c.group_id = "mygroup"
  c.topic = "mytopic"
end

class MyAdapter
  def initialize
    @options = OpenStruct.new
    yield @options
  end

  def fetch_message
    @consumer.each do |fetched_message|
      message = Message.new(
        fetched_message.topic,
       fetched_message.partition,
       fetched_message.offset,
       fetched_message.key,
       fetched_message.value
     )

      yield message
    end
  end

  def connect
    # Connect to Kafka...
    @consumer = ...
    self
  end

  def close
    @consumer.close
  end
end

Parameters:

  • type (:ruby_kafka, Class)

    type of the adapter to use

Yields:

  • a block to conigure the adapter

Yield Parameters:

  • config

    configuration object

See Also:



67
68
69
# File 'lib/glass_octopus/configuration.rb', line 67

def adapter(type, &block)
  @connection_adapter = build_adapter(type, &block)
end