jpush 4.0.5 → 4.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3d83dfc03c68eb6ee9aa5f6186fa6a852d6d732f
4
- data.tar.gz: 0099414451636b14b1863761575777a2f73d5212
3
+ metadata.gz: ed64db7a7f39cfedce9b5e9749209e434399114e
4
+ data.tar.gz: '0058d231e5b2d8fe1b748d319b0833b55bfeb9c1'
5
5
  SHA512:
6
- metadata.gz: fb2db9530810cea2a30f0a7374674fe7fc0a4530a1db80cbfecc3b1d7ff3d317d400b0857a0529f5e1823650e655b69183b5da52e682ce93d94ede62a5e2d48a
7
- data.tar.gz: da5cd810d4246bd8b833c0a081dd934bcaf6495479b1f3d199dfc7c5ef37a0a245ffd65b6fddf57cc24ff89a5112dcb4061fc82d4ac4da54bad5c95c9e34f14c
6
+ metadata.gz: cc7a28529a4a9f57c3eea2c64af47415f63b62604d173dd4aa7b0a82c4eb19b130a2d0b077d1f74f4d121575d1875be86bdcd9244de709b68405883322022c94
7
+ data.tar.gz: e59538d72132c8180f563eb72e62d18de34867912a3ee0d609f535f6e2272fb79662d576c2c20e5f09bc70b1582bccd66183753fbe8894bd256d6ad808fbe2ba
data/README.md CHANGED
@@ -1,5 +1,3 @@
1
- [![](http://community.jpush.cn/uploads/default/original/1X/a1dbd54304178079e65cdc36810fdf528fdebe24.png)](http://community.jpush.cn/)
2
-
3
1
  # JPush API Ruby Client
4
2
 
5
3
  [![Build Status](https://travis-ci.org/jpush/jpush-api-ruby-client.svg?branch=master)](https://travis-ci.org/jpush/jpush-api-ruby-client)
@@ -4,7 +4,6 @@ module JPush
4
4
  # Your code goes here...
5
5
  require 'jpush/api'
6
6
  require 'jpush/config'
7
- require 'jpush/utils/helper'
8
7
  require 'jpush/utils/http'
9
8
 
10
9
  end
@@ -1,16 +1,12 @@
1
- require 'jpush/helper/argument_helper'
2
1
  require 'jpush/http/client'
3
2
 
4
3
  module JPush
5
4
  module Device
6
5
  extend self
7
- extend Helper::ArgumentHelper
8
- using Utils::Helper::ObjectExtensions
9
6
 
10
7
  # GET /v3/devices/{registration_id}
11
8
  # 获取当前设备的所有属性
12
9
  def show(registration_id)
13
- check_registration_id(registration_id)
14
10
  url = base_url + registration_id
15
11
  Http::Client.get(url)
16
12
  end
@@ -18,25 +14,20 @@ module JPush
18
14
  # POST /v3/devices/{registration_id}
19
15
  # 更新当前设备的指定属性,当前支持tags, alias,手机号码mobile
20
16
  def update(registration_id, tags_add: nil, tags_remove: nil, clear_tags: false, alis: nil, mobile: nil)
21
- check_registration_id(registration_id)
22
- check_mobile(mobile) unless mobile.nil?
23
- check_alias(alis) unless alis.nil?
24
17
  tags =
25
18
  if clear_tags
26
- ''
19
+ ''
27
20
  else
28
- tags_add = build_tags(tags_add) unless tags_add.nil?
29
- tags_remove = build_tags(tags_remove) unless tags_remove.nil?
30
- tags = { add: tags_add, remove: tags_remove }.compact
31
- tags.empty? ? nil : tags
21
+ hash = {}
22
+ hash[:add] = [tags_add].flatten unless tags_add.nil?
23
+ hash[:remove] = [tags_remove].flatten unless tags_remove.nil?
24
+ hash.empty? ? nil : hash
32
25
  end
33
26
  body = {
34
27
  tags: tags,
35
28
  alias: alis,
36
29
  mobile: mobile
37
- }.compact
38
-
39
- raise Utils::Exceptions::JPushError, 'Devices update body can not be empty' if body.empty?
30
+ }.select { |_, value| !value.nil? }
40
31
 
41
32
  url = base_url + registration_id
42
33
  Http::Client.post(url, body: body)
@@ -70,7 +61,7 @@ module JPush
70
61
  # 获取用户在线状态
71
62
  # POST /v3/devices/status/
72
63
  def status(registration_ids)
73
- registration_ids = build_registration_ids(registration_ids)
64
+ registration_ids = [registration_ids].flatten
74
65
  url = base_url + 'status'
75
66
  body = { registration_ids: registration_ids }
76
67
  Http::Client.post(url, body: body)
@@ -87,8 +78,6 @@ module JPush
87
78
 
88
79
  module Tag
89
80
  extend self
90
- extend Helper::ArgumentHelper
91
- using Utils::Helper::ObjectExtensions
92
81
 
93
82
  # GET /v3/tags/
94
83
  # 获取当前应用的所有标签列表。
@@ -100,8 +89,6 @@ module JPush
100
89
  # GET /v3/tags/{tag_value}/registration_ids/{registration_id}
101
90
  # 查询某个设备是否在 tag 下
102
91
  def has_device?(tag_value, registration_id)
103
- check_registration_id(registration_id)
104
- check_tag(tag_value)
105
92
  url = base_url + "#{tag_value}/registration_ids/#{registration_id}"
106
93
  Http::Client.get(url)
107
94
  end
@@ -109,15 +96,11 @@ module JPush
109
96
  # POST /v3/tags/{tag_value}
110
97
  # 为一个标签添加或者删除设备。
111
98
  def update(tag_value, devices_add: nil, devices_remove: nil)
112
- check_tag(tag_value)
113
-
114
- devices_add = build_registration_ids(devices_add) unless devices_add.nil?
115
- devices_remove = build_registration_ids(devices_remove) unless devices_remove.nil?
116
- registration_ids = { add: devices_add, remove: devices_remove }.compact
99
+ rids = {}
100
+ rids[:add] = [devices_add].flatten unless devices_add.nil?
101
+ rids[:remove] = [devices_remove].flatten unless devices_remove.nil?
117
102
 
118
- raise Utils::Exceptions::JPushError, 'Tags update body can not be empty.' if registration_ids.empty?
119
-
120
- body = { registration_ids: registration_ids }
103
+ body = { registration_ids: rids }
121
104
  url = base_url + tag_value
122
105
  Http::Client.post(url, body: body)
123
106
  end
@@ -134,8 +117,7 @@ module JPush
134
117
  # DELETE /v3/tags/{tag_value}
135
118
  # 删除一个标签,以及标签与设备之间的关联关系
136
119
  def delete(tag_value, platform = nil)
137
- check_tag(tag_value)
138
- params = platform.nil? ? nil : { platform: build_platform(platform) }
120
+ params = platform.nil? ? nil : { platform: [platform].flatten.join(',') }
139
121
  url = base_url + tag_value
140
122
  Http::Client.delete(url, params: params)
141
123
  end
@@ -151,12 +133,10 @@ module JPush
151
133
 
152
134
  module Alias
153
135
  extend self
154
- extend Helper::ArgumentHelper
155
136
 
156
137
  # GET /v3/aliases/{alias_value}
157
138
  # 获取指定alias下的设备,最多输出10个
158
139
  def show(alias_value, platform = nil)
159
- check_alias(alias_value)
160
140
  params = platform.nil? ? nil : { platform: build_platform(platform) }
161
141
  url = base_url + alias_value
162
142
  Http::Client.get(url, params: params)
@@ -165,7 +145,6 @@ module JPush
165
145
  # DELETE /v3/aliases/{alias_value}
166
146
  # 删除一个别名,以及该别名与设备的绑定关系
167
147
  def delete(alias_value, platform = nil)
168
- check_alias(alias_value)
169
148
  params = platform.nil? ? nil : { platform: build_platform(platform) }
170
149
  url = base_url + alias_value
171
150
  Http::Client.delete(url, params: params)
@@ -177,6 +156,10 @@ module JPush
177
156
  Config.settings[:device_api_host] + Config.settings[:api_version] + '/aliases/'
178
157
  end
179
158
 
159
+ def build_platform(p)
160
+ [p].flatten.join(',')
161
+ end
162
+
180
163
  end
181
164
 
182
165
  end
@@ -7,15 +7,7 @@ module JPush
7
7
  def initialize(raw_response)
8
8
  @http_code = raw_response.code.to_i
9
9
  @body = parse_body(raw_response.body)
10
-
11
- unless raw_response.kind_of? Net::HTTPSuccess
12
- begin
13
- build_error
14
- rescue Utils::Exceptions::VIPAppKeyError => e
15
- puts "\nVIPAppKeyError: #{e}"
16
- puts "\t#{e.backtrace.join("\n\t")}"
17
- end
18
- end
10
+ build_error unless raw_response.kind_of? Net::HTTPSuccess
19
11
  end
20
12
 
21
13
  private
@@ -33,13 +25,7 @@ module JPush
33
25
  else
34
26
  [@body['code'], @body['message']]
35
27
  end
36
-
37
- case error_code
38
- when 2003
39
- raise Utils::Exceptions::VIPAppKeyError.new(http_code, error_code, error_message)
40
- else
41
- raise Utils::Exceptions::JPushResponseError.new(http_code, error_code, error_message)
42
- end
28
+ raise Utils::Exceptions::JPushResponseError.new(http_code, error_code, error_message)
43
29
  end
44
30
 
45
31
  end
@@ -1,16 +1,14 @@
1
- require 'jpush/helper/argument_helper'
2
1
  require 'jpush/push/push_payload'
3
2
  require 'jpush/http/client'
4
3
 
5
4
  module JPush
6
5
  module Push
7
6
  extend self
8
- extend Helper::ArgumentHelper
9
7
 
10
8
  # POST https://api.jpush.cn/v3/push/validate
11
9
  # 验证推送调用是否能够成功,与推送 API 的区别在于:不向用户发送任何消息
12
10
  def validate(push_payload)
13
- url = base_url + 'validate'
11
+ url = base_url + 'validate'
14
12
  send_push(url, push_payload)
15
13
  end
16
14
 
@@ -1,28 +1,24 @@
1
- require 'jpush/helper/argument_helper'
2
-
3
1
  module JPush
4
2
  module Push
5
3
  class Audience
6
- extend Helper::ArgumentHelper
7
- using Utils::Helper::ObjectExtensions
8
4
 
9
5
  def set_tag(tags)
10
- @tag = Audience.build_tags(tags, 20)
6
+ @tag = [tags].flatten
11
7
  self
12
8
  end
13
9
 
14
10
  def set_tag_and(tags)
15
- @tag_and = Audience.build_tags(tags, 20)
11
+ @tag_and = [tags].flatten
16
12
  self
17
13
  end
18
14
 
19
15
  def set_alias(alis)
20
- @alias = Audience.build_alias(alis)
16
+ @alias = [alis].flatten
21
17
  self
22
18
  end
23
19
 
24
20
  def set_registration_id(registration_ids)
25
- @registration_id = Audience.build_registration_ids(registration_ids)
21
+ @registration_id = [registration_ids].flatten
26
22
  self
27
23
  end
28
24
 
@@ -32,8 +28,7 @@ module JPush
32
28
  tag_and: @tag_and,
33
29
  alias: @alias,
34
30
  registration_id: @registration_id
35
- }.compact
36
- raise Utils::Exceptions::JPushError, 'Audience can not be empty.' if @audience.empty?
31
+ }.select { |_, value| !value.nil? }
37
32
  @audience
38
33
  end
39
34
 
@@ -1,15 +1,8 @@
1
- require 'jpush/helper/argument_helper'
2
-
3
1
  module JPush
4
2
  module Push
5
3
  class Notification
6
- extend Helper::ArgumentHelper
7
- using Utils::Helper::ObjectExtensions
8
-
9
- MAX_IOS_NOTIFICATION_SIZE = 2000
10
4
 
11
5
  def set_alert(alert)
12
- Notification.ensure_argument_not_blank('alert': alert)
13
6
  @alert = alert
14
7
  self
15
8
  end
@@ -21,7 +14,6 @@ module JPush
21
14
 
22
15
  def set_android(alert: , title: nil, builder_id: nil,
23
16
  priority: nil, category: nil, style: nil, big_text: nil, inbox: nil, big_pic_path: nil, extras: nil)
24
- extras = Notification.build_extras(extras)
25
17
  @android = {
26
18
  alert: alert,
27
19
  title: title,
@@ -33,13 +25,12 @@ module JPush
33
25
  inbox: inbox,
34
26
  big_pic_path: big_pic_path,
35
27
  extras: extras
36
- }.compact
28
+ }.select { |_, value| !value.nil? }
37
29
  self
