Completed
Push — master ( 7def71...2befdc )
by John
01:10
created

ConfigApp.to_hash()   A

Complexity

Conditions 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 8
rs 9.4285
cc 1
1
require 'dotenv'
2
3
module RingCentralSdk::REST
4
  class Config
5
    attr_accessor :user
6
    attr_accessor :app
7
    attr_accessor :env
8
9
    def initialize
10
      @app = RingCentralSdk::REST::ConfigApp.new
11
      @user = RingCentralSdk::REST::ConfigUser.new
12
      @env = RingCentralSdk::REST::ConfigEnvRc.new
13
    end
14
15
    def load_dotenv
16
      Dotenv.load
17
      @app.load_env()
18
      @user.load_env()
19
      @env.load_env()
20
      return self
21
    end
22
  end
23
end
24
25
module RingCentralSdk::REST
26
  class ConfigUser
27
    attr_accessor :username
28
    attr_accessor :extension
29
    attr_accessor :password
30
31
    def load_env
32
      @username = ENV['RC_USER_USERNAME']
33
      @extension = ENV['RC_USER_EXTENSION']
34
      @password = ENV['RC_USER_PASSWORD']
35
    end
36
37
    def nilify
38
      @username = ''
39
      @extension = ''
40
      @password = ''
41
    end
42
  end
43
end
44
45
module RingCentralSdk::REST
46
  class ConfigApp
47
    attr_accessor :key
48
    attr_accessor :secret
49
    attr_accessor :server_url
50
    attr_accessor :redirect_url
51
52
    def initialize(app_key='', app_secret='', server_url=RingCentralSdk::RC_SERVER_SANDBOX, opts={})
53
      @key = app_key
54
      @secret = app_secret
55
      @server_url = server_url
56
      if opts.key?(:redirect_url)
57
        @redirect_url = opts[:redirect_url]
58
      elsif opts.key?(:redirect_uri)
59
        @redirect_url = opts[:redirect_uri]
60
      else
61
        @redirect_url = ''
62
      end
63
    end
64
65
    def load_env
66
      ['RC_APP_KEY', 'RC_APP_SECRET', 'RC_APP_SERVER_URL', 'RC_APP_REDIRECT_URL'].each do |var|
67
        if !ENV.key?(var)
68
          fail "environment variable '#{var}' not found"
69
        end
70
      end
71
      
72
      @key = ENV['RC_APP_KEY']
73
      @secret = ENV['RC_APP_SECRET']
74
      @server_url = ENV['RC_APP_SERVER_URL']
75
      @redirect_url = ENV['RC_APP_REDIRECT_URL']
76
    end
77
78
    def to_hash
79
      {
80
        key: @key,
81
        secret: @secret,
82
        server_url: @server_url,
83
        redirect_url: @redirect_url
84
      }
85
    end
86
  end
87
end
88
89
module RingCentralSdk::REST
90
  class ConfigEnvRc
91
    attr_accessor :data
92
    def initialize
93
      @data = {}
94
    end
95
    def load_env
96
      ENV.each do |k,v|
97
        next unless k.index('RC_') == 0
98
        @data[k] = v
99
      end
100
    end
101
  end
102
end
103