|
1
|
|
|
#!ruby |
|
2
|
|
|
|
|
3
|
|
|
require 'multi_json' |
|
4
|
|
|
require 'ringcentral_sdk' |
|
5
|
|
|
|
|
6
|
|
|
=begin |
|
7
|
|
|
|
|
8
|
|
|
RingCentralSdkBootstrap is code to load credentials simply from a JSON file. |
|
9
|
|
|
|
|
10
|
|
|
The core part of the example is below. |
|
11
|
|
|
|
|
12
|
|
|
=end |
|
13
|
|
|
|
|
14
|
|
|
class RingCentralSdkBootstrap |
|
15
|
|
|
|
|
16
|
|
|
def load_credentials(credentials_filepath, usage_string=nil) |
|
17
|
|
|
unless credentials_filepath.to_s.length>0 |
|
18
|
|
|
raise usage_string.to_s |
|
19
|
|
|
end |
|
20
|
|
|
|
|
21
|
|
|
unless File.exists?(credentials_filepath.to_s) |
|
22
|
|
|
raise "Error: credentials file does not exist for: #{credentials_filepath}" |
|
23
|
|
|
end |
|
24
|
|
|
|
|
25
|
|
|
@credentials = MultiJson.decode(IO.read(credentials_filepath), :symbolize_keys=>true) |
|
26
|
|
|
|
|
27
|
|
|
@app_index = ARGV.shift |
|
28
|
|
|
@usr_index = ARGV.shift |
|
29
|
|
|
@app_index = @app_index.to_s =~ /^[0-9]+$/ ? @app_index.to_i : 0 |
|
30
|
|
|
@usr_index = @usr_index.to_s =~ /^[0-9]+$/ ? @usr_index.to_i : 0 |
|
31
|
|
|
end |
|
32
|
|
|
|
|
33
|
|
|
def get_sdk_with_token(env=:sandbox) |
|
34
|
|
|
credentials = @credentials |
|
35
|
|
|
app_index = @app_index |
|
36
|
|
|
resource_owner_index = @usr_index |
|
37
|
|
|
|
|
38
|
|
|
rcsdk = RingCentralSdk.new( |
|
39
|
|
|
credentials[env][:applications][app_index][:app_key], |
|
40
|
|
|
credentials[env][:applications][app_index][:app_secret], |
|
41
|
|
|
credentials[env][:api][:server] |
|
42
|
|
|
) |
|
43
|
|
|
|
|
44
|
|
|
rcsdk.authorize( |
|
45
|
|
|
credentials[env][:resource_owners][resource_owner_index][:username], |
|
46
|
|
|
credentials[env][:resource_owners][resource_owner_index][:extension], |
|
47
|
|
|
credentials[env][:resource_owners][resource_owner_index][:password], |
|
48
|
|
|
) |
|
49
|
|
|
|
|
50
|
|
|
return rcsdk |
|
51
|
|
|
end |
|
52
|
|
|
|
|
53
|
|
|
end |
|
54
|
|
|
|
|
55
|
|
|
boot = RingCentralSdkBootstrap.new |
|
56
|
|
|
boot.load_credentials(ARGV.shift, 'Usage: subscription.rb path/to/credentials.json [app_index] [resource_owner_index]') |
|
57
|
|
|
rcsdk = boot.get_sdk_with_token() |
|
58
|
|
|
|
|
59
|
|
|
=begin |
|
60
|
|
|
|
|
61
|
|
|
get_all_extensions(rcsdk) retrieves all extensions. |
|
62
|
|
|
|
|
63
|
|
|
=end |
|
64
|
|
|
|
|
65
|
|
|
def get_all_extensions(rcsdk) |
|
66
|
|
|
extension_ids_map = {} |
|
67
|
|
|
extensions = [] |
|
68
|
|
|
res = rcsdk.client.get do |req| |
|
69
|
|
|
req.url '/restapi/v1.0/account/~/extension' |
|
70
|
|
|
req.params['page'] = 1 |
|
71
|
|
|
req.params['perPage'] = 1000 |
|
72
|
|
|
req.params['status'] = 'Enabled' |
|
73
|
|
|
end |
|
74
|
|
|
res.body['records'].each do |record| |
|
75
|
|
|
if !extension_ids_map.has_key?(record['id']) |
|
76
|
|
|
extensions.push record |
|
77
|
|
|
extension_ids_map[record['id']] = 1 |
|
78
|
|
|
end |
|
79
|
|
|
end |
|
80
|
|
|
while res.body.has_key?('navigation') && res.body['navigation'].has_key?('nextPage') |
|
81
|
|
|
res = rcsdk.client.get do |req| |
|
82
|
|
|
req.url res.body['navigation']['nextPage']['uri'] |
|
83
|
|
|
end |
|
84
|
|
|
res.body['records'].each do |record| |
|
85
|
|
|
if !extension_ids_map.has_key?(record['id']) |
|
86
|
|
|
extensions.push record |
|
87
|
|
|
extension_ids_map[record['id']] = 1 |
|
88
|
|
|
end |
|
89
|
|
|
end |
|
90
|
|
|
end |
|
91
|
|
|
return extensions |
|
92
|
|
|
end |
|
93
|
|
|
|
|
94
|
|
|
extensions = get_all_extensions(rcsdk) |
|
95
|
|
|
|
|
96
|
|
|
# Create an array of event_filters from the array of extensions |
|
97
|
|
|
|
|
98
|
|
|
event_filters = [] |
|
99
|
|
|
|
|
100
|
|
|
extensions.each do |ext| |
|
101
|
|
|
if ext.has_key?('id') |
|
102
|
|
|
event_filter = "/restapi/v1.0/account/~/extension/#{ext['id']}/presence" |
|
103
|
|
|
event_filters.push event_filter |
|
104
|
|
|
end |
|
105
|
|
|
end |
|
106
|
|
|
|
|
107
|
|
|
# An example observer object |
|
108
|
|
|
class MyObserver |
|
109
|
|
|
def update(message) |
|
110
|
|
|
puts "Subscription Message Received" |
|
111
|
|
|
puts JSON.dump(message) |
|
112
|
|
|
end |
|
113
|
|
|
end |
|
114
|
|
|
|
|
115
|
|
|
def run_subscription(rcsdk, event_filters) |
|
116
|
|
|
# Create an observable subscription and add your observer |
|
117
|
|
|
sub = rcsdk.create_subscription() |
|
118
|
|
|
sub.subscribe(event_filters) |
|
119
|
|
|
|
|
120
|
|
|
sub.add_observer(MyObserver.new()) |
|
121
|
|
|
|
|
122
|
|
|
puts "Click any key to finish" |
|
123
|
|
|
|
|
124
|
|
|
stop_script = gets |
|
125
|
|
|
|
|
126
|
|
|
# End the subscription |
|
127
|
|
|
sub.destroy() |
|
128
|
|
|
end |
|
129
|
|
|
|
|
130
|
|
|
# Make a subscription for all event_filters |
|
131
|
|
|
run_subscription(rcsdk, event_filters) |
|
132
|
|
|
|
|
133
|
|
|
puts "DONE" |