jpush 3.1.1 → 3.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (40) hide show
  1. checksums.yaml +4 -4
  2. data/lib/jpush.rb +10 -0
  3. data/lib/jpush/api_connection_exception.rb +11 -0
  4. data/lib/jpush/device_client.rb +84 -0
  5. data/lib/jpush/http_client.rb +32 -18
  6. data/lib/jpush/jpush_client.rb +81 -7
  7. data/lib/jpush/model/alias_uids_result.rb +32 -0
  8. data/lib/jpush/model/exist_result.rb +33 -0
  9. data/lib/jpush/model/get_messages_result.rb +30 -0
  10. data/lib/jpush/model/notification/ios_notification.rb +6 -2
  11. data/lib/jpush/model/options.rb +15 -4
  12. data/lib/jpush/model/push_payload.rb +6 -0
  13. data/lib/jpush/model/push_result.rb +4 -4
  14. data/lib/jpush/model/receiveds_result.rb +3 -3
  15. data/lib/jpush/model/tag_alias.rb +62 -0
  16. data/lib/jpush/model/tag_alias_result.rb +32 -0
  17. data/lib/jpush/model/tag_list_result.rb +32 -0
  18. data/lib/jpush/model/tag_manager.rb +44 -0
  19. data/lib/jpush/model/user_result.rb +40 -0
  20. data/lib/jpush/push_client.rb +18 -4
  21. data/lib/jpush/report_client.rb +11 -6
  22. data/lib/jpush/response_wrapper.rb +2 -1
  23. data/lib/jpush/util/service_helper.rb +3 -2
  24. metadata +13 -26
  25. data/.gitignore +0 -18
  26. data/.project +0 -18
  27. data/Gemfile +0 -4
  28. data/LICENSE.txt +0 -339
  29. data/README.md +0 -75
  30. data/Rakefile +0 -1
  31. data/example/push_example.rb +0 -101
  32. data/example/report_example.rb +0 -18
  33. data/jpush.gemspec +0 -24
  34. data/test/alert_override_tests.rb +0 -62
  35. data/test/audience_tests.rb +0 -169
  36. data/test/base_remote_tests.rb +0 -27
  37. data/test/message_tests.rb +0 -49
  38. data/test/notification_tests.rb +0 -52
  39. data/test/push_payload_test.rb +0 -58
  40. data/test/report_function_tests.rb +0 -40
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: cfdeeb5127c6c5ea4368402101d00b34880fd8ad
4
- data.tar.gz: d1c859beb378456dd060afa9c5606980311494a2
3
+ metadata.gz: afc8cbc7711185ef95c4a147eee1be80a71cc18c
4
+ data.tar.gz: 7c16e63e9a0b54c83ea290a38a36f2205d9903e4
5
5
  SHA512:
6
- metadata.gz: 4af6fe459fc2f34c646d3b15950111279101626e49e23db88050b4ce5bf1371372aac62294b52d0f3ea8bec61fbe2211e23808f978727870fbad007f7512c3b0
7
- data.tar.gz: d8bcdb83e0a48a3fc5ea1b93e703926100e74a3fd7619dac204958713600e1ab383d327890f52df430dec06f4c8945d18b101d83f817bdc4fc620fd10153a8d4
6
+ metadata.gz: ef8fdb050300bb47b07d32d1de23497f2335e3d2aa21d4817cdb6207bdb0864acc4e2fdc08b52a8c50387c098fa2b518aca29393636691cfae52673892850997
7
+ data.tar.gz: 44f804cf52523e6cdc09194923074e51058126212e8058b8f7f80f7b4a98575fe41d229a193a37f9c269135f29b4be36dbaf2862aa05becc2250de1942cc946e
@@ -17,4 +17,14 @@ module JPush
17
17
  autoload :Notification,'jpush/model/notification/notification'
18
18
  autoload :PushResult, 'jpush/model/push_result'
19
19
  autoload :ReceivedsResult, 'jpush/model/receiveds_result'
