Class: LocalGateway
- Inherits:
-
Object
- Object
- LocalGateway
- 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
-
#address ⇒ Object
readonly
Returns the value of attribute address.
-
#password ⇒ Object
readonly
Returns the value of attribute password.
-
#protocol ⇒ Object
readonly
Returns the value of attribute protocol.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
-
#user ⇒ Object
readonly
Returns the value of attribute user.
Class Method Summary collapse
-
.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.
Instance Method Summary collapse
-
#initialize(url, user, password, address = self.class.default_address, protocol = self.class.default_protocol) ⇒ LocalGateway
constructor
Creates a new local gateway for an existing qst server channel.
-
#receive_aos ⇒ Object
Recieves AOs from nuntium.
-
#send_at(text, options) ⇒ Object
Sends an AT message to nuntium.
-
#send_ats(messages) ⇒ Object
Sends many AT messages.
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
#address ⇒ Object (readonly)
Returns the value of attribute address.
21 22 23 |
# File 'lib/local_gateway.rb', line 21 def address @address end |
#password ⇒ Object (readonly)
Returns the value of attribute password.
24 25 26 |
# File 'lib/local_gateway.rb', line 24 def password @password end |
#protocol ⇒ Object (readonly)
Returns the value of attribute protocol.
25 26 27 |
# File 'lib/local_gateway.rb', line 25 def protocol @protocol end |
#url ⇒ Object (readonly)
Returns the value of attribute url.
22 23 24 |
# File 'lib/local_gateway.rb', line 22 def url @url end |
#user ⇒ Object (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.
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_aos ⇒ Object
Recieves AOs from nuntium.
57 58 59 60 61 62 63 64 65 |
# File 'lib/local_gateway.rb', line 57 def receive_aos = if @last_received_id.nil? @client. else @client. :from_id => @last_received_id end @last_received_id = .last['id'] unless .empty? 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, ) = {'text' => text, 'to' => [:to]} send_ats 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() = [] unless .kind_of?(Array) = .map do |m| = m.clone ['from'] ||= self.address ['from'] = with_protocol(['from'] || [:from]) ['to'] = with_protocol(['to'] || [:to]) end @client.() end |