Total Complexity | 19 |
Total Lines | 86 |
Duplicated Lines | 0 % |
1 | module RingCentralSdk::REST |
||
2 | class SimpleClient |
||
3 | attr_accessor :client |
||
4 | |||
5 | def initialize(client) |
||
6 | @client = client |
||
7 | end |
||
8 | |||
9 | def send(request) |
||
10 | if request.is_a?(RingCentralSdk::Helpers::Request) |
||
11 | return @client.request(request) |
||
12 | elsif ! request.is_a?(Hash) |
||
13 | raise "Request is not a RingCentralSdk::Helpers::Request or Hash" |
||
14 | end |
||
15 | |||
16 | verb = request.has_key?(:verb) ? request[:verb].to_s.downcase : 'get' |
||
17 | |||
18 | if verb == 'get' |
||
19 | return get(request) |
||
20 | elsif verb == 'post' |
||
21 | return post(request) |
||
22 | elsif verb == 'put' |
||
23 | return put(request) |
||
24 | elsif verb == 'delete' |
||
25 | return delete(request) |
||
26 | else |
||
27 | raise 'Method not supported' |
||
28 | end |
||
29 | end |
||
30 | |||
31 | def delete(opts={}) |
||
32 | return @client.http.delete do |req| |
||
33 | req.url build_url(opts[:path]) |
||
34 | end |
||
35 | end |
||
36 | |||
37 | def get(opts={}) |
||
38 | return @client.http.get do |req| |
||
39 | req.url build_url(opts[:path]) |
||
40 | end |
||
41 | end |
||
42 | |||
43 | def post(opts={}) |
||
44 | return @client.http.post do |req| |
||
45 | req.url build_url(opts[:path]) |
||
46 | if opts.has_key?(:body) |
||
47 | req.body = opts[:body] |
||
48 | if opts[:body].is_a?(Hash) |
||
49 | req.headers['Content-Type'] = 'application/json' |
||
50 | end |
||
51 | end |
||
52 | end |
||
53 | end |
||
54 | |||
55 | def put(opts={}) |
||
56 | return @client.http.put do |req| |
||
57 | req.url build_url(opts[:path]) |
||
58 | if opts.has_key?(:body) |
||
59 | req.body = opts[:body] |
||
60 | if opts[:body].is_a?(Hash) |
||
61 | req.headers['Content-Type'] = 'application/json' |
||
62 | end |
||
63 | end |
||
64 | end |
||
65 | end |
||
66 | |||
67 | def build_url(path) |
||
68 | url = '' |
||
69 | if !path.is_a?(Array) |
||
70 | path = [path] |
||
71 | end |
||
72 | if path.length>0 |
||
73 | path0 = path[0].to_s |
||
74 | if path0 !~ /\// |
||
75 | if path0.index('account') != 0 |
||
76 | if path0.index('extension') != 0 |
||
77 | path.unshift('extension/~') |
||
78 | end |
||
79 | path.unshift('account/~') |
||
80 | end |
||
81 | end |
||
82 | end |
||
83 | url = path.join('/') |
||
84 | return url |
||
85 | end |
||
86 | |||
87 | end |
||
89 |