Class: Wechat::Qy::Client
- Inherits:
-
Object
- Object
- Wechat::Qy::Client
- Defined in:
- lib/wechat/qy/client.rb
Constant Summary collapse
- API_BASE =
'https://qyapi.weixin.qq.com/cgi-bin/'
Instance Attribute Summary collapse
-
#corp_id ⇒ Object
readonly
Returns the value of attribute corp_id.
-
#corp_secret ⇒ Object
readonly
Returns the value of attribute corp_secret.
-
#logger ⇒ Object
Returns the value of attribute logger.
Instance Method Summary collapse
- #access_token ⇒ Object
- #get(uri, params = {}) ⇒ Object
-
#initialize(corp_id, corp_secret) ⇒ Client
constructor
A new instance of Client.
- #post(uri, data = {}) ⇒ Object
- #refresh ⇒ Object
- #with_access_token(uri, params, tried = 2) ⇒ Object
Constructor Details
#initialize(corp_id, corp_secret) ⇒ Client
Returns a new instance of Client.
18 19 20 21 22 |
# File 'lib/wechat/qy/client.rb', line 18 def initialize(corp_id, corp_secret) @corp_id, @corp_secret = corp_id, corp_secret @logger = Logger.new(STDOUT) @token_file = File.join('/tmp', "wechat-api-#{corp_id}") end |
Instance Attribute Details
#corp_id ⇒ Object (readonly)
Returns the value of attribute corp_id.
15 16 17 |
# File 'lib/wechat/qy/client.rb', line 15 def corp_id @corp_id end |
#corp_secret ⇒ Object (readonly)
Returns the value of attribute corp_secret.
15 16 17 |
# File 'lib/wechat/qy/client.rb', line 15 def corp_secret @corp_secret end |
#logger ⇒ Object
Returns the value of attribute logger.
16 17 18 |
# File 'lib/wechat/qy/client.rb', line 16 def logger @logger end |
Instance Method Details
#access_token ⇒ Object
24 25 26 27 28 29 30 31 |
# File 'lib/wechat/qy/client.rb', line 24 def access_token @access_token ||= begin token = MultiJson.load(File.read(@token_file)) token['access_token'] rescue refresh end end |
#get(uri, params = {}) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/wechat/qy/client.rb', line 43 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
55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/wechat/qy/client.rb', line 55 def post(uri, data = {}) with_access_token(uri, {}) do |url, params| logger.debug { [:data, data] } 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
33 34 35 36 37 38 39 40 41 |
# File 'lib/wechat/qy/client.rb', line 33 def refresh url = format('%sgettoken', 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 @access_token @access_token end |
#with_access_token(uri, params, tried = 2) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/wechat/qy/client.rb', line 69 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 |