Class: RingCentralSdk::REST::SimpleClient

Inherits:
Object
  • Object
show all
Defined in:
lib/ringcentral_sdk/rest/simple_client.rb

Overview

A simplified, but still generic, REST interface.

NOTE: This is an experimental module.

client = RingCentralSdk::REST::Client.new … simple = RingCentralSdk::REST::SimpleClient client

simple.post(

path: 'sms',
body: {
  from: {phoneNumber: '+16505551212'},
  to: [{phoneNumber: '+14155551212'}],
  text: 'Hi There!'
}

)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ SimpleClient

Returns a new instance of SimpleClient.



21
22
23
# File 'lib/ringcentral_sdk/rest/simple_client.rb', line 21

def initialize(client)
  @client = client
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



19
20
21
# File 'lib/ringcentral_sdk/rest/simple_client.rb', line 19

def client
  @client
end

Instance Method Details

#build_url(path) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/ringcentral_sdk/rest/simple_client.rb', line 82

def build_url(path)
  url = ''
  if !path.is_a?(Array)
    path = [path]
  end
  if path.length > 0
    path0 = path[0].to_s
    if path0 !~ /\//
      if path0.index('account') != 0
        if path0.index('extension') != 0
          path.unshift('extension/~')
        end
        path.unshift('account/~')
      end
    end
  end
  url = path.join('/')
  return url
end

#delete(opts = {}) ⇒ Object



47
48
49
50
51
# File 'lib/ringcentral_sdk/rest/simple_client.rb', line 47

def delete(opts={})
  return @client.http.delete do |req|
    req.url build_url(opts[:path])
  end
end

#get(opts = {}) ⇒ Object



53
54
55
56
57
# File 'lib/ringcentral_sdk/rest/simple_client.rb', line 53

def get(opts={})
  return @client.http.get do |req|
    req.url build_url(opts[:path])
  end
end

#inflate_request(req, opts = {}) ⇒ Object



71
72
73
74
75
76
77
78
79
80
# File 'lib/ringcentral_sdk/rest/simple_client.rb', line 71

def inflate_request(req, opts={})
  req.url build_url(opts[:path])
  if opts.key? :body
    req.body = opts[:body]
    if opts[:body].is_a?(Hash)
      req.headers['Content-Type'] = 'application/json'
    end
  end
  req
end

#post(opts = {}) ⇒ Object



59
60
61
62
63
# File 'lib/ringcentral_sdk/rest/simple_client.rb', line 59

def post(opts={})
  return @client.http.post do |req|
    req = inflate_request req, opts
  end
end

#put(opts = {}) ⇒ Object



65
66
67
68
69
# File 'lib/ringcentral_sdk/rest/simple_client.rb', line 65

def put(opts={})
  return @client.http.put do |req|
    req = inflate_request req, opts
  end
end

#send(request) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/ringcentral_sdk/rest/simple_client.rb', line 25

def send(request)
  if request.is_a?(RingCentralSdk::Helpers::Request)
    return @client.request(request)
  elsif ! request.is_a?(Hash)
    raise "Request is not a RingCentralSdk::Helpers::Request or Hash"
  end

  verb = request.key?(:verb) ? request[:verb].to_s.downcase : 'get'

  if verb == 'get'
    return get(request)
  elsif verb == 'post'
    return post(request)
  elsif verb == 'put'
    return put(request)
  elsif verb == 'delete'
    return delete(request)
  else
    raise 'Method not supported'
  end
end