Completed
Push — master ( d614fe...b7118a )
by John
01:37
created

RingCentralSdkBootstrap   A

Complexity

Total Complexity 4

Size/Duplication

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A get_sdk_with_token() 0 17 1
A load_credentials() 0 11 3
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
  end
19
20
  def get_sdk_with_token(env=:sandbox, app_index=0, resource_owner_index=0)
21
    credentials = @credentials
22
23
    rcsdk = RingCentralSdk.new(
24
      credentials[env][:applications][app_index][:app_key],
25
      credentials[env][:applications][app_index][:app_secret],
26
      credentials[env][:api][:server]
27
    )
28
29
    rcsdk.authorize(
30
      credentials[env][:resource_owners][resource_owner_index][:username],
31
      credentials[env][:resource_owners][resource_owner_index][:extension],
32
      credentials[env][:resource_owners][resource_owner_index][:password],
33
    ) 
34
35
    return rcsdk
36
  end
37
38
end
39
40
boot = RingCentralSdkBootstrap.new
41
boot.load_credentials(ARGV.shift, 'Usage: subscription.rb path/to/credentials.json [extensionId]')
42
rcsdk = boot.get_sdk_with_token()
43
44
to_phone_number = ARGV.shift
45
46
unless to_phone_number.to_s.length>0
47
  abort("Usage: fax_send.rb rc-credentials.json phone_number my_file.pdf")
48
end
49
50
file_name = ARGV.shift
51
52
unless file_name.to_s.length>0
53
  abort("Usage: fax_send.rb rc-credentials.json phone_number my_file.pdf")
54
end
55
56
unless File.exists?(file_name.to_s)
57
  abort("Error: file to fax does not exist for: #{file_name}")
58
end
59
60
def send_fax(rcsdk, to_phone_number, file_name)
61
  fax = RingCentralSdk::Helpers::CreateFaxRequest.new(
62
    nil,
63
    {
64
    	:to            => [{:phoneNumber => to_phone_number}],
65
    	:faxResolution => 'High',
66
    	:coverPageText => 'RingCentral Fax Base64 using Ruby!'
67
    },
68
    :file_name       => file_name,
69
    :base64_encode   => true
70
  )
71
72
  puts fax.body
73
74
  client = rcsdk.client
75
76
  if 1==1
77
    response = client.post do |req|
78
      req.url fax.url
79
      req.headers['Content-Type'] = fax.content_type
80
      req.body = fax.body
81
    end
82
    puts response.body.to_s
83
  end
84
85
end
86
87
send_fax(rcsdk, to_phone_number, file_name)
88
89
puts "DONE"