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 = self.PRODUCTION_SERVER, debug_mode = false) ⇒ RingCentral

Returns a new instance of RingCentral.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/ringcentral.rb', line 16

def initialize(client_id, client_secret, server = self.PRODUCTION_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.



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

def auto_refresh
  @auto_refresh
end

#client_idObject (readonly)

Returns the value of attribute client_id.



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

def client_id
  @client_id
end

#client_secretObject (readonly)

Returns the value of attribute client_secret.



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

def client_secret
  @client_secret
end

#debug_modeObject

Returns the value of attribute debug_mode.



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

def debug_mode
  @debug_mode
end

#serverObject (readonly)

Returns the value of attribute server.



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

def server
  @server
end

#tokenObject

Returns the value of attribute token.



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

def token
  @token
end

Class Method Details

.PRODUCTION_SERVERObject



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

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

Instance Method Details

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



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/ringcentral.rb', line 44

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
    warn('Password auth is deprecated. Use JWT auth or OAuth instead.')
    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



91
92
93
94
95
96
97
98
# File 'lib/ringcentral.rb', line 91

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



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/ringcentral.rb', line 191

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



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/ringcentral.rb', line 100

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



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/ringcentral.rb', line 171

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



119
120
121
122
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
# File 'lib/ringcentral.rb', line 119

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



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/ringcentral.rb', line 151

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



73
74
75
76
77
78
79
80
81
82
# File 'lib/ringcentral.rb', line 73

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



84
85
86
87
88
89
# File 'lib/ringcentral.rb', line 84

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