Module: Wechat::ApiLoader
- Defined in:
- lib/wechat/api_loader.rb
Class Method Summary collapse
- .class_exists?(class_name) ⇒ Boolean
- .config ⇒ Object
- .config_from_environment ⇒ Object
- .config_from_file ⇒ Object
- .loading_config! ⇒ Object
- .with(options) ⇒ Object
Class Method Details
.class_exists?(class_name) ⇒ Boolean
82 83 84 85 86 |
# File 'lib/wechat/api_loader.rb', line 82 def self.class_exists?(class_name) return Module.const_get(class_name).present? rescue NameError return false end |
.config ⇒ Object
24 25 26 27 28 |
# File 'lib/wechat/api_loader.rb', line 24 def self.config return @config unless @config.nil? @config ||= loading_config! @config end |
.config_from_environment ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/wechat/api_loader.rb', line 66 def self.config_from_environment { appid: ENV['WECHAT_APPID'], secret: ENV['WECHAT_SECRET'], corpid: ENV['WECHAT_CORPID'], corpsecret: ENV['WECHAT_CORPSECRET'], agentid: ENV['WECHAT_AGENTID'], token: ENV['WECHAT_TOKEN'], access_token: ENV['WECHAT_ACCESS_TOKEN'], encrypt_mode: ENV['WECHAT_ENCRYPT_MODE'], timeout: ENV['WECHAT_TIMEOUT'], skip_verify_ssl: ENV['WECHAT_SKIP_VERIFY_SSL'], encoding_aes_key: ENV['WECHAT_ENCODING_AES_KEY'], jsapi_ticket: ENV['WECHAT_JSAPI_TICKET'], trusted_domain_fullname: ENV['WECHAT_TRUSTED_DOMAIN_FULLNAME'] } end |
.config_from_file ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/wechat/api_loader.rb', line 45 def self.config_from_file if defined?(::Rails) config_file = Rails.root.join('config/wechat.yml') return YAML.load(ERB.new(File.read(config_file)).result)[Rails.env] if File.exist?(config_file) else rails_config_file = File.join(Dir.getwd, 'config/wechat.yml') home_config_file = File.join(Dir.home, '.wechat.yml') if File.exist?(rails_config_file) rails_env = ENV['RAILS_ENV'] || 'default' config = YAML.load(ERB.new(File.read(rails_config_file)).result)[rails_env] if config.present? && (config['appid'] || config['corpid']) puts "Using rails project config/wechat.yml #{rails_env} setting..." return config end end if File.exist?(home_config_file) return YAML.load ERB.new(File.read(home_config_file)).result end end end |
.loading_config! ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/wechat/api_loader.rb', line 32 def self.loading_config! config ||= config_from_file || config_from_environment if defined?(::Rails) config[:access_token] ||= Rails.root.join('tmp/access_token').to_s config[:jsapi_ticket] ||= Rails.root.join('tmp/jsapi_ticket').to_s end config[:timeout] ||= 20 config[:have_session_class] = class_exists?('WechatSession') config.symbolize_keys! @config = OpenStruct.new(config) end |
.with(options) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/wechat/api_loader.rb', line 3 def self.with() c = ApiLoader.config token_file = [:token_file] || c.access_token || '/var/tmp/wechat_access_token' js_token_file = [:js_token_file] || c.jsapi_ticket || '/var/tmp/wechat_jsapi_ticket' if c.appid && c.secret && token_file.present? Wechat::Api.new(c.appid, c.secret, token_file, c.timeout, c.skip_verify_ssl, js_token_file) elsif c.corpid && c.corpsecret && token_file.present? Wechat::CorpApi.new(c.corpid, c.corpsecret, token_file, c.agentid, c.timeout, c.skip_verify_ssl, js_token_file) else puts "Need create ~/.wechat.yml with wechat appid and secret\nor running at rails root folder so wechat can read config/wechat.yml\n" exit 1 end end |