local_gateway 0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/local_gateway.rb +111 -0
- data/lib/ruby-local-gateway.rb +1 -0
- metadata +80 -0
@@ -0,0 +1,111 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'qst_client'
|
3
|
+
require 'rest_client'
|
4
|
+
require 'uri'
|
5
|
+
require 'json'
|
6
|
+
|
7
|
+
# A local gateway that sends ATs and receives AOs.
|
8
|
+
#
|
9
|
+
# Usage:
|
10
|
+
#
|
11
|
+
# # Ask for a ticket.
|
12
|
+
# # Returns the welcome message and the newly created local gateway.
|
13
|
+
# msg, lgw = LocalGateway.with_automatic_configuration_for do |ticket_number|
|
14
|
+
# # Create nuntium channel with ticket number
|
15
|
+
# end
|
16
|
+
#
|
17
|
+
# lgw.send_at 'Hello!', to: '1234'
|
18
|
+
#
|
19
|
+
# aos = lgw.receive_aos
|
20
|
+
class LocalGateway
|
21
|
+
attr_reader :address
|
22
|
+
attr_reader :url
|
23
|
+
attr_reader :user
|
24
|
+
attr_reader :password
|
25
|
+
attr_reader :protocol
|
26
|
+
|
27
|
+
# Creates a new local gateway for an existing qst server channel.
|
28
|
+
def initialize(url, user, password, address = self.class.default_address, protocol = self.class.default_protocol)
|
29
|
+
@address = address
|
30
|
+
@url = url
|
31
|
+
@user = user
|
32
|
+
@password = password
|
33
|
+
@protocol = protocol
|
34
|
+
@client = QstClient.new(url, user, password)
|
35
|
+
return self
|
36
|
+
end
|
37
|
+
|
38
|
+
# Creates a new local gateway that asks for a ticket number, yields it to the block,
|
39
|
+
# then polls the ticket status. You must normally create a nuntium channel inside the given block.
|
40
|
+
def self.with_automatic_configuration_for(url='http://nuntium.instedd.org/', address = default_address, protocol = default_protocol)
|
41
|
+
response = request_configuration_code url, address
|
42
|
+
yield response['code']
|
43
|
+
data = poll_configuration_status response['code'], response['secret_key'], url
|
44
|
+
url = URI::join(url, "/#{data['account']}/qst").to_s
|
45
|
+
[data['message'], self.new(url, data['channel'], data['password'])]
|
46
|
+
end
|
47
|
+
|
48
|
+
# Sends an AT message to nuntium.
|
49
|
+
#
|
50
|
+
# lgw.send_at 'Hello!', to: '1234'
|
51
|
+
def send_at(text, options)
|
52
|
+
message = {'text' => text, 'to' => options[:to]}
|
53
|
+
send_ats message
|
54
|
+
end
|
55
|
+
|
56
|
+
# Recieves AOs from nuntium.
|
57
|
+
def receive_aos
|
58
|
+
messages = if @last_received_id.nil?
|
59
|
+
@client.get_messages
|
60
|
+
else
|
61
|
+
@client.get_messages :from_id => @last_received_id
|
62
|
+
end
|
63
|
+
@last_received_id = messages.last['id'] unless messages.empty?
|
64
|
+
messages
|
65
|
+
end
|
66
|
+
|
67
|
+
# Sends many AT messages.
|
68
|
+
#
|
69
|
+
# send_ats [{from: '123', to: '4656', text: 'Hello'}, ...]
|
70
|
+
def send_ats(messages)
|
71
|
+
messages = [messages] unless messages.kind_of?(Array)
|
72
|
+
messages = messages.map do |m|
|
73
|
+
message = m.clone
|
74
|
+
message['from'] ||= self.address
|
75
|
+
message['from'] = with_protocol(message['from'] || message[:from])
|
76
|
+
message['to'] = with_protocol(message['to'] || message[:to])
|
77
|
+
message
|
78
|
+
end
|
79
|
+
@client.put_messages(messages)
|
80
|
+
end
|
81
|
+
|
82
|
+
|
83
|
+
private
|
84
|
+
|
85
|
+
def with_protocol(address)
|
86
|
+
return nil if address.nil? || address.empty?
|
87
|
+
return "#{protocol}://#{address}" unless address.start_with?("#{protocol}://")
|
88
|
+
end
|
89
|
+
|
90
|
+
def self.request_configuration_code(url, address)
|
91
|
+
endpoint = URI::join(url, '/tickets.json').to_s
|
92
|
+
JSON.parse(RestClient.post(endpoint, :address => address))
|
93
|
+
end
|
94
|
+
|
95
|
+
def self.poll_configuration_status(code, secret_key, url)
|
96
|
+
endpoint = URI::join(url, "/tickets/#{code}.json").to_s
|
97
|
+
10.times do
|
98
|
+
response = JSON.parse(RestClient.get(endpoint, :params => {:secret_key => secret_key}))
|
99
|
+
return response['data'] if response['status'] == 'complete'
|
100
|
+
end
|
101
|
+
raise "Channel for ticket #{code} does not exist"
|
102
|
+
end
|
103
|
+
|
104
|
+
def self.default_protocol
|
105
|
+
'sms'
|
106
|
+
end
|
107
|
+
|
108
|
+
def self.default_address
|
109
|
+
'9990000'
|
110
|
+
end
|
111
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'local_gateway'
|
metadata
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: local_gateway
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: '0.1'
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- InsTEDD
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-11-11 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: qst_client
|
16
|
+
requirement: &70138644822120 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70138644822120
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: builder
|
27
|
+
requirement: &70138644821440 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :runtime
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70138644821440
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: rest-client
|
38
|
+
requirement: &70138644820680 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
type: :runtime
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *70138644820680
|
47
|
+
description: A Local Gateway -developed by InSTEDD- implemented in Ruby for testing
|
48
|
+
purposes.
|
49
|
+
email: aborenszweig@manas.com.ar
|
50
|
+
executables: []
|
51
|
+
extensions: []
|
52
|
+
extra_rdoc_files: []
|
53
|
+
files:
|
54
|
+
- lib/local_gateway.rb
|
55
|
+
- lib/ruby-local-gateway.rb
|
56
|
+
homepage: https://github.com/spalladino/ruby-local-gateway
|
57
|
+
licenses: []
|
58
|
+
post_install_message:
|
59
|
+
rdoc_options: []
|
60
|
+
require_paths:
|
61
|
+
- lib
|
62
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
63
|
+
none: false
|
64
|
+
requirements:
|
65
|
+
- - ! '>='
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '0'
|
68
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
69
|
+
none: false
|
70
|
+
requirements:
|
71
|
+
- - ! '>='
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: '0'
|
74
|
+
requirements: []
|
75
|
+
rubyforge_project:
|
76
|
+
rubygems_version: 1.8.10
|
77
|
+
signing_key:
|
78
|
+
specification_version: 3
|
79
|
+
summary: A Local Gateway implemented in Ruby
|
80
|
+
test_files: []
|