Class: Wechat::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 = Wechat::Client.new(API_BASE)
  @access_token = Wechat::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



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

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

#media(media_id) ⇒ Object



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

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

#media_create(type, file) ⇒ Object



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

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


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

def menu
  get("menu/get")
end


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

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


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

def menu_delete
  get("menu/delete")
end

#user(openid) ⇒ Object



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

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

#usersObject



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

def users
  get("user/get")
end