-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.rb
68 lines (58 loc) · 1.78 KB
/
app.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
require "sinatra"
require "sinatra/reloader"
require "json"
require "pony"
require_relative 'nexmo.rb'
class CallerId < Sinatra::Base
get '/' do
erb :phone_form
end
post '/lookup' do
@insight = Nexmo.lookup(params["phone"], "#{request.base_url}/nexmo_insights?email=#{params["email"]}")
erb :phone_lookup
end
post '/nexmo_insights' do
phone_info = request.body.read
#print it out so we can view the response in the console
puts params
puts phone_info
email_insight(phone_info, params[:email])
#We need to return a 200 success code to Nexmo when the webhook hits our endpoint
#if we don't return a 200 success code, Nexmo will retry sending the result
status 200
end
def email_insight(phone_info, email)
begin
Pony.mail(
:to => email,
:via => :smtp,
:via_options => {
:address => "smtp.gmail.com",
:port => 587,
:user_name => '[email protected]',
:password => MY_PASSWORD,
:authentication => 'plain',
:enable_starttls_auto => true
},
:from => '[email protected]',
:subject => "Nexmo insight for #{JSON.parse(phone_info)["national_format_number"]}",
:headers => { 'Content-Type' => "text/html" },
:body => to_html_string(phone_info))
rescue Exception => e
puts e
end
end
def to_html_string(phone_info)
html = "<h1>Number Insight</h1>\n<table>"
JSON.parse(phone_info).each do |key, value|
html << %Q(
<tr>
<td> #{key.split("_").map(&:capitalize).join(' ')} </td>
<td> #{value} </td>
</tr>
)
end
html << "</table>"
end
CallerId.run!
end