38
30
  end
39
31
 
40
32
  def set_ios(alert: , sound: nil, badge: '+1', available: nil, category:nil, extras: nil, contentavailable: nil, mutablecontent: nil)
41
33
  contentavailable = available if contentavailable.nil?
42
- extras = Notification.build_extras(extras)
43
34
  contentavailable = nil unless contentavailable.is_a? TrueClass
44
35
  mutablecontent = nil unless mutablecontent.is_a? TrueClass
45
36
  @ios = {
@@ -50,8 +41,7 @@ module JPush
50
41
  'mutable-content': mutablecontent,
51
42
  category: category,
52
43
  extras: extras
53
- }.compact
54
- Notification.ensure_not_over_bytesize('ios', {'ios': @ios}, MAX_IOS_NOTIFICATION_SIZE)
44
+ }.select { |_, value| !value.nil? }
55
45
  self
56
46
  end
57
47
 
@@ -60,7 +50,7 @@ module JPush
60
50
  alert: @alert,
61
51
  android: @android,
62
52
  ios: @ios
63
- }.compact
53
+ }.select { |_, value| !value.nil? }
64
54
  raise Utils::Exceptions::JPushError, 'Notification can not be empty.' if @notification.empty?
65
55
  @notification
66
56
  end
@@ -1,14 +1,10 @@
1
- require 'jpush/helper/argument_helper'
2
1
  require 'jpush/push/audience'
