-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.rb
43 lines (36 loc) · 974 Bytes
/
api.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
#!/usr/bin/env ruby
$LOAD_PATH << '.'
require 'sinatra'
require 'json'
require 'mail'
require 'user'
set :port, 8080
=begin
version > 1
request.body = {
"from":"username",
"to":["mail_friend1", mail_friend2],
"custom_msg":"I am best friend...."}
=end
post "/api/:version/users/invite" do
content_type :json
data = JSON.parse(request.body.read)
case params[:version]
when "1"
"api not supported in this version"
else
from = User.new(data["from"]).load
if from.exists
data["to"].each do |email|
User.new(email).addRefer(from.email).save
end
if data["custom_msg"] == nil
data["custom_msg"] = "My Msg"
end
Mail.send(from, data["to"], "You've been invited to a secret place", data["custom_msg"])
{:status=>"ok",:msg=>"Invitations have been sent"}.to_json
else
{:status=>"error",:msg=>"You are not allowed to invite friends"}.to_json
end
end
end