Class: Wechat::Api::Client
- Inherits:
-
Object
- Object
- Wechat::Api::Client
- Defined in:
- lib/wechat/api/client.rb
Constant Summary collapse
- API_BASE =
'https://api.weixin.qq.com/cgi-bin/'
Instance Attribute Summary collapse
-
#app_id ⇒ Object
readonly
Returns the value of attribute app_id.
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#secret ⇒ Object
readonly
Returns the value of attribute secret.
Instance Method Summary collapse
- #access_token ⇒ Object
- #get(uri, params = {}) ⇒ Object
-
#initialize(app_id, secret) ⇒ Client
constructor
A new instance of Client.
- #post(uri, data = {}) ⇒ Object
- #refresh ⇒ Object
- #with_access_token(uri, params, tried = 2) ⇒ Object
Methods included from Util
#create_qrcode, #create_qrcode_temp
Methods included from User
Methods included from Message
Constructor Details
#initialize(app_id, secret) ⇒ Client
Returns a new instance of Client.
24 25 26 27 28 |
# File 'lib/wechat/api/client.rb', line 24 def initialize(app_id, secret) @app_id, @secret = app_id, secret @logger = Logger.new(STDOUT) @token_file = File.join('/tmp', "wechat-api-#{app_id}") end |
Instance Attribute Details
#app_id ⇒ Object (readonly)
Returns the value of attribute app_id.
21 22 23 |
# File 'lib/wechat/api/client.rb', line 21 def app_id @app_id end |
#logger ⇒ Object
Returns the value of attribute logger.
22 23 24 |
# File 'lib/wechat/api/client.rb', line 22 def logger @logger end |
#secret ⇒ Object (readonly)
Returns the value of attribute secret.
21 22 23 |
# File 'lib/wechat/api/client.rb', line 21 def secret @secret end |
Instance Method Details
#access_token ⇒ Object
30 31 32 33 34 35 36 37 |
# File 'lib/wechat/api/client.rb', line 30 def access_token @access_token ||= begin token = MultiJson.load(File.read(@token_file)) token['access_token'] rescue refresh end end |
#get(uri, params = {}) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/wechat/api/client.rb', line 49 def get(uri, params = {}) with_access_token(uri, params) do |url, params_with_token| debug_request do connection.get do |req| req.url url, params_with_token req.headers[:accept] = 'application/json' req.headers[:content_type] = 'application/json' end end end end |
#post(uri, data = {}) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/wechat/api/client.rb', line 61 def post(uri, data = {}) with_access_token(uri, {}) do |url, params| debug_request do connection.post do |req| req.url url, params req.headers[:accept] = 'application/json' req.headers[:content_type] = 'application/json' req.body = MultiJson.dump(data) end end end end |
#refresh ⇒ Object
39 40 41 42 43 44 45 46 47 |
# File 'lib/wechat/api/client.rb', line 39 def refresh url = format('%stoken', API_BASE) resp = connection.get(url, token_params) response = MultiJson.load(resp.body) return handle_error(response) if response['errcode'] @access_token = response['access_token'] File.open(@token_file, 'w') { |f| f.write(resp.body) } if token @access_token end |
#with_access_token(uri, params, tried = 2) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/wechat/api/client.rb', line 74 def with_access_token(uri, params, tried = 2) url = format('%s%s', API_BASE, uri) begin resp = yield(url, params.merge(access_token: access_token)) response = MultiJson.load(resp.body) handle_error(response) rescue AccessTokenExpiredError => e refresh retry unless (tried -= 1).zero? raise e end end |