20
+ autoload :DeviceClient, 'jpush/device_client'
21
+ autoload :TagManager, 'jpush/model/tag_manager'
22
+ autoload :TagAlias, 'jpush/model/tag_alias'
23
+ autoload :TagAliasResult, 'jpush/model/tag_alias_result'
24
+ autoload :ExistResult, 'jpush/model/exist_result'
25
+ autoload :TagListResult, 'jpush/model/tag_list_result'
26
+ autoload :AliasUidsResult, 'jpush/model/alias_uids_result'
27
+ autoload :ApiConnectionException, 'jpush/api_connection_exception'
28
+ autoload :GetMessagesResult, 'jpush/model/get_messages_result'
29
+ autoload :UserResult, 'jpush/model/user_result'
20
30
  end
@@ -0,0 +1,11 @@
1
+ path = File.expand_path('../', __FILE__)
2
+ require File.join(path, 'response_wrapper.rb')
3
+
4
+ module JPush
5
+ class ApiConnectionException < Exception
6
+ attr_accessor :res_wrapper
7
+ def initialize(wrapper)
8
+ @res_wrapper = wrapper
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,84 @@
1
+ path= File.expand_path('../', __FILE__)
2
+ require File.join(path, 'http_client.rb')
3
+ require 'json'
4
+
5
+ =begin
6
+ Entrance for Device api.
7
+ =end
8
+ module JPush
9
+ class DeviceClient
10
+ @@DEVICE_HOST_NAME = 'https://device.jpush.cn'
11
+ @@DEVICE_PATH = '/v3/devices/'
12
+ @@TAG_PATH_LIST = '/v3/tags/'
13
+
14
+ def initialize(maxRetryTimes)
15
+ @httpclient = JPush::NativeHttpClient.new(maxRetryTimes)
16
+ end
17
+
18
+ def getDeviceTagAlias(registration_id)
19
+ result = JPush::TagAliasResult.new
20
+ url = @@DEVICE_HOST_NAME + @@DEVICE_PATH + registration_id
21
+ puts url
22
+ wrapper = @httpclient.sendGet(url, nil)
23
+ result.fromResponse(wrapper)
24
+ return result
25
+ end
26
+
27
+ def updateDeviceTagAlias(registration_id, tagAlias)
28
+ json_data = JSON.generate(tagAlias.toJSON)
29
+ url = @@DEVICE_HOST_NAME + @@DEVICE_PATH + registration_id
30
+ return @httpclient.sendPost(url, json_data)
31
+ end
32
+
33
+ def getAppkeyTagList()
34
+ url = @@DEVICE_HOST_NAME + @@TAG_PATH_LIST
35
+ tag_list = JPush::TagListResult.new
36
+ wrapper = @httpclient.sendGet(url, nil)
37
+ tag_list.fromResponse(wrapper)
38
+ return tag_list
39
+ end
40
+
41
+ def userExistsInTag(tag_value, registration_id)
42
+ result = JPush::ExistResult.new
43
+ url = @@DEVICE_HOST_NAME + '/v3/tags/' + tag_value + '/registration_ids/' + registration_id
44
+ wrapper = @httpclient.sendGet(url, nil)
45
+ result.fromResponse(wrapper)
46
+ return result
47
+ end
48
+
49
+ def tagAddingOrRemovingUsers(tag_value, tagManager)
50
+ json_data = JSON.generate(tagManager.toJSON)
51
+ url = @@DEVICE_HOST_NAME + '/v3/tags/' + tag_value
52
+ return @httpclient.sendPost(url, json_data)
53
+ end
54
+
55
+ def tagDelete(tag_value, platform)
56
+
57
+ url = @@DEVICE_HOST_NAME + '/v3/tags/' + tag_value
58
+ if platform != nil
59
+ url = url + '?platform=' + platform
60
+ end
61
+ return @httpclient.sendDelete(url, nil)
62
+ end
63
+
64
+ def getAliasUids(alias_value, platform)
65
+ aliasUidsResult = JPush::AliasUidsResult.new
66
+ url = @@DEVICE_HOST_NAME + '/v3/aliases/' + alias_value
67
+ if platform != nil
68
+ url = url + '?platform=' + platform
69
+ end
70
+ wrapper = @httpclient.sendGet(url, nil)
71
+ aliasUidsResult.fromResponse(wrapper)
72
+ return aliasUidsResult
73
+ end
74
+
75
+ def aliasDelete(alias_value, platform)
76
+ url = @@DEVICE_HOST_NAME + '/v3/aliases/' + alias_value
77
+ if platform != nil
78
+ url = url + '?platform=' + platform
79
+ end
80
+ return @httpclient.sendDelete(url, nil)
81
+ end
82
+
83
+ end
84
+ end
@@ -8,32 +8,39 @@ require 'net/https'
8
8
 
