Class: WechatPublic::Api

Inherits:
Object
  • Object
show all
Defined in:
lib/wechat/api.rb

Constant Summary collapse

API_BASE =
"https://api.weixin.qq.com/cgi-bin/"
FILE_BASE =
"http://file.api.weixin.qq.com/cgi-bin/"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(appid, secret, token_file) ⇒ Api

Returns a new instance of Api.



10
11
12
13
# File 'lib/wechat/api.rb', line 10

def initialize appid, secret, token_file
  @client = WechatPublic::Client.new(API_BASE)
  @access_token = WechatPublic::AccessToken.new(@client, appid, secret, token_file)
end

Instance Attribute Details

#access_tokenObject (readonly)

Returns the value of attribute access_token.



5
6
7
# File 'lib/wechat/api.rb', line 5

def access_token
  @access_token
end

#clientObject (readonly)

Returns the value of attribute client.



5
6
7
# File 'lib/wechat/api.rb', line 5

def client
  @client
end

Instance Method Details

#custom_message_send(message) ⇒ Object



45
46
47
# File 'lib/wechat/api.rb', line 45

def custom_message_send message
  post "message/custom/send", message.to_json, content_type: :json
end

#media(media_id) ⇒ Object



37
38
39
# File 'lib/wechat/api.rb', line 37

def media media_id
  response = get "media/get", params:{media_id: media_id}, base: FILE_BASE, as: :file
end

#media_create(type, file) ⇒ Object



41
42
43
# File 'lib/wechat/api.rb', line 41

def media_create type, file
  post "media/upload", {upload:{media: file}}, params:{type: type}, base: FILE_BASE
end


24
25
26
# File 'lib/wechat/api.rb', line 24

def menu
  get("menu/get")
end


32
33
34
35
# File 'lib/wechat/api.rb', line 32

def menu_create menu
  # 微信不接受7bit escaped json(eg \uxxxx), 中文必须UTF-8编码, 这可能是个安全漏洞
  post("menu/create", JSON.generate(menu))
end


28
29
30
# File 'lib/wechat/api.rb', line 28

def menu_delete
  get("menu/delete")
end

#template_message_send(message) ⇒ Object



49
50
51
# File 'lib/wechat/api.rb', line 49

def template_message_send message
  post "message/template/send", message.to_json, content_type: :json
end

#user(openid) ⇒ Object



20
21
22
# File 'lib/wechat/api.rb', line 20

def user openid
  get("user/info", params:{openid: openid})
end

#users(nextid = nil) ⇒ Object



15
16
17
18
# File 'lib/wechat/api.rb', line 15

def users nextid = nil
  params = {params: {next_openid: nextid}} if nextid.present?
  get('user/get', params||{})
end