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.
-
#site ⇒ Object
Returns the value of attribute site.
-
#ticket ⇒ Object
readonly
Returns the value of attribute ticket.
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, #js_config, #js_ticket, #sign
Methods included from User
Methods included from Message
Constructor Details
#initialize(app_id, secret) ⇒ Client
Returns a new instance of Client.
25 26 27 28 29 30 |
# File 'lib/wechat/api/client.rb', line 25 def initialize(app_id, secret) @app_id, @secret = app_id, secret @logger = Logger.new(STDOUT) @ticket = JsTicket.new self @token_file = File.join('/tmp', "wechat-api-#{app_id}") end |
Instance Attribute Details
#app_id ⇒ Object (readonly)
Returns the value of attribute app_id.
22 23 24 |
# File 'lib/wechat/api/client.rb', line 22 def app_id @app_id end |
#logger ⇒ Object
Returns the value of attribute logger.
23 24 25 |
# File 'lib/wechat/api/client.rb', line 23 def logger @logger end |
#secret ⇒ Object (readonly)
Returns the value of attribute secret.
22 23 24 |
# File 'lib/wechat/api/client.rb', line 22 def secret @secret end |
#site ⇒ Object
Returns the value of attribute site.
23 24 25 |
# File 'lib/wechat/api/client.rb', line 23 def site @site end |
#ticket ⇒ Object (readonly)
Returns the value of attribute ticket.
22 23 24 |
# File 'lib/wechat/api/client.rb', line 22 def ticket @ticket end |
Instance Method Details
#access_token ⇒ Object
32 33 34 35 36 37 38 39 |
# File 'lib/wechat/api/client.rb', line 32 def access_token @access_token ||= begin token = MultiJson.load(File.read(@token_file)) token['access_token'] rescue refresh end end |
#get(uri, params = {}) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/wechat/api/client.rb', line 52 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
64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/wechat/api/client.rb', line 64 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
41 42 43 44 45 46 47 48 49 50 |
# File 'lib/wechat/api/client.rb', line 41 def refresh url = format('%stoken', base_url) 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
77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/wechat/api/client.rb', line 77 def with_access_token(uri, params, tried = 2) url = format('%s%s', base_url, uri) begin resp = yield(url, params.merge(access_token: access_token)) raise ResponseError, resp unless resp.success? response = MultiJson.load(resp.body) handle_error(response) rescue AccessTokenExpiredError => e refresh retry unless (tried -= 1).zero? raise e end end |