3
2
  require 'jpush/push/notification'
4
3
 
5
4
  module JPush
6
5
  module Push
7
6
  class PushPayload
8
- extend Helper::ArgumentHelper
9
- using Utils::Helper::ObjectExtensions
10
7
 
11
- VALID_OPTION_KEY = [:sendno, :time_to_live, :override_msg_id, :apns_production, :big_push_duration]
12
8
  MAX_SMS_CONTENT_SIZE = 480
13
9
  MAX_SMS_DELAY_TIME = 86400 # 24 * 60 * 60 (second)
14
10
 
@@ -30,14 +26,11 @@ module JPush
30
26
  end
31
27
 
32
28
  def set_options(opts)
33
- @options = build_options(opts)
29
+ @options = opts
34
30
  self
35
31
  end
36
32
 
37
33
  def to_hash
38
- raise Utils::Exceptions::MissingArgumentError.new(['audience']) unless @audience
39
- err_msg = 'missing notification or message'
40
- raise Utils::Exceptions::JPushError, err_msg unless @notification || @message
41
34
  @push_payload = {
42
35
  platform: @platform,
43
36
  audience: @audience,
@@ -45,13 +38,14 @@ module JPush
45
38
  message: @message,
46
39
  sms_message: @sms_message,
47
40
  options: { apns_production: false }.merge(@options.nil? ? {} : @options)
48
- }.compact
41
+ }.select { |_, value| !value.nil? }
49
42
  end
