class Haoyaoshi::Client

Attributes

access_token[RW]
channel[RW]
client_id[RW]
client_secret[RW]
custom_access_token[RW]
expired_at[RW]
grant_type[RW]
parnterkey[RW]
redis_key[RW]
scope[RW]
token_type[RW]
token_type_token[RW]

Public Class Methods

new(client_id, client_secret, channel , parnterkey, grant_type = "client_credentials" , scope = "order", options={}) click to toggle source

options: redis_key, custom_access_token

Calls superclass method
# File lib/haoyaoshi/client.rb, line 23
def initialize(client_id, client_secret, channel , parnterkey, grant_type = "client_credentials" , scope = "order", options={})
  @client_id = client_id
  @client_secret = client_secret
  @expired_at = Time.now.to_i
  @channel = channel
  @parnterkey = parnterkey
  @grant_type =  grant_type if grant_type.present?
  @scope = scope
  @redis_key = security_redis_key(options[:redis_key] || "haoyaoshi_#{client_id}")
  @custom_access_token = options[:custom_access_token]
  @custom_token_type = options[:custom_token_type]
  super() # Monitor#initialize
end

Public Instance Methods

get_access_token() click to toggle source

return token

# File lib/haoyaoshi/client.rb, line 38
def get_access_token
  return custom_access_token if !custom_access_token.nil?
  synchronize{ token_store.access_token }
end
get_token_type() click to toggle source

return token

# File lib/haoyaoshi/client.rb, line 44
def get_token_type
  return custom_token_type if !custom_token_type.nil?
  synchronize{ token_store.token_type }
end
http_delete(url, delete_body={}, url_params={}, endpoint="plain") click to toggle source
# File lib/haoyaoshi/client.rb, line 70
def http_delete(url, delete_body={}, url_params={}, endpoint="plain")
  url_params = access_token_param.merge(url_params)
  url = "#{url}?access_token=#{get_access_token}"
  Haoyaoshi.http_delete_without_token(url, delete_body, url_params, endpoint)
end
http_drug_get(url, url_params = {}, path = nil, endpoint="plain") click to toggle source

暴露出:http_get,http_post两个方法,方便第三方开发者扩展未开发的微信API。

# File lib/haoyaoshi/client.rb, line 77
def http_drug_get(url, url_params = {}, path = nil, endpoint="plain")
  url_params = {channel: @channel}.merge(url_params)
  tmp_option = url_params.deep_dup
  url_params[:sign] = sign(tmp_option,path)
  Rails.logger.info("#{url_params[:sign]}")
  Haoyaoshi.http_get_without_token(url, url_params, endpoint)
end
http_get(url, url_params={}, endpoint="plain") click to toggle source

暴露出:http_get,http_post两个方法,方便第三方开发者扩展未开发的微信API。

# File lib/haoyaoshi/client.rb, line 60
def http_get(url, url_params={}, endpoint="plain")
  url_params = url_params.merge(access_token_param)
  Haoyaoshi.http_get_without_token(url, url_params, endpoint)
end
http_post(url, post_body={}, url_params={}, endpoint="plain") click to toggle source
# File lib/haoyaoshi/client.rb, line 65
def http_post(url, post_body={}, url_params={}, endpoint="plain")
  url_params = access_token_param.merge(url_params)
  Haoyaoshi.http_post_without_token(url, post_body, url_params, endpoint)
end
is_valid?() click to toggle source

检查appid和app_secret是否有效。

# File lib/haoyaoshi/client.rb, line 50
def is_valid?
  return true if !custom_access_token.nil?
  token_store.valid?
end
token_store() click to toggle source
# File lib/haoyaoshi/client.rb, line 55
def token_store
  Token::Store.init_with(self)
end

Private Instance Methods

access_token_param() click to toggle source
# File lib/haoyaoshi/client.rb, line 87
def access_token_param
  {access_token: get_access_token}
end
security_redis_key(key) click to toggle source
# File lib/haoyaoshi/client.rb, line 91
def security_redis_key(key)
  Digest::MD5.hexdigest(key.to_s).upcase
end
sign(params,path) click to toggle source

生成签名

# File lib/haoyaoshi/client.rb, line 96
def sign(params,path)
  option = params.sort().collect{|key,value| "#{key}#{value.to_s}"}.join
  Rails.logger.info("#{path}#{option.to_s}#{@parnterkey}")
  Digest::SHA1.hexdigest("#{path}#{option.to_s}#{@parnterkey}")
end