-
Notifications
You must be signed in to change notification settings - Fork 0
/
group.rb
134 lines (109 loc) · 2.95 KB
/
group.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# Couch wrapper from http://wiki.apache.org/couchdb/Getting_started_with_Ruby
require 'rubygems'
require 'net/http'
module Couch
class Server
def initialize(host, port, options = nil)
@host = host
@port = port
@options = options
end
def delete(uri)
request(Net::HTTP::Delete.new(uri))
end
def get(uri)
request(Net::HTTP::Get.new(uri))
end
def put(uri, json)
req = Net::HTTP::Put.new(uri)
req["content-type"] = "application/json"
req.body = json
request(req)
end
def post(uri, json)
req = Net::HTTP::Post.new(uri)
req["content-type"] = "application/json"
req.body = json
request(req)
end
def request(req)
res = Net::HTTP.start(@host, @port) { |http|http.request(req) }
unless res.kind_of?(Net::HTTPSuccess)
handle_error(req, res)
end
res
end
private
def handle_error(req, res)
e = RuntimeError.new("#{res.code}:#{res.message}\nMETHOD:#{req.method}\nURI:#{req.path}\n#{res.body}")
raise e
end
end
end
# end Couch wrapper
# Notifo wrapper
module Notifo
class Service
def initialize(name, secret, options = nil)
@name = name
@secret = secret
@options = options
end
def subscribe(user)
req = Net::HTTP::Post.new("/subscribe")
req.form_data = {:service => @name, :secret => @secret, :user => user}
request req
end
def post(user, message)
req = Net::HTTP::Post.new("/post")
req.form_data = {:service => @name, :secret => @secret, :user => user, :message => message}
request req
end
def request(req)
url = URI.parse('http://pushproxy.heroku.com')
res = Net::HTTP.start(url.host, url.port) { |http| http.request(req) }
unless res.kind_of? Net::HTTPSuccess
handle_error req, res
end
res
end
private
def handle_error(req, res)
e = RuntimeError.new("#{res.code}:#{res.message}\nMETHOD:#{req.method}\nURI:#{req.path}\n#{res.body}")
raise e
end
end
end
# end Notifo wrapper
require 'json'
COUCH = "a couchdb"
DB = "a database on that couchdb"
NOTIFO_SERVICE = "service name"
NOTIFO_SECRET = "api secret token"
@couch = Couch::Server.new(COUCH, "5984")
@notifo = Notifo::Service.new(NOTIFO_SERVICE, NOTIFO_SECRET)
def save(number)
doc = <<-JSON
{"type":"contact","number":"#{number}"}
JSON
@couch.post("/#{DB}", doc)
end
def view(view)
server = Couch::Server.new("localhost", "5984")
res = @couch.get("/#{DB}/_design/group/_view/#{view}")
JSON.parse(res.body)
end
number = $currentCall.callerID.to_s
numbers = view('numbers')['rows'].map{|row| row['value']['number'].to_s}
save(number) unless numbers.include? number
def send(message, numbers)
numbers.each do |number|
message(message, {
:to => number,
:network => "SMS",
:callerID => $currentCall.calledID.to_s
})
end
say message
end
send($currentCall.initialText, numbers)