Commit a1513769 by lanrion

refactor codes

parent 721a335f
...@@ -18,20 +18,17 @@ module WeixinAuthorize ...@@ -18,20 +18,17 @@ module WeixinAuthorize
end end
def valid? def valid?
valid["valid"] authenticate["valid"]
end end
def valid def authenticate
valid_result = http_get_access_token auth_result = http_get_access_token
if valid_result.code == OK_CODE auth = false
set_access_token_for_client(valid_result.result) if auth_result.is_ok?
return {"valid" => true, "handler" => valid_result} set_access_token_for_client(auth_result.result)
auth = true
end end
{"valid" => false, "handler" => valid_result} {"valid" => auth, "handler" => auth_result}
end
def token_expired?
raise NotImplementedError, "Subclasses must implement a token_expired? method"
end end
def refresh_token def refresh_token
...@@ -43,6 +40,10 @@ module WeixinAuthorize ...@@ -43,6 +40,10 @@ module WeixinAuthorize
refresh_token if token_expired? refresh_token if token_expired?
end end
def token_expired?
raise NotImplementedError, "Subclasses must implement a token_expired? method"
end
def set_access_token_for_client(access_token_infos=nil) def set_access_token_for_client(access_token_infos=nil)
token_infos = access_token_infos || http_get_access_token.result token_infos = access_token_infos || http_get_access_token.result
client.access_token = token_infos["access_token"] client.access_token = token_infos["access_token"]
...@@ -60,9 +61,9 @@ module WeixinAuthorize ...@@ -60,9 +61,9 @@ module WeixinAuthorize
private private
def handle_valid_exception def handle_valid_exception
valid_result = valid auth_result = authenticate
if !valid_result["valid"] if !auth_result["valid"]
result_handler = valid_result["handler"] result_handler = auth_result["handler"]
raise ValidAccessTokenException, "#{result_handler.code}:#{result_handler.en_msg}(#{result_handler.cn_msg})" raise ValidAccessTokenException, "#{result_handler.code}:#{result_handler.en_msg}(#{result_handler.cn_msg})"
end end
end end
......
# encoding: utf-8 # encoding: utf-8
module WeixinAuthorize module WeixinAuthorize
class ResultHandler class ResultHandler
...@@ -12,6 +11,21 @@ module WeixinAuthorize ...@@ -12,6 +11,21 @@ module WeixinAuthorize
@result = package_result(result) @result = package_result(result)
end 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 private
# if define Rails constant # 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