module WeixinAuthorize

Constants

CUSTOM_ENDPOINT

用于标记endpoint可以直接使用url作为完整请求API

GLOBAL_CODES
GRANT_TYPE
OK_CODE
OK_MSG
VERSION

Attributes

config[RW]

Public Class Methods

api_endpoint() click to toggle source
# File lib/weixin_authorize.rb, line 75
def api_endpoint
  "https://api.weixin.qq.com"
end
calculate_expire(expires_in) click to toggle source
# File lib/weixin_authorize.rb, line 91
def calculate_expire(expires_in)
  Time.now.to_i + expires_in.to_i - key_expired.to_i
end
configure() { |config ||= config| ... } click to toggle source
# File lib/weixin_authorize/config.rb, line 7
def configure
  yield self.config ||= Config.new
end
endpoint_url(endpoint, url) click to toggle source
# File lib/weixin_authorize.rb, line 65
def endpoint_url(endpoint, url)
  # 此处为了应对第三方开发者如果自助对接接口时,URL不规范的情况下,可以直接使用URL当为endpoint
  return url if endpoint == CUSTOM_ENDPOINT
  send("#{endpoint}_endpoint") + url
end
file_endpoint() click to toggle source
# File lib/weixin_authorize.rb, line 79
def file_endpoint
  "http://file.api.weixin.qq.com/cgi-bin"
end
http_get_without_token(url, url_params={}, endpoint="plain") click to toggle source
# File lib/weixin_authorize.rb, line 39
def http_get_without_token(url, url_params={}, endpoint="plain")
  get_api_url = endpoint_url(endpoint, url)
  load_json(resource(get_api_url).get(params: url_params))
end
http_post_without_token(url, post_body={}, url_params={}, endpoint="plain") click to toggle source
# File lib/weixin_authorize.rb, line 44
def http_post_without_token(url, post_body={}, url_params={}, endpoint="plain")
  post_api_url = endpoint_url(endpoint, url)
  # to json if invoke "plain"
  if endpoint == "plain" || endpoint == CUSTOM_ENDPOINT
    post_body = JSON.dump(post_body)
  end
  load_json(resource(post_api_url).post(post_body, params: url_params))
end
key_expired() click to toggle source
# File lib/weixin_authorize/config.rb, line 16
def key_expired
  config.key_expired || 100
end
load_json(string) click to toggle source

return hash

# File lib/weixin_authorize.rb, line 58
def load_json(string)
  result_hash = JSON.parse(string.force_encoding("UTF-8").gsub(/[\u0011-\u001F]/, ""))
  code   = result_hash.delete("errcode")
  en_msg = result_hash.delete("errmsg")
  ResultHandler.new(code, en_msg, result_hash)
end
mp_endpoint(url) click to toggle source
# File lib/weixin_authorize.rb, line 83
def mp_endpoint(url)
  "https://mp.weixin.qq.com/cgi-bin#{url}"
end
open_endpoint(url) click to toggle source
# File lib/weixin_authorize.rb, line 87
def open_endpoint(url)
  "https://open.weixin.qq.com#{url}"
end
plain_endpoint() click to toggle source
# File lib/weixin_authorize.rb, line 71
def plain_endpoint
  "#{api_endpoint}/cgi-bin"
end
resource(url) click to toggle source
# File lib/weixin_authorize.rb, line 53
def resource(url)
  RestClient::Resource.new(url, rest_client_options)
end
rest_client_options() click to toggle source

可选配置: RestClient timeout, etc. key 必须是符号 如果出现 RestClient::SSLCertificateNotVerified Exception: SSL_connect returned=1 errno=0 state=SSLv3 read server certificate B: certificate verify failed 这个错,除了改 verify_ssl: true,请参考:www.extendi.it/blog/2015/5/23/47-sslv3-read-server-certificate-b-certificate-verify-failed

# File lib/weixin_authorize/config.rb, line 24
def rest_client_options
  if config.nil?
    return {timeout: 5, open_timeout: 5, verify_ssl: true}
  end
  config.rest_client_options
end
weixin_redis() click to toggle source
# File lib/weixin_authorize/config.rb, line 11
def weixin_redis
  return nil if config.nil?
  @redis ||= config.redis
end