1
|
|
|
module RingCentralSdk::REST |
2
|
|
|
|
3
|
|
|
# A simplified, but still generic, REST interface. |
4
|
|
|
# |
5
|
|
|
# NOTE: This is an experimental module. |
6
|
|
|
# |
7
|
|
|
# client = RingCentralSdk::REST::Client.new ... |
8
|
|
|
# simple = RingCentralSdk::REST::SimpleClient client |
9
|
|
|
# |
10
|
|
|
# simple.post( |
11
|
|
|
# path: 'sms', |
12
|
|
|
# body: { |
13
|
|
|
# from: {phoneNumber: '+16505551212'}, |
14
|
|
|
# to: [{phoneNumber: '+14155551212'}], |
15
|
|
|
# text: 'Hi There!' |
16
|
|
|
# } |
17
|
|
|
# ) |
18
|
|
|
class SimpleClient |
19
|
|
|
attr_accessor :client |
20
|
|
|
|
21
|
|
|
def initialize(client) |
22
|
|
|
@client = client |
23
|
|
|
end |
24
|
|
|
|
25
|
|
|
def send(request) |
26
|
|
|
if request.is_a?(RingCentralSdk::Helpers::Request) |
27
|
|
|
return @client.request(request) |
28
|
|
|
elsif ! request.is_a?(Hash) |
29
|
|
|
raise "Request is not a RingCentralSdk::Helpers::Request or Hash" |
30
|
|
|
end |
31
|
|
|
|
32
|
|
|
verb = request.key?(:verb) ? request[:verb].to_s.downcase : 'get' |
33
|
|
|
|
34
|
|
|
if verb == 'get' |
35
|
|
|
return get(request) |
36
|
|
|
elsif verb == 'post' |
37
|
|
|
return post(request) |
38
|
|
|
elsif verb == 'put' |
39
|
|
|
return put(request) |
40
|
|
|
elsif verb == 'delete' |
41
|
|
|
return delete(request) |
42
|
|
|
else |
43
|
|
|
raise 'Method not supported' |
44
|
|
|
end |
45
|
|
|
end |
46
|
|
|
|
47
|
|
|
def delete(opts={}) |
48
|
|
|
return @client.http.delete do |req| |
49
|
|
|
req.url build_url(opts[:path]) |
50
|
|
|
end |
51
|
|
|
end |
52
|
|
|
|
53
|
|
|
def get(opts={}) |
54
|
|
|
return @client.http.get do |req| |
55
|
|
|
req.url build_url(opts[:path]) |
56
|
|
|
end |
57
|
|
|
end |
58
|
|
|
|
59
|
|
|
def post(opts={}) |
60
|
|
|
return @client.http.post do |req| |
61
|
|
|
req = inflate_request req, opts |
62
|
|
|
end |
63
|
|
|
end |
64
|
|
|
|
65
|
|
|
def put(opts={}) |
66
|
|
|
return @client.http.put do |req| |
67
|
|
|
req = inflate_request req, opts |
68
|
|
|
end |
69
|
|
|
end |
70
|
|
|
|
71
|
|
|
def inflate_request(req, opts={}) |
72
|
|
|
req.url build_url(opts[:path]) |
73
|
|
|
if opts.key? :body |
74
|
|
|
req.body = opts[:body] |
75
|
|
|
if opts[:body].is_a?(Hash) |
76
|
|
|
req.headers['Content-Type'] = 'application/json' |
77
|
|
|
end |
78
|
|
|
end |
79
|
|
|
req |
80
|
|
|
end |
81
|
|
|
|
82
|
|
|
def build_url(path) |
83
|
|
|
url = '' |
84
|
|
|
if !path.is_a?(Array) |
85
|
|
|
path = [path] |
86
|
|
|
end |
87
|
|
|
if path.length > 0 |
88
|
|
|
path0 = path[0].to_s |
89
|
|
|
if path0 !~ /\// |
90
|
|
|
if path0.index('account') != 0 |
91
|
|
|
if path0.index('extension') != 0 |
92
|
|
|
path.unshift('extension/~') |
93
|
|
|
end |
94
|
|
|
path.unshift('account/~') |
95
|
|
|
end |
96
|
|
|
end |
97
|
|
|
end |
98
|
|
|
url = path.join('/') |
99
|
|
|
return url |
100
|
|
|
end |
101
|
|
|
end |
102
|
|
|
end |
103
|
|
|
|