Completed
Push — master ( 945838...e44b4d )
by John
01:50
created

RingCentralSdkBootstrap   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 40
Duplicated Lines 0 %
Metric Value
dl 0
loc 40
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A load_credentials() 0 16 3
A get_sdk_with_token() 0 19 1
1
#!ruby
2
3
require 'multi_json'
4
require 'ringcentral_sdk'
5
6
class RingCentralSdkBootstrap
7
8
  def load_credentials(credentials_filepath, usage_string=nil)
9
    unless credentials_filepath.to_s.length>0
10
      raise usage_string.to_s
11
    end
12
13
    unless File.exists?(credentials_filepath.to_s)
14
      raise "Error: credentials file does not exist for: #{credentials_filepath}"
15
    end
16
17
    @credentials = MultiJson.decode(IO.read(credentials_filepath), :symbolize_keys=>true)
18
19
    @app_index = ARGV.shift
20
    @usr_index = ARGV.shift
21
    @app_index = @app_index.to_s =~ /^[0-9]+$/ ? @app_index.to_i : 0
22
    @usr_index = @usr_index.to_s =~ /^[0-9]+$/ ? @usr_index.to_i : 0
23
  end
24
25
  def get_sdk_with_token(env=:sandbox)
26
    credentials = @credentials
27
    app_index = @app_index
28
    resource_owner_index = @usr_index
29
30
    rcsdk = RingCentralSdk.new(
31
      credentials[env][:applications][app_index][:app_key],
32
      credentials[env][:applications][app_index][:app_secret],
33
      credentials[env][:api][:server]
34
    )
35
36
    rcsdk.authorize(
37
      credentials[env][:resource_owners][resource_owner_index][:username],
38
      credentials[env][:resource_owners][resource_owner_index][:extension],
39
      credentials[env][:resource_owners][resource_owner_index][:password],
40
    ) 
41
42
    return rcsdk
43
  end
44
45
end
46
47
boot = RingCentralSdkBootstrap.new
48
boot.load_credentials(ARGV.shift, 'Usage: subscription.rb path/to/credentials.json [app_index] [resource_owner_index]')
49
rcsdk = boot.get_sdk_with_token()
50
51
# An example observer object
52
class MyObserver
53
  def update(message)
54
    puts "Subscription Message Received"
55
    puts JSON.dump(message)
56
  end
57
end
58
59
def run_subscription(rcsdk)
60
  # Create an observable subscription and add your observer
61
  sub = rcsdk.create_subscription()
62
  sub.subscribe(["/restapi/v1.0/account/~/extension/~/presence"])
63
64
  sub.add_observer(MyObserver.new())
65
66
  puts "Click any key to finish"
67
68
  stop_script = gets
69
70
  # End the subscription
71
  sub.destroy()
72
end
73
74
run_subscription(rcsdk)
75
76
puts "DONE"