Commit 45a505f9 by lanrion

refactor client.rb with "http_get" and "http_post" method

parent 41f8fbc0
# encoding: utf-8 # encoding: utf-8
module WeixinAuthorize module WeixinAuthorize
class Client class Client
...@@ -29,15 +28,15 @@ module WeixinAuthorize ...@@ -29,15 +28,15 @@ module WeixinAuthorize
# authenticate access_token # authenticate access_token
def authenticate def authenticate
hash_infos = JSON.parse(RestClient.get(authenticate_url)) hash_infos = http_get_without_token("/token", authenticate_options)
self.access_token = hash_infos["access_token"] self.access_token = hash_infos["access_token"]
self.expired_at = Time.now.to_i + hash_infos["expires_in"] self.expired_at = Time.now.to_i + hash_infos["expires_in"]
end end
private private
def authenticate_url def authenticate_options
"#{endpoint}/token?grant_type=client_credential&appid=#{app_id}&secret=#{app_secret}" {grant_type: "client_credential", appid: app_id, secret: app_secret}
end end
def endpoint def endpoint
...@@ -45,7 +44,23 @@ module WeixinAuthorize ...@@ -45,7 +44,23 @@ module WeixinAuthorize
end end
def access_token_param def access_token_param
"access_token=#{get_access_token}" {access_token: get_access_token}
end
def http_get_without_token(url, options={})
get_api_url = endpoint + url
JSON.parse(RestClient.get(get_api_url, :params => options))
end
def http_get(url, options={})
options = options.merge(access_token_param)
http_get_without_token(url, options)
end
# Refactor
def http_post(url, options={})
post_api_url = endpoint + url + "?access_token=#{get_access_token}"
JSON.parse(RestClient.post(post_api_url ,options))
end end
end end
......
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