9
9
  module JPush
10
10
  class NativeHttpClient
11
- def initialize(maxRetryTimes = 5)
11
+ def initialize(maxRetryTimes = 5, opts = {})
12
12
  @maxRetryTimes = maxRetryTimes
13
13
  @logger = Logger.new(STDOUT)
14
+ @@proxy_addr = opts[:proxy_addr]
15
+ @@proxy_port = opts[:proxy_port]
14
16
  end
15
17
 
16
- def sendPost(url, content, authCode)
17
- return sendRequest(url, content, 'POST', authCode)
18
+ def sendPost(url, content)
19
+ return sendRequest(url, content, 'POST')
18
20
  end
19
21
 
20
- def sendGet(url, content, authCode)
21
- return sendRequest(url, content, 'GET', authCode)
22
+ def sendGet(url, content)
23
+ return sendRequest(url, content, 'GET')
22
24
  end
25
+
26
+ def sendDelete(url, content)
27
+ return sendRequest(url, content, 'DELETE')
28
+ end
29
+
23
30
  private
24
31
 
25
- def sendRequest(url, content, method, authCode)
26
- wrapper = _sendRequest(url, content, method, authCode)
32
+ def sendRequest(url, content, method)
27
33
  retryTimes = 0
28
34
  while retryTimes < @maxRetryTimes
29
35
  begin
30
- response = _sendRequest(url, content, method, authCode)
36
+ wrapper = _sendRequest(url, content, method)
31
37
  break
32
- rescue
38
+ rescue Exception=> ex
39
+ puts ex.class
33
40
  if retryTimes > @maxRetryTimes
34
41
  raise RuntimeError.new('connect error')
35
42
  else
36
- @logger.debug('Retry again - ' + (retryTimes + 1))
43
+ @logger.debug('Retry again - ' + (retryTimes + 1).to_s)
37
44
  retryTimes = retryTimes + 1
38
45
  end
39
46
  end
@@ -42,7 +49,7 @@ module JPush
42
49
  end
43
50
  private
44
51
 
45
- def _sendRequest(url, content, method, authCode)
52
+ def _sendRequest(url, content, method)
46
53
  begin
47
54
 
48
55
  header = {}
@@ -50,22 +57,26 @@ module JPush
50
57
  header['Connection'] = 'Keep-Alive'
51
58
  header['Charset'] = 'UTF-8'
52
59
  header['Content-Type'] = 'application/json'
53
- header['Authorization'] = authCode
60
+ header['Authorization'] = $authcode
54
61
  #url = url+content
62
+ #url.gsub!(/[^\w$&\-+.,\/:;=?@]/) { |x| x = format("%%%x", x[0]) }
63
+ url = URI.escape(url)
55
64
  uri = URI.parse(url)
56
- http = Net::HTTP.new(uri.host, uri.port)
65
+ http = Net::HTTP.new(uri.host, uri.port, @@proxy_addr, @@proxy_port)
57
66
  http.use_ssl = true
58
- #http.verify_mode = OpenSSL::SSL::VERIFY_NONE
59
- http.open_timeout = 5
67
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
68
+ http.open_timeout = 30
60
69
  http.read_timeout = 30
61
70
  use_ssl = true
62
71
  if method == 'POST' && use_ssl == true
63
72
  req = Net::HTTP::Post.new(uri.path, initheader = header)
64
- req.body = content
65
- response = http.request(req)
73
+ req.body = content
74
+ response = http.request(req)
66
75
  elsif method == 'GET' && use_ssl == true
67
76
  request = Net::HTTP::Get.new(uri.request_uri, initheader = header)
68
77
  response = http.request(request)
78
+ elsif method == 'DELETE' && use_ssl == true
79
+ response = http.delete(uri.path, initheader = header)
69
80
  end
70
81
  #if method == 'POST'
71
82
  # @response = http.post(path,content,header)
@@ -78,11 +89,14 @@ module JPush
78
89
  wrapper.code = code
79
90
  wrapper.setResponseContent(response.body)
80
91
  headers = response.header.to_hash
92
+ if response['X-Rate-Limit-Limit'] == nil
93
+ else
81
94
  quota = response['X-Rate-Limit-Limit']
