Class: LocalGateway

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

Overview

A local gateway that sends ATs and receives AOs.

Usage:

# Ask for a ticket.
# Returns the welcome message and the newly created local gateway.
msg, lgw = LocalGateway.with_automatic_configuration_for do |ticket_number|
  # Create nuntium channel with ticket number
end

lgw.send_at 'Hello!', to: '1234'

aos = lgw.receive_aos

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, user, password, address = self.class.default_address, protocol = self.class.default_protocol) ⇒ LocalGateway

Creates a new local gateway for an existing qst server channel.



28
29
30
31
32
33
34
35
36
# File 'lib/local_gateway.rb', line 28

def initialize(url, user, password, address = self.class.default_address, protocol = self.class.default_protocol)
  @address = address
  @url = url
  @user = user
  @password = password
  @protocol = protocol
  @client = QstClient.new(url, user, password)
  return self
end

Instance Attribute Details

#addressObject (readonly)

Returns the value of attribute address.



21
22
23
# File 'lib/local_gateway.rb', line 21

def address
  @address
end

#passwordObject (readonly)

Returns the value of attribute password.



24
25
26
# File 'lib/local_gateway.rb', line 24

def password
  @password
end

#protocolObject (readonly)

Returns the value of attribute protocol.



25
26
27
# File 'lib/local_gateway.rb', line 25

def protocol
  @protocol
end

#urlObject (readonly)

Returns the value of attribute url.



22
23
24
# File 'lib/local_gateway.rb', line 22

def url
  @url
end

#userObject (readonly)

Returns the value of attribute user.



23
24
25
# File 'lib/local_gateway.rb', line 23

def user
  @user
end

Class Method Details

.with_automatic_configuration_for(url = 'http://nuntium.instedd.org/', address = default_address, protocol = default_protocol) {|response['code']| ... } ⇒ Object

Creates a new local gateway that asks for a ticket number, yields it to the block, then polls the ticket status. You must normally create a nuntium channel inside the given block.

Yields:

  • (response['code'])


40
41
42
43
44
45
46
# File 'lib/local_gateway.rb', line 40

def self.with_automatic_configuration_for(url='http://nuntium.instedd.org/', address = default_address, protocol = default_protocol)
  response = request_configuration_code url, address
  yield response['code']
  data = poll_configuration_status response['code'], response['secret_key'], url
  url = URI::join(url, "/#{data['account']}/qst").to_s
  [data['message'], self.new(url, data['channel'], data['password'])]
end

Instance Method Details

#receive_aosObject

Recieves AOs from nuntium.



57
58
59
60
61
62
63
64
65
# File 'lib/local_gateway.rb', line 57

def receive_aos
  messages = if @last_received_id.nil?
               @client.get_messages
             else
               @client.get_messages :from_id => @last_received_id
             end
  @last_received_id = messages.last['id'] unless messages.empty?
  messages
end

#send_at(text, options) ⇒ Object

Sends an AT message to nuntium.

lgw.send_at 'Hello!', to: '1234'


51
52
53
54
# File 'lib/local_gateway.rb', line 51

def send_at(text, options)
  message = {'text' => text, 'to' => options[:to]}
  send_ats message
end

#send_ats(messages) ⇒ Object

Sends many AT messages.

send_ats [{from: '123', to: '4656', text: 'Hello'}, ...]


70
71
72
73
74
75
76
77
78
79
80
# File 'lib/local_gateway.rb', line 70

def send_ats(messages)
  messages = [messages] unless messages.kind_of?(Array)
  messages = messages.map do |m|
    message = m.clone
    message['from'] ||= self.address
    message['from'] = with_protocol(message['from'] || message[:from])
    message['to'] = with_protocol(message['to'] || message[:to])
    message
  end
  @client.put_messages(messages)
end