Class: RingCentral

Inherits:
Object
  • Object
show all
Defined in:
lib/ringcentral.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client_id, client_secret, server, debug_mode = false) ⇒ RingCentral

Returns a new instance of RingCentral.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/ringcentral.rb', line 21

def initialize(client_id, client_secret, server, debug_mode = false)
  @client_id = client_id
  @client_secret = client_secret
  @server = server
  @auto_refresh = false
  @debug_mode = debug_mode
  @token = nil
  @timer = nil
  @faraday = Faraday.new(url: server, request: { params_encoder: Faraday::FlatParamsEncoder }) do |faraday|
    faraday.request :multipart
    faraday.request :url_encoded
    faraday.response :json, content_type: /\bjson$/
    faraday.adapter Faraday.default_adapter
  end
end

Instance Attribute Details

#auto_refreshObject

Returns the value of attribute auto_refresh.



19
20
21
# File 'lib/ringcentral.rb', line 19

def auto_refresh
  @auto_refresh
end

#client_idObject (readonly)

Returns the value of attribute client_id.



18
19
20
# File 'lib/ringcentral.rb', line 18

def client_id
  @client_id
end

#client_secretObject (readonly)

Returns the value of attribute client_secret.



18
19
20
# File 'lib/ringcentral.rb', line 18

def client_secret
  @client_secret
end

#debug_modeObject

Returns the value of attribute debug_mode.



19
20
21
# File 'lib/ringcentral.rb', line 19

def debug_mode
  @debug_mode
end

#serverObject (readonly)

Returns the value of attribute server.



18
19
20
# File 'lib/ringcentral.rb', line 18

def server
  @server
end

#tokenObject

Returns the value of attribute token.



18
19
20
# File 'lib/ringcentral.rb', line 18

def token
  @token
end

Class Method Details

.PRODUCTION_SERVERObject



14
15
16
# File 'lib/ringcentral.rb', line 14

def self.PRODUCTION_SERVER
  'https://platform.ringcentral.com'
end

.SANDBOX_SERVERObject



10
11
12
# File 'lib/ringcentral.rb', line 10

def self.SANDBOX_SERVER
  'https://platform.devtest.ringcentral.com'
end

Instance Method Details

#authorize(username: nil, extension: nil, password: nil, auth_code: nil, redirect_uri: nil, jwt: nil, verifier: nil) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/ringcentral.rb', line 49

def authorize(username: nil, extension: nil, password: nil, auth_code: nil, redirect_uri: nil, jwt: nil, verifier: nil)
  if auth_code != nil
    payload = {
      grant_type: 'authorization_code',
      code: auth_code,
      redirect_uri: redirect_uri,
    }
    if verifier != nil
      payload["code_verifier"] = verifier
    end
  elsif jwt != nil
    payload = {
      grant_type: 'urn:ietf:params:oauth:grant-type:jwt-bearer',
      assertion: jwt
    }
  else
    payload = {
      grant_type: 'password',
      username: username,
      extension: extension,
      password: password,
    }
  end
  self.token = nil
  r = self.post('/restapi/oauth/token', payload: payload)
  self.token = r.body
end

#authorize_uri(redirect_uri, hash = {}) ⇒ Object



95
96
97
98
99
100
101
102
# File 'lib/ringcentral.rb', line 95

def authorize_uri(redirect_uri, hash = {})
  hash[:response_type] = 'code'
  hash[:redirect_uri] = redirect_uri
  hash[:client_id] = @client_id
  uri = Addressable::URI.parse(@server) + '/restapi/oauth/authorize'
  uri.query_values = hash
  uri.to_s
end

#delete(endpoint, params = {}) ⇒ Object



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/ringcentral.rb', line 195

def delete(endpoint, params = {})
  r = @faraday.delete do |req|
    req.url endpoint
    req.params = params
    req.headers = headers
  end
  if @debug_mode
    puts r.status, r.body
  end
  if r.status >= 400
    raise "HTTP status #{r.status}: 

headers: #{r.headers}

body: #{r.body}"
  end
  return r
end

#get(endpoint, params = {}) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/ringcentral.rb', line 104

def get(endpoint, params = {})
  r = @faraday.get do |req|
    req.url endpoint
    req.params = params
    req.headers = headers
  end
  if @debug_mode
    puts r.status, r.body
  end
  if r.status >= 400
    raise "HTTP status #{r.status}: 

headers: #{r.headers}

body: #{r.body}"
  end
  return r
end

#patch(endpoint, payload: nil, params: {}, files: nil) ⇒ Object



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/ringcentral.rb', line 175

def patch(endpoint, payload: nil, params: {}, files: nil)
  r = @faraday.patch do |req|
    req.url endpoint
    req.params = params
    req.headers = headers.merge({ 'Content-Type': 'application/json' })
    req.body = payload.to_json
  end
  if @debug_mode
    puts r.status, r.body
  end
  if r.status >= 400
    raise "HTTP status #{r.status}: 

headers: #{r.headers}

body: #{r.body}"
  end
  return r
end

#post(endpoint, payload: nil, params: {}, files: nil) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/ringcentral.rb', line 123

def post(endpoint, payload: nil, params: {}, files: nil)
  r = @faraday.post do |req|
    req.url endpoint
    req.params = params
    if files != nil && files.size > 0 # send fax or MMS
      io = StringIO.new(payload.to_json)
      payload = {}
      payload[:json] = Faraday::UploadIO.new(io, 'application/json')
      payload[:attachment] = files.map{ |file| Faraday::UploadIO.new(file[0], file[1]) }
      req.headers = headers
      req.body = payload
    elsif payload != nil && @token != nil
      req.headers = headers.merge({ 'Content-Type': 'application/json' })
      req.body = payload.to_json
    else
      req.headers = headers
      req.body = payload
    end
  end
  if @debug_mode
    puts r.status, r.body
  end
  if r.status >= 400
    raise "HTTP status #{r.status}: 

headers: #{r.headers}

body: #{r.body}"
  end
  return r
end

#put(endpoint, payload: nil, params: {}, files: nil) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/ringcentral.rb', line 155

def put(endpoint, payload: nil, params: {}, files: nil)
  r = @faraday.put do |req|
    req.url endpoint
    req.params = params
    req.headers = headers.merge({ 'Content-Type': 'application/json' })
    req.body = payload.to_json
  end
  if @debug_mode
    puts r.status, r.body
  end
  if r.status >= 400
    raise "HTTP status #{r.status}: 

headers: #{r.headers}

body: #{r.body}"
  end
  return r
end

#refreshObject



77
78
79
80
81
82
83
84
85
86
# File 'lib/ringcentral.rb', line 77

def refresh
  return if @token == nil
  payload = {
    grant_type: 'refresh_token',
    refresh_token: @token['refresh_token']
  }
  self.token = nil
  r = self.post('/restapi/oauth/token', payload: payload)
  self.token = r.body
end

#revokeObject



88
89
90
91
92
93
# File 'lib/ringcentral.rb', line 88

def revoke
  return if @token == nil
  payload = { token: @token['access_token'] }
  self.token = nil
  self.post('/restapi/oauth/revoke', payload: payload)
end