82
95
  remaining = response['X-Rate-Limit-Remaining']
83
96
  reset = response['X-Rate-Limit-Reset']
84
-
85
97
  wrapper.setRateLimit(Integer(quota), Integer(remaining), Integer(reset))
98
+ end
99
+
86
100
  if code == 200
87
101
  @logger.debug('Succeed to get response - 200 OK')
88
102
  if content != nil
@@ -14,7 +14,6 @@ Create a JPush Client.
14
14
  masterSecret API access secret of the appKey.
15
15
  appKey The KEY of one application on JPush.
16
16
  =end
17
-
18
17
  def initialize(appkey, masterSecret, maxRetryTimes = 5)
19
18
  begin
20
19
  @logger = Logger.new(STDOUT)
@@ -33,22 +32,28 @@ appKey The KEY of one application on JPush.
33
32
  @masterSecret = masterSecret
34
33
  @pushClient = JPush::PushClient.new(maxRetryTimes = maxRetryTimes)
35
34
  @reportClient = JPush::ReportClient.new(maxRetryTimes = maxRetryTimes)
36
- @authcode = ServiceHelper.getAuthorizationBase64(appkey, masterSecret)
35
+ @deviceClient = JPush::DeviceClient.new(maxRetryTimes = maxRetryTimes)
36
+ $authcode = ServiceHelper.getAuthorizationBase64(appkey, masterSecret)
37
37
  end
38
38
 
39
39
  # Send a push with object.
40
40
  # @param pushPayload payload object of a push.
41
41
  # @return JSON data.
42
42
  def sendPush(payload)
43
- result = @pushClient.sendPush(payload, @authcode)
43
+ result = @pushClient.sendPush(payload)
44
44
  return result
45
45
  end
46
46
 
47
+ def validate(payload)
48
+ result = @pushClient.validate(payload)
49
+ return result
50
+ end
51
+
47
52
  #Get received report.
48
53
  # @param msgIds 100 msgids to batch getting is supported.
49
54
  # @return JSON data.
50
55
  def getReportReceiveds(msgIds)
51
- result = @reportClient.getReceiveds(msgIds, @authcode)
56
+ result = @reportClient.getReceiveds(msgIds)
52
57
  return result
53
58
  end
54
59
 
@@ -56,7 +61,7 @@ appKey The KEY of one application on JPush.
56
61
  # @param msgIds 100 msgids to batch getting is supported.
57
62
  # @return JSON data.
58
63
  def getReportMessages(msgIds)
59
- result = @reportClient.getMessages(msgIds, @authcode)
64
+ result = @reportClient.getMessages(msgIds)
60
65
  return result
61
66
  end
62
67
 
@@ -65,11 +70,80 @@ appKey The KEY of one application on JPush.
65
70
  #@param start is a string for example 2014-06-10
66
71
  #@duration
67
72
  # @return JSON data.
68
-
69
73
  def getReportUsers(timeUnit, start, duration)
70
- result = @reportClient.getUsers(timeUnit, start, duration, @authcode)
74
+ result = @reportClient.getUsers(timeUnit, start, duration)
71
75
  return result
72
76
  end
73
77
 
