Class: Wechat::Adapter::WechatAPI
- Inherits:
-
Object
- Object
- Wechat::Adapter::WechatAPI
- Defined in:
- lib/wechat/adapter/wechatapi.rb
Instance Method Summary collapse
- #aquire_access_token ⇒ Object
- #get(path, options = {}) ⇒ Object
- #handle_err_response(values) ⇒ Object
-
#initialize(appid, appsecret, base = 'https://api.weixin.qq.com/cgi-bin') ⇒ WechatAPI
constructor
A new instance of WechatAPI.
- #json_request(*args, &block) ⇒ Object
- #post(path, options = {}, payload = nil) ⇒ Object
- #raw_json_request(method, path, params = {}, payload = nil) ⇒ Object
Constructor Details
#initialize(appid, appsecret, base = 'https://api.weixin.qq.com/cgi-bin') ⇒ WechatAPI
Returns a new instance of WechatAPI.
16 17 18 |
# File 'lib/wechat/adapter/wechatapi.rb', line 16 def initialize appid, appsecret, base = 'https://api.weixin.qq.com/cgi-bin' @appid, @appsecret, @base = appid, appsecret, base end |
Instance Method Details
#aquire_access_token ⇒ Object
52 53 54 55 56 57 58 59 |
# File 'lib/wechat/adapter/wechatapi.rb', line 52 def aquire_access_token values = raw_json_request :get, 'token', { :grant_type => 'client_credential', :appid => @appid, :secret => @appsecret } @access_token = values["access_token"] end |
#get(path, options = {}) ⇒ Object
65 66 67 |
# File 'lib/wechat/adapter/wechatapi.rb', line 65 def get path, = {} json_request(:get, path, ) end |
#handle_err_response(values) ⇒ Object
20 21 22 23 24 25 |
# File 'lib/wechat/adapter/wechatapi.rb', line 20 def handle_err_response values code = values["errcode"] return if code == 0 raise TokenExpiredError if [42001, 40014].include? code raise ResponseError.new(code, values['errmsg']) end |
#json_request(*args, &block) ⇒ Object
42 43 44 45 46 47 48 49 50 |
# File 'lib/wechat/adapter/wechatapi.rb', line 42 def json_request *args, &block begin aquire_access_token unless @access_token response = raw_json_request *args, &block rescue TokenExpiredError aquire_access_token retry end end |
#post(path, options = {}, payload = nil) ⇒ Object
61 62 63 |
# File 'lib/wechat/adapter/wechatapi.rb', line 61 def post path, = {}, payload = nil json_request(:post, path, , payload) end |
#raw_json_request(method, path, params = {}, payload = nil) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/wechat/adapter/wechatapi.rb', line 27 def raw_json_request method, path, params = {}, payload = nil params[:access_token] = @access_token if @access_token url = "#{@base}/#{path}" response = ::RestClient::Request.execute( :method => method, :url => url, :headers => { :params => params }, :payload => (payload.to_json if payload), :content_type => :json) values = JSON.parse(response.body) values["errcode"] ? handle_err_response(values) : values end |