module WechatPublic
Constants
- API_BASE
- TICKET_URL
- VERSION
Attributes
config[R]
Public Class Methods
api()
click to toggle source
# File lib/wechat_public.rb, line 45 def self.api @api ||= WechatPublic::Api.new(self.config.appid, self.config.secret, self.config.access_token) end
config() { |config| ... }
click to toggle source
# File lib/wechat_public.rb, line 22 def self.config # @config ||= begin # if defined? Rails # config_file = Rails.root.join("config/wechat.yml") # config = YAML.load(ERB.new(File.new(config_file).read).result)[Rails.env] if (File.exist?(config_file)) # end # config ||= {appid: ENV["WECHAT_APPID"], secret: ENV["WECHAT_SECRET"], token: ENV["WECHAT_TOKEN"], access_token: ENV["WECHAT_ACCESS_TOKEN"]} # config.symbolize_keys! # config[:access_token] ||= Rails.root.join("tmp/access_token").to_s # puts '======' # puts config # OpenStruct.new(config) # end @config ||= begin config = {}; yield config puts '======',config @config = OpenStruct.new(config) end end
generate_ticket(hash)
click to toggle source
# File lib/wechat_public.rb, line 65 def self.generate_ticket hash hash ||= {} expire_seconds = hash.fetch("expire_seconds",10); action_name = hash.fetch("action_name","QR_LIMIT_SCENE") scene_id = hash.fetch("scene_id",Random.new.rand(100..10000)) form = { expire_seconds: expire_seconds, action_name: action_name, action_info:{ scene:{ scene_id: scene_id } } } body = RestClient.post(TICKET_URL+self.token,form.to_json,json_header) body = JSON.parse(body) puts body body end
get_token()
click to toggle source
# File lib/wechat_public.rb, line 49 def self.get_token unless @access_token client = WechatPublic::Client.new(API_BASE) @access_token = WechatPublic::AccessToken.new(client, self.config.appid, self.config.secret, self.config.access_token) end @access_token.token_data end
token()
click to toggle source
# File lib/wechat_public.rb, line 57 def self.token unless @access_token client = WechatPublic::Client.new(API_BASE) @access_token = WechatPublic::AccessToken.new(client, self.config.appid, self.config.secret, self.config.access_token) end @access_token.token end
Private Class Methods
json_header()
click to toggle source
# File lib/wechat_public.rb, line 85 def self.json_header { content_type: :json, accept: :json } end