Class: Connfu::ListenerChannel

Inherits:
Object
  • Object
show all
Includes:
ConnfuLogger
Defined in:
lib/connfu/listener_channel.rb

Overview

This class models a specific connFu listener channel. A connFu listener channel is the path to fetch events from your resources.

Current supported channel types:

  • :voice: inbound calls to a phone number mapped to the application

  • :sms: inbound sms to a phone number mapped to the application

  • :twitter: tweet messages that matches at least one of the rules defined by the application

  • :rss: atom or rss feed

Constant Summary collapse

CHANNEL_TYPES =

Constant that defines the supported channels

[:voice, :twitter, :sms, :rss]
EVENT_TYPES =

Constant that defines the supported channel events

[:new, :join, :leave, :new_topic]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ConnfuLogger

included, #logger

Constructor Details

#initialize(name) ⇒ ListenerChannel

Channel initializer

Parameters

  • name channel name (CHANNELS item)



41
42
43
44
45
# File 'lib/connfu/listener_channel.rb', line 41

def initialize(name)
  @name = name
  # initialize an Struct that shall keep the event blocks. There is a specific key per each event type
  @blocks = Struct.new(*EVENT_TYPES).new(*Array.new(EVENT_TYPES.length){Array.new()})
end

Instance Attribute Details

#_filterObject (readonly)

how to filter inbound events from connFu



34
35
36
# File 'lib/connfu/listener_channel.rb', line 34

def _filter
  @_filter
end

#blocksObject

blocks associated to the channel. Hash object (key = event_type, value = Proc)



31
32
33
# File 'lib/connfu/listener_channel.rb', line 31

def blocks
  @blocks
end

#nameObject (readonly)

channel name (a CHANNELS item)



28
29
30
# File 'lib/connfu/listener_channel.rb', line 28

def name
  @name
end

Class Method Details

.valid?(name) ⇒ Boolean

Checks if a channel name is valid

Parameters

  • name channel name

Return

true if the channel name if valid, false if not

Returns:

  • (Boolean)


93
94
95
# File 'lib/connfu/listener_channel.rb', line 93

def valid?(name)
  CHANNEL_TYPES.include?(name)
end

Instance Method Details

#add_blockObject



75
76
77
78
79
80
81
82
83
84
# File 'lib/connfu/listener_channel.rb', line 75

def add_block
  block_given? and self.instance_exec self, &Proc.new

  #
  # Avoid using instance_exec: we can directly call the Proc received
  # but we need to change the expectations regarding context "messages"
  # @connfu.listener_channels[:foo].should... => @connfu.should
  #
  #block_given? and Proc.new.call(self)
end

#filterObject



71
72
73
# File 'lib/connfu/listener_channel.rb', line 71

def filter
  @_filter
end

#filter=(value) ⇒ Object



65
66
67
68
69
# File 'lib/connfu/listener_channel.rb', line 65

def filter=(value)
  @_filter = value
  # TODO
  # connFu provisioning request
end

#message(event, arg = nil) ⇒ Object



59
60
61
62
63
# File 'lib/connfu/listener_channel.rb', line 59

def message(event, arg = nil)
  @blocks[event].each { |block|
    block.call(arg)
  }
end

#on(event) ⇒ Object

Method that defines a new event in a channel

Parameters

  • event event type. Must be included in EVENT_TYPES



52
53
54
55
# File 'lib/connfu/listener_channel.rb', line 52

def on(event)
  EVENT_TYPES.include?(event) or (self.class.logger.error "Invalid event #{event}" and return)
  block_given? and @blocks[event] << Proc.new
end