50
43
 
51
44
  private
52
45
 
53
46
  def build_platform(platform)
54
- PushPayload.build_platform(platform)
47
+ return platform if platform.is_a? Array
48
+ return [platform]
55
49
  end
56
50
 
57
51
  def build_audience(audience)
@@ -64,30 +58,20 @@ module JPush
64
58
  end
65
59
 
66
60
  def build_message(msg_content, title = nil, content_type = nil, extras = nil)
67
- hash = {'msg_content': msg_content, 'title': title, 'content_type': content_type}.select{|key, value| !value.nil?}
68
- PushPayload.ensure_argument_not_blank(hash)
69
- extras = PushPayload.build_extras(extras)
61
+ extras = (extras.nil? || !extras.is_a?(Hash) || extras.empty?) ? nil : extras
70
62
  message = {
71
63
  msg_content: msg_content,
72
64
  title: title,
73
65
  content_type: content_type,
74
66
  extras: extras
75
- }.compact
67
+ }.select { |_, value| !value.nil? }
76
68
  end
77
69
 
78
70
  def build_sms_message(content, delay_time)
79
- PushPayload.ensure_argument_not_blank('content': content)
80
- PushPayload.ensure_not_over_size('content', content, MAX_SMS_CONTENT_SIZE)
81
71
  delay_time = 0 if delay_time > MAX_SMS_DELAY_TIME
82
72
  {content: content, delay_time: delay_time}
83
73
  end
84
74
 
