Commit a1513769 by lanrion

refactor codes

parent 721a335f
......@@ -18,20 +18,17 @@ module WeixinAuthorize
end
def valid?
valid["valid"]
authenticate["valid"]
end
def valid
valid_result = http_get_access_token
if valid_result.code == OK_CODE
set_access_token_for_client(valid_result.result)
return {"valid" => true, "handler" => valid_result}
def authenticate
auth_result = http_get_access_token
auth = false
if auth_result.is_ok?
set_access_token_for_client(auth_result.result)
auth = true
end
{"valid" => false, "handler" => valid_result}
end
def token_expired?
raise NotImplementedError, "Subclasses must implement a token_expired? method"
{"valid" => auth, "handler" => auth_result}
end
def refresh_token
......@@ -43,6 +40,10 @@ module WeixinAuthorize
refresh_token if token_expired?
end
def token_expired?
raise NotImplementedError, "Subclasses must implement a token_expired? method"
end
def set_access_token_for_client(access_token_infos=nil)
token_infos = access_token_infos || http_get_access_token.result
client.access_token = token_infos["access_token"]
......@@ -60,9 +61,9 @@ module WeixinAuthorize
private
def handle_valid_exception
valid_result = valid
if !valid_result["valid"]
result_handler = valid_result["handler"]
auth_result = authenticate
if !auth_result["valid"]
result_handler = auth_result["handler"]
raise ValidAccessTokenException, "#{result_handler.code}:#{result_handler.en_msg}(#{result_handler.cn_msg})"
end
end
......
# encoding: utf-8
module WeixinAuthorize
class ResultHandler
......@@ -12,6 +11,21 @@ module WeixinAuthorize
@result = package_result(result)
end
# This method is to valid the current request if is true or is false
def is_ok?
code == OK_CODE
end
# e.g.:
# 45009: api freq out of limit(接口调用超过限制)
def full_message
"#{code}: #{en_msg}(#{cn_msg})."
end
def full_error_message
full_message if !is_ok?
end
private
# if define Rails constant
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment