Module: Connfu::DSL

Includes:
ConnfuLogger
Defined in:
lib/connfu/dsl.rb

Overview

This class parses a connFu application defined using the DSL i.e.

Connfu.application(VALID_TOKEN, ENDPOINT) {
   listen(:voice) do |conference| {
      conference.on(:join) do |call|
        puts "New inbound call from #{call[:from]} on number #{call[:to]}"
      end
  }
   listen(:twitter) do |conference| {
     twitter.on(:new) do |tweet|
       puts "#{tweet[:channel_name]} just posted a new tweet: #{tweet.content}"
     end
  }
}

Class Method Summary collapse

Methods included from ConnfuLogger

included, #logger

Class Method Details

.listen(name, args = {}) ⇒ Object

This method is called while parsing the application logic and a new channel is defined. It inserts a new ListenerChannels in the listener_channels attribute with the Proc associated to each event type.

i.e.

 listen(:voice) do |conference| {
    conference.on(:join) do |call|
      puts "New inbound call from #{call[:from]} on number #{call[:to]}"
    end
}

While parsing this snippet of code, a new listener_channel will be stored, indicating that when a new join event associated to a voice channel is received, the line

puts "New inbound call from #{call[:from]} on number #{call[:to]}"

must be executed.

Parameters

  • name channel name

  • args actually not in used



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/connfu/dsl.rb', line 65

def listen(name, args = {})

  unless ListenerChannel.valid?(name)
    logger.error "Invalid channel name #{name}"
    return nil
  end

  # Create a new channel listener
  listener_channel = Connfu::ListenerChannel.new(name)

  # If defined, insert into the channel the desired logic
  block_given? and listener_channel.add_block(&Proc.new)

  # Add channel to channels array
  @listener_channels.store(name, listener_channel)
end

.runObject

Parses the given block

Return

Hash of values containing as key the listener channel type and as value the blocks to be executed when an event associated to that channel is received



32
33
34
35
36
37
38
39
40
# File 'lib/connfu/dsl.rb', line 32

def run
  @listener_channels = {}
  if block_given?
    self.instance_exec(&Proc.new)
    @listener_channels
  else
    nil
  end
end