jpush 4.0.8 → 4.0.10

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,52 @@
1
+ require 'jpush/push/notification'
2
+
3
+ module JPush
4
+ module Push
5
+ class SinglePushPayload
6
+
7
+ def initialize(platform: , target: )
8
+ @platform = 'all' == platform ? 'all' : build_platform(platform)
9
+ @target = target
10
+ end
11
+
12
+ def set_notification(notification)
13
+ @notification = notification
14
+ self
15
+ end
16
+
17
+ def set_message(message)
18
+ @message = message
19
+ self
20
+ end
21
+
22
+ def set_sms_message(sms_message)
23
+ @sms_message = sms_message
24
+ self
25
+ end
26
+
27
+ def set_options(options)
28
+ @options = options
29
+ self
30
+ end
31
+
32
+ def to_hash
33
+ single_push_payload = {
34
+ platform: @platform,
35
+ target: @target,
36
+ notification: @notification,
37
+ message: @message,
38
+ sms_message: @sms_message,
39
+ options: @options
40
+ }.select { |_, value| !value.nil? }
41
+ end
42
+
43
+ private
44
+
45
+ def build_platform(platform)
46
+ return platform if platform.is_a? Array
47
+ return [platform]
48
+ end
49
+
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,84 @@
1
+ require 'jpush/http/client'
2
+ require 'jpush/push/push_payload'
3
+ require 'jpush/push/single_push_payload'
4
+ require 'jpush/handler'
5
+
6
+ module JPush
7
+ class Pusher < Handler
8
+
9
+ # POST https://api.jpush.cn/v3/push/validate
10
+ # 验证推送调用是否能够成功,与推送 API 的区别在于:不向用户发送任何消息
11
+ def validate(push_payload)
12
+ url = base_url + 'validate'
13
+ send_push(url, push_payload)
14
+ end
15
+
16
+ # POST https://api.jpush.cn/v3/push
17
+ # 向某单个设备或者某设备列表推送一条通知、或者消息
18
+ def push(push_payload)
19
+ if push_payload.cid.nil?
20
+ cid_response = get_cid(count=1, type='push')
21
+ cid = cid_response.body['cidlist'].at(0)
22
+ push_payload.set_cid(cid)
23
+ end
24
+ send_push(base_url, push_payload)
25
+ end
26
+
27
+ # GET https://api.jpush.cn/v3/push/cid[?count=n[&type=xx]]
28
+ # 获取cid:推送唯一标识符
29
+ # https://docs.jiguang.cn/jpush/server/push/rest_api_v3_push/#cid
30
+ def get_cid(count=nil, type=nil)
31
+ params = {
32
+ count: count,
33
+ type: type
34
+ }.select { |_, value| !value.nil? }
35
+ url = base_url + 'cid'
36
+ Http::Client.get(@jpush, url, params: params)
37
+ end
38
+
39
+ # POST https://api.jpush.cn/v3/push/batch/regid/single
40
+ # 针对RegID方式批量单推(VIP专属接口)
41
+ # https://docs.jiguang.cn/jpush/server/push/rest_api_v3_push/#vip
42
+ def push_batch_regid(single_push_payloads)
43
+ cid_response = get_cid(count=single_push_payloads.size, type='push')
44
+ cidlist = cid_response.body['cidlist']
45
+ body = {}
46
+ body['pushlist'] = {}
47
+ single_push_payloads.each { |payload|
48
+ cid = cidlist.pop
49
+ body['pushlist'][cid] = payload.to_hash
50
+ }
51
+ url = base_url + 'batch/regid/single'
52
+ Http::Client.post(@jpush, url, body: body)
53
+ end
54
+
55
+ # POST https://api.jpush.cn/v3/push/batch/alias/single
56
+ # 针对Alias方式批量单推(VIP专属接口)
57
+ # https://docs.jiguang.cn/jpush/server/push/rest_api_v3_push/#vip
58
+ def push_batch_alias(single_push_payloads)
59
+ cid_response = get_cid(count=single_push_payloads.size, type='push')
60
+ cidlist = cid_response.body['cidlist']
61
+ body = {}
62
+ body['pushlist'] = {}
63
+ single_push_payloads.each { |payload|
64
+ cid = cidlist.pop
65
+ body['pushlist'][cid] = payload.to_hash
66
+ }
67
+ url = base_url + 'batch/alias/single'
68
+ Http::Client.post(@jpush, url, body: body)
69
+ end
70
+
71
+ private
72
+
73
+ def send_push(url, push_payload)
74
+ push_payload = push_payload.is_a?(JPush::Push::PushPayload) ? push_payload : nil
75
+ body = push_payload.to_hash
76
+ Http::Client.post(@jpush, url, body: body)
77
+ end
78
+
79
+ def base_url
80
+ 'https://api.jpush.cn/v3/push/'
81
+ end
82
+
83
+ end
84
+ end
@@ -1,59 +1,98 @@
1
- require 'jpush/http/client'
2
-
3
- module JPush
4
- module Report
5
- extend self
6
-
7
- TIME_UNIT = ['HOUR', 'DAY', 'MONTH']
8
- TIME_FORMAT = { hour: '%F %H', day: '%F', month: '%Y-%m' }
9
- MAX_DURATION = { hour: 24, day: 60, month: 2 }
10
-
11
- # GET /v3/received
12
- # 送达统计
13
- def received(msg_ids)
14
- msg_ids = [msg_ids].flatten
15
- url = base_url + '/received'
16
- params = {
17
- msg_ids: msg_ids.join(',')
18
- }
19
- Http::Client.get(url, params: params)
20
- end
21
-
22
- # GET /v3/messages
23
- # 消息统计
24
- def messages(msg_ids)
25
- msg_ids = [msg_ids].flatten
26
- url = base_url + '/messages'
27
- params = {
28
- msg_ids: msg_ids.join(',')
29
- }
30
- Http::Client.get(url, params: params)
31
- end
32
-
33
- # GET /v3/users
34
- # 用户统计
35
- def users(time_unit, start, duration)
36
- start = start.strftime(TIME_FORMAT[time_unit.downcase.to_sym])
37
- duration = build_duration(time_unit.downcase.to_sym, duration)
38
- params = {
39
- time_unit: time_unit.upcase,
40
- start: start,
41
- duration: duration
42
- }
43
- url = base_url + '/users'
44
- Http::Client.get(url, params: params)
45
- end
46
-
47
- private
48
-
49
- def base_url
50
- Config.settings[:report_api_host] + Config.settings[:api_version]
51
- end
52
-
53
- def build_duration(time_unit, duration)
54
- return 1 if duration < 0
55
- duration > MAX_DURATION[time_unit] ? MAX_DURATION[time_unit] : duration
56
- end
57
-
58
- end
59
- end
1
+ require 'jpush/http/client'
2
+ require 'jpush/handler'
3
+
4
+ module JPush
5
+ class Report < Handler
6
+
7
+ TIME_UNIT = ['HOUR', 'DAY', 'MONTH']
8
+ TIME_FORMAT = { hour: '%F %H', day: '%F', month: '%Y-%m' }
9
+ MAX_DURATION = { hour: 24, day: 60, month: 2 }
10
+
11
+ # GET /v3/received
12
+ # 送达统计
13
+ def received(msg_ids)
14
+ msg_ids = [msg_ids].flatten
15
+ url = base_url + '/received'
16
+ params = {
17
+ msg_ids: msg_ids.join(',')
18
+ }
19
+ Http::Client.get(@jpush, url, params: params)
20
+ end
21
+
22
+ # GET /v3/received/detail
23
+ # 送达统计
24
+ # https://docs.jiguang.cn/jpush/server/push/rest_api_v3_report/#_7
25
+ def received_detail(msg_ids)
26
+ msg_ids = [msg_ids].flatten
27
+ url = base_url + '/received/detail'
28
+ params = {
29
+ msg_ids: msg_ids.join(',')
30
+ }
31
+ Http::Client.get(@jpush, url, params: params)
32
+ end
33
+
34
+ # GET /v3/status/message
35
+ # 送达状态查询
36
+ # Status API 用于查询已推送的一条消息在一组设备上的送达状态。
37
+ # https://docs.jiguang.cn/jpush/server/push/rest_api_v3_report/#_11
38
+ def status_message(msg_id: , registration_ids: , date: nil)
39
+ registration_ids = [registration_ids].flatten
40
+ url = base_url + 'status/message'
41
+ body = {
42
+ msg_id: msg_id.to_i,
43
+ registration_ids: registration_ids,
44
+ date: date
45
+ }.select { |_, value| !value.nil? }
46
+ Http::Client.post(@jpush, url, body: body)
47
+ end
48
+
49
+ # GET /v3/messages
50
+ # 消息统计
51
+ def messages(msg_ids)
52
+ msg_ids = [msg_ids].flatten
53
+ url = base_url + '/messages'
54
+ params = {
55
+ msg_ids: msg_ids.join(',')
56
+ }
57
+ Http::Client.get(@jpush, url, params: params)
58
+ end
59
+
60
+ # GET /v3/messages/detail
61
+ # 消息统计详情(VIP 专属接口,新)
62
+ # https://docs.jiguang.cn/jpush/server/push/rest_api_v3_report/#vip_1
63
+ def messages_detail(msg_ids)
64
+ msg_ids = [msg_ids].flatten
65
+ url = base_url + '/messages/detail'
66
+ params = {
67
+ msg_ids: msg_ids.join(',')
68
+ }
69
+ Http::Client.get(@jpush, url, params: params)
70
+ end
71
+
72
+ # GET /v3/users
73
+ # 用户统计
74
+ def users(time_unit, start, duration)
75
+ start = start.strftime(TIME_FORMAT[time_unit.downcase.to_sym])
76
+ duration = build_duration(time_unit.downcase.to_sym, duration)
77
+ params = {
78
+ time_unit: time_unit.upcase,
79
+ start: start,
80
+ duration: duration
81
+ }
82
+ url = base_url + '/users'
83
+ Http::Client.get(@jpush, url, params: params)
84
+ end
85
+
86
+ private
87
+
88
+ def base_url
89
+ 'https://report.jpush.cn/v3/'
90
+ end
91
+
92
+ def build_duration(time_unit, duration)
93
+ return 1 if duration < 0
94
+ duration > MAX_DURATION[time_unit] ? MAX_DURATION[time_unit] : duration
95
+ end
96
+
97
+ end
98
+ end
@@ -1,48 +1,48 @@
1
- require 'jpush/push/push_payload'
2
- require 'jpush/schedule/trigger'
3
-
4
- module JPush
5
- module Schedule
6
- class SchedulePayload
7
-
8
- def initialize(name, trigger, push_payload, enabled = nil)
9
- @name = name
10
- @trigger = build_trigger(trigger)
11
- @push_payload = build_push_payload(push_payload)
12
- @enabled = enabled
13
- end
14
-
15
- def to_update_hash
16
- @schedule_payload = {
17
- name: @name,
18
- enabled: @enabled,
19
- trigger: @trigger,
20
- push: @push_payload
21
- }.select { |_, value| !value.nil? }
22
- raise Utils::Exceptions::JPushError, 'Schedule update body can not be empty' if @schedule_payload.empty?
23
- @schedule_payload
24
- end
25
-
26
- def to_hash
27
- @schedule_payload = {
28
- name: @name,
29
- enabled: true,
30
- trigger: @trigger,
31
- push: @push_payload
32
- }
33
- hash = @schedule_payload.select { |_, value| value.nil? }
34
- @schedule_payload
35
- end
36
-
37
- def build_trigger(trigger)
38
- return { single: { time: trigger.strftime('%F %T') } } if trigger.is_a? Time
39
- trigger.is_a?(Trigger) ? trigger.to_hash : nil
40
- end
41
-
42
- def build_push_payload(push_payload)
43
- push_payload.is_a?(Push::PushPayload) ? push_payload.to_hash : nil
44
- end
45
-
46
- end
47
- end
48
- end
1
+ require 'jpush/push/push_payload'
2
+ require 'jpush/schedule/trigger'
3
+
4
+ module JPush
5
+ module Schedule
6
+ class SchedulePayload
7
+
8
+ def initialize(name, trigger, push_payload, enabled = nil)
9
+ @name = name
10
+ @trigger = build_trigger(trigger)
11
+ @push_payload = build_push_payload(push_payload)
12
+ @enabled = enabled
13
+ end
14
+
15
+ def to_update_hash
16
+ @schedule_payload = {
17
+ name: @name,
18
+ enabled: @enabled,
19
+ trigger: @trigger,
20
+ push: @push_payload
21
+ }.select { |_, value| !value.nil? }
22
+ raise Utils::Exceptions::JPushError, 'Schedule update body can not be empty' if @schedule_payload.empty?
23
+ @schedule_payload
24
+ end
25
+
26
+ def to_hash
27
+ @schedule_payload = {
28
+ name: @name,
29
+ enabled: true,
30
+ trigger: @trigger,
31
+ push: @push_payload
32
+ }
33
+ hash = @schedule_payload.select { |_, value| value.nil? }
34
+ @schedule_payload
35
+ end
36
+
37
+ def build_trigger(trigger)
38
+ return { single: { time: trigger.strftime('%F %T') } } if trigger.is_a? Time
39
+ trigger.is_a?(Trigger) ? trigger.to_hash : nil
40
+ end
41
+
42
+ def build_push_payload(push_payload)
43
+ push_payload.is_a?(Push::PushPayload) ? push_payload.to_hash : nil
44
+ end
45
+
46
+ end
47
+ end
48
+ end
@@ -1,53 +1,53 @@
1
- module JPush
2
- module Schedule
3
- class Trigger
4
-
5
- WEEK = ['MON','TUE','WED','THU','FRI','SAT','SUN']
6
- MDAY = ('01'..'31').to_a
7
-
8
- def set_single(time)
9
- @periodical = nil
10
- @single = { time: time.strftime('%F %T') }
11
- self
12
- end
13
-
14
- def set_periodical(start_time, end_time, time, time_unit, frequency, point)
15
- @single = nil
16
- require 'time'
17
- frequency = 100 if frequency > 100
18
- @periodical = {
19
- start: start_time.strftime('%F %T'),
20
- end: end_time.strftime('%F %T'),
21
- time: Time.parse(time).strftime('%T'),
22
- time_unit: time_unit,
23
- frequency: frequency,
24
- point: build_point(time_unit, point)
25
- }
26
- self
27
- end
28
-
29
- def to_hash
30
- @trigger = {}
31
- @trigger[:single] = @single unless @single.nil?
32
- @trigger[:periodical] = @periodical unless @periodical.nil?
33
- @trigger
34
- end
35
-
36
- private
37
-
38
- def build_point(time_unit, point)
39
- array = [point].flatten
40
- point =
41
- case time_unit.upcase
42
- when 'DAY'
43
- nil
44
- when 'WEEK'
45
- WEEK & array.map{ |e| e.upcase }
46
- when 'MONTH'
47
- MDAY & array
48
- end
49
- end
50
-
51
- end
52
- end
53
- end
1
+ module JPush
2
+ module Schedule
3
+ class Trigger
4
+
5
+ WEEK = ['MON','TUE','WED','THU','FRI','SAT','SUN']
6
+ MDAY = ('01'..'31').to_a
7
+
8
+ def set_single(time)
9
+ @periodical = nil
10
+ @single = { time: time.strftime('%F %T') }
11
+ self
12
+ end
13
+
14
+ def set_periodical(start_time, end_time, time, time_unit, frequency, point)
15
+ @single = nil
16
+ require 'time'
17
+ frequency = 100 if frequency > 100
18
+ @periodical = {
19
+ start: start_time.strftime('%F %T'),
20
+ end: end_time.strftime('%F %T'),
21
+ time: Time.parse(time).strftime('%T'),
22
+ time_unit: time_unit,
23
+ frequency: frequency,
24
+ point: build_point(time_unit, point)
25
+ }
26
+ self
27
+ end
28
+
29
+ def to_hash
30
+ @trigger = {}
31
+ @trigger[:single] = @single unless @single.nil?
32
+ @trigger[:periodical] = @periodical unless @periodical.nil?
33
+ @trigger
34
+ end
35
+
36
+ private
37
+
38
+ def build_point(time_unit, point)
39
+ array = [point].flatten
40
+ point =
41
+ case time_unit.upcase
42
+ when 'DAY'
43
+ nil
44
+ when 'WEEK'
45
+ WEEK & array.map{ |e| e.upcase }
46
+ when 'MONTH'
47
+ MDAY & array
48
+ end
49
+ end
50
+
51
+ end
52
+ end
53
+ end