78
+ # Get user profile
79
+ #@param registration_id
80
+ #Response Data
81
+ #{
82
+ # "tags": ["tag1", "tag2"],
83
+ # "alias": "alias1" }
84
+ def getDeviceTagAlias(registration_id)
85
+ return @deviceClient.getDeviceTagAlias(registration_id)
86
+ end
87
+
88
+ =begin
89
+ Update user device profile
90
+ @param registration_id
91
+ @param tagAlias
92
+ =end
93
+ def updateDeviceTagAlias(registration_id, tagAlias)
94
+ return @deviceClient.updateDeviceTagAlias(registration_id, tagAlias);
95
+ end
96
+
97
+ =begin
98
+ Appkey Tag List
99
+ =end
100
+ def getAppkeyTagList()
101
+ return @deviceClient.getAppkeyTagList
102
+ end
103
+
104
+ =begin
105
+ User Exists In Tag
106
+ @param tag_value
107
+ @param registration_id
108
+ =end
109
+ def userExistsInTag(tag_value, registration_id)
110
+ return @deviceClient.userExistsInTag(tag_value, registration_id)
111
+ end
112
+
113
+ =begin
114
+ Tag Adding or Removing Users
115
+ @param tag_value
116
+ @param registration_ids
117
+ =end
118
+ def tagAddingOrRemovingUsers(tag_value, registration_ids)
119
+ return @deviceClient.tagAddingOrRemovingUsers(tag_value, registration_ids)
120
+ end
121
+
122
+ =begin
123
+ Tag Delete
124
+ @param tag_value
125
+ @param platform default is all
126
+ =end
127
+ def tagDelete(tag_value, platform = nil)
128
+ return @deviceClient.tagDelete(tag_value, platform)
129
+ end
130
+
131
+ =begin
132
+ get alias uids
133
+ @param alias_value
134
+ @param platform default is all
135
+ =end
136
+ def getAliasUids(alias_value, platform = nil)
137
+ return @deviceClient.getAliasUids(alias_value, platform)
138
+ end
139
+
140
+ =begin
141
+ Alias Delete
142
+ @param alias_value
143
+ @param platform
144
+ =end
145
+ def aliasDelete(alias_value, platform = nil)
146
+ return @deviceClient.aliasDelete(alias_value, platform)
147
+ end
74
148
  end
75
149
  end
@@ -0,0 +1,32 @@
1
+ require 'json'
2
+
3
+ module JPush
4
+ class AliasUidsResult
5
+ attr_accessor :registration_ids, :isok
6
+
7
+ def initialize
8
+ @isok=false
9
+ end
10
+ def fromResponse(wrapper)
11
+ if wrapper.code != 200
12
+ logger = Logger.new(STDOUT)
13
+ logger.error('Error response from JPush server. Should review and fix it. ')
14
+ logger.info('HTTP Status:' + wrapper.code.to_s)
15
+ logger.info('Error Message:' + wrapper.error.to_s)
16
+ raise JPush::ApiConnectionException.new(wrapper)
17
+ end
18
+ content = wrapper.getResponseContent
19
+ hash = JSON.parse(content)
20
+ @registration_ids = hash['registration_ids']
21
+ @isok=true
22
+ return self
23
+ end
24
+
25
+ def toJSON
26
+ array={}
27
+ array['registration_ids'] = @registration_ids
28
+ return array
29
+ end
30
+
31
+ end
32
+ end
@@ -0,0 +1,33 @@
1
+ require 'json'
2
+ require 'logger'
3
+
4
+ module JPush
5
+ class ExistResult
6
+ attr_accessor :result, :isok
7
+
8
+ def initialize
9
+ @isok=false
10
+ end
11
+ def fromResponse(wrapper)
12
+ if wrapper.code != 200
13
+ logger = Logger.new(STDOUT)
14
+ logger.error('Error response from JPush server. Should review and fix it. ')
15
+ logger.info('HTTP Status:' + wrapper.code.to_s)
16
+ logger.info('Error Message:' + wrapper.error.to_s)
17
+ raise JPush::ApiConnectionException.new(wrapper)
18
+ end
19
+ content = wrapper.getResponseContent
20
+ hash = JSON.parse(content)
21
+ @result = hash['result']
22
+ @isok=true
23
+ return self
24
+ end
25
+
26
+ def toJSON
27
+ array={}
28
+ array['result'] = @result
29
+ return array
30
+ end
31
+
32
+ end
33
+ end
@@ -0,0 +1,30 @@
1
+ require 'json'
2
+
3
+ module JPush
4
+ class GetMessagesResult
5
+ attr_accessor :isok, :result
6
+ def initialize
7
+ @isok = false
8
+ end
9
+
10
+ def fromResponse(wrapper)
11
+ if wrapper.code != 200
12
+ logger = Logger.new(STDOUT)
13
+ logger.error('Error response from JPush server. Should review and fix it. ')
14
+ logger.info('HTTP Status:' + wrapper.code.to_s)
15
+ logger.info('Error Message' + wrapper.error.to_s)
16
+ raise JPush::ApiConnectionException.new(wrapper)
17
+ end
18
+ content = wrapper.getResponseContent
19
+ @result = JSON.parse(content)
20
+ return self
21
+ end
22
+
23
+ def toJSON
24
+ return @result.to_json
25
+ end
26
+
27
+ end
28
+
29
+
30
+ end