85
- def build_options(opts)
86
- opts.each_key do |key|
87
- raise Utils::Exceptions::InvalidElementError.new('options', key.to_sym, VALID_OPTION_KEY) unless VALID_OPTION_KEY.include?(key.to_sym)
88
- end
89
- end
90
-
91
75
  end
92
76
  end
93
77
  end
@@ -1,10 +1,8 @@
1
- require 'jpush/helper/argument_helper'
2
1
  require 'jpush/http/client'
3
2
 
4
3
  module JPush
5
4
  module Report
6
5
  extend self
7
- extend Helper::ArgumentHelper
8
6
 
9
7
  TIME_UNIT = ['HOUR', 'DAY', 'MONTH']
10
8
  TIME_FORMAT = { hour: '%F %H', day: '%F', month: '%Y-%m' }
@@ -13,7 +11,7 @@ module JPush
13
11
  # GET /v3/received
14
12
  # 送达统计
15
13
  def received(msg_ids)
16
- msg_ids = build_msg_ids(msg_ids)
14
+ msg_ids = [msg_ids].flatten
17
15
  url = base_url + '/received'
18
16
  params = {
19
17
  msg_ids: msg_ids.join(',')
@@ -24,7 +22,7 @@ module JPush
24
22
  # GET /v3/messages
25
23
  # 消息统计
26
24
  def messages(msg_ids)
27
- msg_ids = build_msg_ids(msg_ids)
25
+ msg_ids = [msg_ids].flatten
28
26
  url = base_url + '/messages'
29
27
  params = {
30
28
  msg_ids: msg_ids.join(',')
@@ -35,7 +33,6 @@ module JPush
35
33
  # GET /v3/users
36
34
  # 用户统计
37
35
  def users(time_unit, start, duration)
38
- raise Utils::Exceptions::InvalidElementError.new('time unit', time_unit, TIME_UNIT) unless TIME_UNIT.include?(time_unit.upcase)
39
36
  start = start.strftime(TIME_FORMAT[time_unit.downcase.to_sym])
40
37
  duration = build_duration(time_unit.downcase.to_sym, duration)
41
38
  params = {
@@ -1,4 +1,3 @@
1
- require 'jpush/helper/argument_helper'
2
1
  require 'jpush/schedule/schedule_payload'
3
2
  require 'jpush/http/client'
4
3
 
@@ -6,7 +5,6 @@ module JPush
6
5
 
7
6
  module Schedule
8
7
  extend self
9
- extend Helper::ArgumentHelper
10
8
 
11
9
  # POST https://api.jpush.cn/v3/schedules
12
10
  # 创建一个新的定时任务
@@ -1,15 +1,12 @@
1
- require 'jpush/helper/argument_helper'
2
1
  require 'jpush/push/push_payload'
3
2
  require 'jpush/schedule/trigger'
4
3
 
5
4
  module JPush
6
5
  module Schedule
7
6
  class SchedulePayload
8
- extend Helper::ArgumentHelper
9
- using Utils::Helper::ObjectExtensions
10
7
 
11
8
  def initialize(name, trigger, push_payload, enabled = nil)
12
- @name = build_name(name)
9
+ @name = name
13
10
  @trigger = build_trigger(trigger)
14
11
  @push_payload = build_push_payload(push_payload)
15
12
  @enabled = enabled
@@ -21,7 +18,7 @@ module JPush
21
18
  enabled: @enabled,
22
19
  trigger: @trigger,
23
20
  push: @push_payload
24
- }.compact
21
+ }.select { |_, value| !value.nil? }
25
22
  raise Utils::Exceptions::JPushError, 'Schedule update body can not be empty' if @schedule_payload.empty?
26
23
  @schedule_payload
27
24
  end
@@ -34,15 +31,9 @@ module JPush
34
31
  push: @push_payload
35
32
  }
36
33
  hash = @schedule_payload.select { |_, value| value.nil? }
37
- raise Utils::Exceptions::MissingArgumentError.new(hash.keys) unless hash.empty?
38
34
  @schedule_payload
39
35
  end
40
36
 
41
- def build_name(name)
42
- SchedulePayload.ensure_word_valid('name', name) unless name.nil?
43
- name
44
- end
45
-
46
37
  def build_trigger(trigger)
47
38
  return { single: { time: trigger.strftime('%F %T') } } if trigger.is_a? Time
