Class: Propono::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/propono/components/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(settings = {}, &block) ⇒ Client

Returns a new instance of Client.



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/propono/components/client.rb', line 23

def initialize(settings = {}, &block)
  @config = Configuration.new
  if block_given?
    configure(&block)
  else
    settings.each do |key, value|
      config.send("#{key}=", value)
    end
  end

  @aws_client = AwsClient.new(AwsConfig.new(config))
end

Instance Attribute Details

#aws_clientObject (readonly)

Propono configuration settings.

Settings should be set in an initializer or using some other method that insures they are set before any Propono code is used. They can be set as followed:

Propono.config.access_key = "my-access-key"

The following settings are allowed:

  • :access_key - The AWS access key

  • :secret_key - The AWS secret key

  • :queue_region - The AWS region

  • :application_name - The name of the application Propono is included in.

  • :queue_suffix - Optional string to append to topic and queue names.

  • :logger - A logger object that responds to puts.



22
23
24
# File 'lib/propono/components/client.rb', line 22

def aws_client
  @aws_client
end

#configObject (readonly)

Propono configuration settings.

Settings should be set in an initializer or using some other method that insures they are set before any Propono code is used. They can be set as followed:

Propono.config.access_key = "my-access-key"

The following settings are allowed:

  • :access_key - The AWS access key

  • :secret_key - The AWS secret key

  • :queue_region - The AWS region

  • :application_name - The name of the application Propono is included in.

  • :queue_suffix - Optional string to append to topic and queue names.

  • :logger - A logger object that responds to puts.



22
23
24
# File 'lib/propono/components/client.rb', line 22

def config
  @config
end

Instance Method Details

#configure {|config| ... } ⇒ Object

Yields:



36
37
38
# File 'lib/propono/components/client.rb', line 36

def configure
  yield config
end

#drain_queue(topic, &message_processor) ⇒ Object

Listens on a queue and yields for each message

Calling this will enter a queue-listening loop that yields the message_processor for each messages. The loop will end when all messages have been processed.

This method will automatically create a subscription if one does not exist, so there is no need to call subscribe in addition.

Parameters:

  • topic (String)

    The topic to subscribe to.

  • &message_processor

    The block to yield for each message.



88
89
90
# File 'lib/propono/components/client.rb', line 88

def drain_queue(topic, &message_processor)
  QueueListener.drain(aws_client, config, topic, &message_processor)
end

#listen(topic_name, options = {}, &message_processor) ⇒ Object

Listens on a queue and yields for each message

Calling this will enter a queue-listening loop that yields the message_processor for each messages.

This method will automatically create a subscription if one does not exist, so there is no need to call subscribe in addition.

Parameters:

  • topic (String)

    The topic to subscribe to.

  • &message_processor

    The block to yield for each message.



72
73
74
# File 'lib/propono/components/client.rb', line 72

def listen(topic_name, options = {}, &message_processor)
  QueueListener.listen(aws_client, config, topic_name, options, &message_processor)
end

#publish(topic, message, options = {}) ⇒ Object

Publishes a new message into the Propono pub/sub network.

This requires a topic and a message. By default this pushes out AWS SNS.

Parameters:

  • topic (String)

    The name of the topic to publish to.

  • message (String)

    The message to post.



47
48
49
50
# File 'lib/propono/components/client.rb', line 47

def publish(topic, message, options = {})
  suffixed_topic = "#{topic}#{config.queue_suffix}"
  Publisher.publish(aws_client, config, suffixed_topic, message, options)
end

#subscribe(topic) ⇒ Object

Creates a new SNS-SQS subscription on the specified topic.

This is implicitly called by #listen.

Parameters:

  • topic (String)

    The name of the topic to subscribe to.



57
58
59
# File 'lib/propono/components/client.rb', line 57

def subscribe(topic)
  QueueSubscription.create(aws_client, config, topic)
end