Class: EasyWechat::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/easy_wechat/client.rb

Constant Summary collapse

TOKEN_URL =
"https://api.weixin.qq.com/cgi-bin/token"
BATCHGET_MATERIAL_URL =
"https://api.weixin.qq.com/cgi-bin/material/batchget_material"
UPLOADIMG_URL =
"https://api.weixin.qq.com/cgi-bin/media/uploadimg"
ADD_NEWS_URL =
"https://api.weixin.qq.com/cgi-bin/material/add_news"
GET_USER_SUMMARY_URL =
"https://api.weixin.qq.com/datacube/getusersummary"
GET_USER_CUMULATE_URL =
"https://api.weixin.qq.com/datacube/getusercumulate"
GET_TICKET_URL =
"https://api.weixin.qq.com/cgi-bin/ticket/getticket"

Instance Method Summary collapse

Constructor Details

#initializeClient

Returns a new instance of Client.



16
17
# File 'lib/easy_wechat/client.rb', line 16

def initialize
end

Instance Method Details

#access_tokenObject



19
20
21
# File 'lib/easy_wechat/client.rb', line 19

def access_token
  @access_token ||= token["access_token"]
end

#add_news(articles) {|r| ... } ⇒ Object

Yields:

  • (r)


59
60
61
62
63
64
65
66
67
# File 'lib/easy_wechat/client.rb', line 59

def add_news(articles)
  payload = { articles: articles }

  resp = HTTPX.post(ADD_NEWS_URL, params: { access_token: access_token }, json: payload)

  r = ::JSON.parse(resp.body, quirks_mode: true)
  yield r if block_given?
  r
end

#batchget_material(type = "image", offset = 0, count = 20) {|r| ... } ⇒ Object

Yields:

  • (r)


79
80
81
82
83
84
85
86
87
88
89
# File 'lib/easy_wechat/client.rb', line 79

def batchget_material(type = "image", offset = 0, count = 20)
  # TODO: 检查参数完整性
  # TODO: 检查参数合法性
  payload = { type: type, offset: offset, count: count }

  resp = HTTPX.post(BATCHGET_MATERIAL_URL, params: { access_token: access_token }, json: payload)

  r = ::JSON.parse(resp.body, quirks_mode: true)
  yield r if block_given?
  r
end

#get_ticket(type) {|r| ... } ⇒ Object

Yields:

  • (r)


69
70
71
72
73
74
75
# File 'lib/easy_wechat/client.rb', line 69

def get_ticket(type)
  resp = HTTPX.get(GET_TICKET_URL, params: { access_token: access_token, type: type })

  r = ::JSON.parse(resp.body, quirks_mode: true)
  yield r if block_given?
  r
end

#get_user_cumulate(begin_date, end_date) {|r| ... } ⇒ Object

Yields:

  • (r)


33
34
35
36
37
38
39
40
41
# File 'lib/easy_wechat/client.rb', line 33

def get_user_cumulate(begin_date, end_date)
  payload = { begin_date: begin_date, end_date: end_date }

  resp = HTTPX.post(GET_USER_CUMULATE_URL, params: { access_token: access_token }, json: payload)

  r = ::JSON.parse(resp.body, quirks_mode: true)
  yield r if block_given?
  r
end

#get_user_summary(begin_date, end_date) {|r| ... } ⇒ Object

Yields:

  • (r)


23
24
25
26
27
28
29
30
31
# File 'lib/easy_wechat/client.rb', line 23

def get_user_summary(begin_date, end_date)
  payload = { begin_date: begin_date, end_date: end_date }

  resp = HTTPX.post(GET_USER_SUMMARY_URL, params: { access_token: access_token }, json: payload)

  r = ::JSON.parse(resp.body, quirks_mode: true)
  yield r if block_given?
  r
end

Raises:

  • (NotImplementedError)


104
105
106
# File 'lib/easy_wechat/client.rb', line 104

def menu_create
  raise NotImplementedError
end

#token {|r| ... } ⇒ Object

Yields:

  • (r)


45
46
47
48
49
50
51
52
53
54
55
# File 'lib/easy_wechat/client.rb', line 45

def token
  resp = HTTPX.get(TOKEN_URL, params: {
                                appid: EasyWechat.app_id,
                                secret: EasyWechat.app_secret,
                                grant_type: "client_credential",
                              })

  r = ::JSON.parse(resp.body, quirks_mode: true)
  yield r if block_given?
  r
end

#uploadimg(media) {|r| ... } ⇒ Object

Yields:

  • (r)


93
94
95
96
97
98
99
100
101
102
# File 'lib/easy_wechat/client.rb', line 93

def uploadimg(media)
  # media path or Tempfile
  # https://honeyryderchuck.gitlab.io/httpx/wiki/Multipart-Uploads.html#notes
  file = HTTP::FormData::File.new(media)
  resp = HTTPX.plugin(:multipart).post(UPLOADIMG_URL, params: { access_token: access_token }, form: { media: file })

  r = ::JSON.parse(resp.body, quirks_mode: true)
  yield r if block_given?
  r
end