48
39
  trigger.is_a?(Trigger) ? trigger.to_hash : nil
@@ -1,12 +1,7 @@
1
- require 'jpush/helper/argument_helper'
2
-
3
1
  module JPush
4
2
  module Schedule
5
3
  class Trigger
6
- extend Helper::ArgumentHelper
7
- using Utils::Helper::ObjectExtensions
8
4
 
9
- TIME_UNIT = ['MONTH', 'WEEK', 'DAY']
10
5
  WEEK = ['MON','TUE','WED','THU','FRI','SAT','SUN']
11
6
  MDAY = ('01'..'31').to_a
12
7
 
@@ -18,7 +13,6 @@ module JPush
18
13
 
19
14
  def set_periodical(start_time, end_time, time, time_unit, frequency, point)
20
15
  @single = nil
21
- raise Utils::Exceptions::InvalidElementError.new('time unit', time_unit, TIME_UNIT) unless TIME_UNIT.include?(time_unit.upcase)
22
16
  require 'time'
23
17
  frequency = 100 if frequency > 100
24
18
  @periodical = {
@@ -33,11 +27,9 @@ module JPush
33
27
  end
34
28
 
35
29
  def to_hash
36
- @trigger = {
37
- single: @single,
38
- periodical: @periodical
39
- }.compact
40
- raise Utils::Exceptions::JPushError, 'Trigger can not be empty.' if @trigger.empty?
30
+ @trigger = {}
31
+ @trigger[:single] = @single unless @single.nil?
32
+ @trigger[:periodical] = @periodical unless @periodical.nil?
41
33
  @trigger
42
34
  end
43
35
 
@@ -54,7 +46,6 @@ module JPush
54
46
  when 'MONTH'
55
47
  MDAY & array
56
48
  end
57
- raise Utils::Exceptions::InvalidArgumentError.new([], "invalid point") if point.empty?
58
49
  end
59
50
 
60
51
  end
@@ -1,9 +1,6 @@
1
- require 'jpush/utils/helper'
2
-
3
1
  module JPush
4
2
  module Utils
5
3
  module Exceptions
6
- using Utils::Helper::ObjectExtensions
7
4
 
8
5
  class JPushError < StandardError
9
6
  attr_reader :message
@@ -12,50 +9,12 @@ module JPush
12
9
  end
13
10
  end
14
11
 
15
- class JPushArgumentError < JPushError
16
- end
17
-
18
- class MissingArgumentError < JPushArgumentError
19
- def initialize(missed_args)
20
- list = missed_args.map {|arg| arg.to_s} * (', ')
21
- msg = "#{list} are required."
22
- super(msg)
23
- end
24
- end
25
-
26
- class InvalidArgumentError < JPushArgumentError
27
- def initialize(invalid_args, msg = nil)
28
- list = invalid_args.map {|arg| arg.to_s} * (', ')
29
- msg ||= "#{list} can not be blank."
30
- super(msg)
31
- end
32
- end
33
-
34
- class InvalidWordError < JPushError
35
- def initialize(name, word)
36
- super("invalid #{name}: #{word} ( #{name} can only contain letters, numbers,
37
- '_', Chinese character and special characters(@!#$&*+=.|))")
38
- end
39
- end
40
-
41
- class InvalidElementError < JPushError
42
- def initialize(name, invalid_element, list)
43
- super("invalid #{name}: #{invalid_element} ( #{name} only support #{list * (', ')} )")
44
- end
45
- end
46
-
47
- class OverLimitError < JPushError
48
- def initialize(name, limit, unit)
49
- super("#{name} must have at most #{limit} #{unit}")
50
- end
51
- end
52
-
53
12
  class JPushResponseError < JPushError
54
13
  attr_reader :http_code, :error_code, :error_message
55
14
 
56
15
  def initialize(http_code, error_code, error_message)
57
16
  @http_code, @error_code, @error_message = http_code, error_code, error_message
58
- @error_message = "UnknownError[#{@http_code}]." if @error_message.blank?
17
+ @error_message = "UnknownError[#{@http_code}]." if @error_message.nil?
59
18
  super("#{@error_message} (error code: #{@error_code}) ")
60
19
  end
61
20
 
@@ -64,12 +23,6 @@ module JPush
64
23
  end
65
24
  end
66
25
 
67
- class VIPAppKeyError < JPushResponseError
68
- def initialize(http_code, error_code, error_message)
69
- super(http_code, error_code, error_message)
70
- end
71
- end
72
-
73
26
  class TimeOutError < JPushError
74
27
  def initialize(error)
75
28
  super("#{error.class} was raised, please rescue it")
@@ -1,5 +1,6 @@
1
1
  require 'net/http'
2
2
  require 'json'
3
+ require 'jpush/utils/exceptions'
3
4
 
4
5
  module JPush
5
6
  module Utils
@@ -1,3 +1,3 @@
1
1
  module JPush
2
- VERSION = "4.0.5"
2
+ VERSION = "4.0.6"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jpush
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.0.5
4
+ version: 4.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - JPush Offical
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-12-27 00:00:00.000000000 Z
11
+ date: 2017-01-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -74,8 +74,6 @@ files:
74
74
  - lib/jpush/api.rb
75
75
  - lib/jpush/config.rb
76
76
  - lib/jpush/device.rb
77
- - lib/jpush/helper/argument.rb
78
- - lib/jpush/helper/argument_helper.rb
79
77
  - lib/jpush/http/client.rb
80
78
  - lib/jpush/http/response.rb
81
79
  - lib/jpush/push.rb
@@ -87,7 +85,6 @@ files:
87
85
  - lib/jpush/schedule/schedule_payload.rb
88
86
  - lib/jpush/schedule/trigger.rb
89
87
  - lib/jpush/utils/exceptions.rb
90
- - lib/jpush/utils/helper.rb
91
88
  - lib/jpush/utils/http.rb
92
89
  - lib/jpush/version.rb
93
90
  homepage: https://github.com/jpush/jpush-api-ruby-client
@@ -1,78 +0,0 @@
1
- require 'jpush/utils/helper'
2
- require 'jpush/utils/exceptions'
3
-
4
- module JPush
5
- module Helper
6
- module Argument
7
- using Utils::Helper::ObjectExtensions
8
-
9
- MOBILE_RE = /^(1[34578])(\d{9})$/
10
-
11
- MAX_ALIAS_BYTESIZE = 40
12
- MAX_TAG_BYTESIZE = 40
13
-
14
- def check_registration_id(registration_id)
15
- ensure_argument_not_blank('registration id': registration_id)
16
- end
17
-
18
- def check_mobile(mobile)
19
- raise Utils::Exceptions::InvalidArgumentError.new([], "invalid mobile") unless MOBILE_RE === mobile
20
- end
21
-
22
- def check_alias(alis)
23
- return '' == alis
24
- ensure_word_valid('alias', alis)
25
- ensure_not_over_bytesize('alias', alis, MAX_ALIAS_BYTESIZE)
26
- end
27
-
28
- def check_tag(tag)
29
- ensure_word_valid('tag', tag)
30
- ensure_not_over_bytesize('tag', tag, MAX_TAG_BYTESIZE)
31
- end
32
-
33
- def check_platform(platform)
34
- valid_platform = JPush::Config.settings[:valid_platform]
35
- raise Utils::Exceptions::InvalidElementError.new('platform', platform, valid_platform) unless valid_platform.include?(platform)
36
- end
37
-
38
- def ensure_argument_not_blank(args)
39
- invalid_args = args.select{|key, value| value.blank?}
40
- raise Utils::Exceptions::InvalidArgumentError.new(invalid_args.keys) unless invalid_args.empty?
41
- end
42
-
43
- def ensure_argument_required(args)
44
- missing_args = args.select{|key, value| value.nil?}
45
- raise Utils::Exceptions::MissingArgumentError.new(missing_args.keys) unless missing_args.empty?
46
- end
47
-
48
- def ensure_not_over_bytesize(name, value, max_bytesize)
49
- bytesize =
50
- if value.respond_to? :bytesize
51
- value.bytesize
52
- else
53
- size = value.to_json.bytesize if value.is_a? Hash
54
- size = value.inject(0){|r, e| r += e.bytesize} if value.is_a? Array
55
- size
56
- end
57
- raise Utils::Exceptions::OverLimitError.new(name, max_bytesize, 'bytes') if bytesize > max_bytesize
58
- end
59
-
60
- def ensure_not_over_size(name, value, max_size)
61
- size = value.size
62
- unit =
63
- if value.is_a? String
64
- 'characters'
65
- else
66
- 'items'
67
- end
68
- raise Utils::Exceptions::OverLimitError.new(name, max_size, unit) if size > max_size
69
- end
70
-
71
- # 有效的 tag alias 组成: 字母(区分大小写), 数字, 下划线, 汉字, 特殊字符(@!#$&*+=.|)
72
- def ensure_word_valid(name, word)
73
- raise Utils::Exceptions::InvalidWordError.new(name, word) unless word.valid_word?
74
- end
75
-
76
- end
77
- end
78
- end
@@ -1,59 +0,0 @@
1
- require 'jpush/helper/argument'
2
- require 'jpush/utils/helper'
3
-
4
- module JPush
5
- module Helper
6
- module ArgumentHelper
7
- extend Argument
8
- using Utils::Helper::ObjectExtensions
9
-
10
- MAX_TAG_ARRAY_SZIE = 100
11
- MAX_ALIAS_ARRAY_SIZE = 1000
12
- MAX_REGISTRATION_ID_ARRAY_SIZE = 1000
13
- MAX_TAG_ARRAY_MAX_BYTESIZE = 1024
14
- MAX_MSG_IDS_ARRAY_SIZE = 100
15
-
16
- def self.extended(base)
17
- base.extend Argument
18
- end
19
-
20
- def build_tags(tags, max_size = MAX_TAG_ARRAY_SZIE)
21
- tags = build_args_from_array_or_string('tags', tags, max_size)
22
- ensure_not_over_bytesize('tags', tags, MAX_TAG_ARRAY_MAX_BYTESIZE)
23
- tags
24
- end
25
-
26
- def build_registration_ids(ids)
27
- build_args_from_array_or_string('registration ids', ids, MAX_REGISTRATION_ID_ARRAY_SIZE)
28
- end
29
-
30
- def build_alias(alis)
31
- build_args_from_array_or_string('alias', alis, MAX_ALIAS_ARRAY_SIZE)
32
- end
33
-
34
- def build_msg_ids(msg_ids)
35
- build_args_from_array_or_string('msg ids', msg_ids, MAX_MSG_IDS_ARRAY_SIZE)
36
- end
37
-
38
- def build_extras(extras)
39
- (extras.nil? || !extras.is_a?(Hash) || extras.empty?) ? nil : extras
40
- end
41
-
42
- def build_platform(platform)
43
- [platform].flatten.each{ |pf| check_platform(pf) }
44
- end
45
-
46
-
47
- private
48
-
49
- def build_args_from_array_or_string(args_name, args_value, max_size)
50
- # remove blank elements in args array
51
- args = [args_value].flatten.compact.reject{ |arg| arg.blank? }.uniq
52
- ensure_argument_not_blank(args_name.to_sym => args)
53
- ensure_not_over_size(args_name, args, max_size)
54
- args.each{|word| ensure_word_valid(args_name, word)}
55
- end
56
-
57
- end
58
- end
59
- end
@@ -1,48 +0,0 @@
1
- module JPush
2
- module Utils
3
- module Helper
4
- module ObjectExtensions
5
-
6
- refine Object do
7
-
8
- def jpush_symbolize_keys!
9
- return self.inject({}){ |memo, (k, v)| memo[k.to_sym] = v.jpush_symbolize_keys!; memo } if self.is_a? Hash
10
- return self.inject([]){ |memo, v | memo << v.jpush_symbolize_keys!; memo } if self.is_a? Array
11
- return self
12
- end
13
-
14
- # An object is blank if it's false, empty, or a whitespace string.
15
- # For example, false, '', ' ', nil, [], and {} are all blank.
16
- def blank?
17
- respond_to?(:empty?) ? !!empty? : !self
18
- end
19
-
20
- end
21
-
22
- refine String do
23
- BLANK_RE = /\A[[:space:]]*\z/
24
- WORD_RE = /\A[\p{han}a-zA-Z0-9_@!#&=|\$\*\+\.]+\z/u
25
-
26
- def titleize
27
- self.split(' ').map(&:capitalize).join(' ')
28
- end
29
-
30
- def blank?
31
- BLANK_RE === self
32
- end
33
-
34
- def valid_word?
35
- WORD_RE === self
36
- end
37
- end
38
-
39
- refine Hash do
40
- def compact
41
- self.select { |_, value| !value.nil? }
42
- end
43
- end
44
-
45
- end
46
- end
47
- end
48
- end