Module: Wechat::ApiLoader

Defined in:
lib/wechat/api_loader.rb

Class Method Summary collapse

Class Method Details

.config(account = :default) ⇒ Object



40
41
42
43
44
# File 'lib/wechat/api_loader.rb', line 40

def self.config( = :default)
   = :default if .nil?
  @configs ||= loading_config!
  @configs[.to_sym] || raise("Wechat configuration for #{} is missing.")
end

.load_yaml(result) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/wechat/api_loader.rb', line 50

def self.load_yaml(result)
  if YAML.respond_to?(:unsafe_load)
    YAML.unsafe_load(result)
  else
    begin
      YAML.safe_load(result, aliases: true)
    rescue ArgumentError
      YAML.safe_load(result)
    end
  end
end

.reload_config!Object



46
47
48
# File 'lib/wechat/api_loader.rb', line 46

def self.reload_config!
  @configs = loading_config!
end

.with(options) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/wechat/api_loader.rb', line 7

def self.with(options)
   = options[:account] || :default
  c = ApiLoader.config()

  token_file = options[:token_file] || c.access_token.presence || '/var/tmp/wechat_access_token'
  js_token_file = options[:js_token_file] || c.jsapi_ticket.presence || '/var/tmp/wechat_jsapi_ticket'
  type = options[:type] || c.type

  network_setting = Wechat::NetworkSetting.new(c.timeout, c.skip_verify_ssl, c.proxy_url, c.proxy_username, c.proxy_password)

  current_appid = c.corpid.presence || c.appid
  current_secret = c.corpsecret.presence || c.secret

  unless current_appid && current_secret && token_file.present?
    raise 'Need create ~/.wechat.yml with wechat appid and secret or corpid and corpsecret, or running at rails root folder so wechat can read config/wechat.yml'
  end

  api_config = Wechat::ApiConfig.new(current_appid, current_secret, token_file, js_token_file, network_setting)
  if type == 'mp'
    qcloud_env = options[:qcloud_env] || c.qcloud_env
    qcloud_token_file = options[:qcloud_token_file] || c.qcloud_token_file.presence || '/var/tmp/qcloud_access_token'
    qcloud_token_lifespan = options[:qcloud_token_lifespan] || c.qcloud_token_lifespan
    qcloud_setting = Wechat::Qcloud::Setting.new(qcloud_env, qcloud_token_file, qcloud_token_lifespan)
    Wechat::MpApi.new(api_config, qcloud_setting)
  elsif c.corpid.present? # CorpApi needs agentid
    Wechat::CorpApi.new(api_config, c.agentid)
  else # Regular Public Api
    Wechat::Api.new(api_config)
  end
end