class Wechat::ApiBase

Constants

API_BASE
DATACUBE_BASE
MP_BASE
OAUTH2_BASE
QYAPI_BASE
TCB_BASE
WXA_BASE

Attributes

access_token[R]
client[R]
jsapi_ticket[R]
qcloud[R]

Public Instance Methods

callbackip() click to toggle source
# File lib/wechat/api_base.rb, line 15
def callbackip
  get 'getcallbackip'
end
clear_quota() click to toggle source
# File lib/wechat/api_base.rb, line 43
def clear_quota
  post 'clear_quota', JSON.generate(appid: Wechat.config[:appid])
end
media(media_id) click to toggle source
# File lib/wechat/api_base.rb, line 23
def media(media_id)
  get 'media/get', params: { media_id: media_id }, as: :file
end
media_create(type, file) click to toggle source
# File lib/wechat/api_base.rb, line 31
def media_create(type, file)
  post_file 'media/upload', file, params: { type: type }
end
media_hq(media_id) click to toggle source
# File lib/wechat/api_base.rb, line 27
def media_hq(media_id)
  get 'media/get/jssdk', params: { media_id: media_id }, as: :file
end
media_uploadimg(file) click to toggle source
# File lib/wechat/api_base.rb, line 35
def media_uploadimg(file)
  post_file 'media/uploadimg', file
end
media_uploadnews(mpnews_message) click to toggle source
# File lib/wechat/api_base.rb, line 39
def media_uploadnews(mpnews_message)
  post 'media/uploadnews', mpnews_message.to_json
end
qrcode(ticket) click to toggle source
# File lib/wechat/api_base.rb, line 19
def qrcode(ticket)
  client.get 'showqrcode', params: { ticket: ticket }, base: MP_BASE, as: :file
end

Protected Instance Methods

get(path, headers = {}) click to toggle source
# File lib/wechat/api_base.rb, line 49
def get(path, headers = {})
  with_access_token(headers[:params]) do |params|
    client.get path, headers.merge(params: params)
  end
end
post(path, payload, headers = {}) click to toggle source
# File lib/wechat/api_base.rb, line 55
def post(path, payload, headers = {})
  with_access_token(headers[:params]) do |params|
    client.post path, payload, headers.merge(params: params)
  end
end
post_file(path, file, headers = {}) click to toggle source
# File lib/wechat/api_base.rb, line 61
def post_file(path, file, headers = {})
  with_access_token(headers[:params]) do |params|
    client.post_file path, file, headers.merge(params: params)
  end
end
with_access_token(params = {}, tries = 2) { |merge(access_token: token)| ... } click to toggle source
# File lib/wechat/api_base.rb, line 67
def with_access_token(params = {}, tries = 2)
  params ||= {}
  yield(params.merge(access_token: access_token.token))
rescue AccessTokenExpiredError
  access_token.refresh
  retry unless (tries -= 1).zero?
end