jpush 3.1.1

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.
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,101 @@
1
+ require 'jpush'
2
+
3
+ master_secret = '2b38ce69b1de2a7fa95706ea'
4
+ app_key = 'dd1066407b044738b6479275'
5
+ client = JPush::JPushClient.new(app_key, master_secret)
6
+
7
+ logger = Logger.new(STDOUT)
8
+ #send broadcast
9
+ payload1 = JPush::PushPayload.build(
10
+ platform: JPush::Platform.all,
11
+ audience: JPush::Audience.all,
12
+ notification: JPush::Notification.build(
13
+ alert: 'alert meassage')
14
+ )
15
+ result = client.sendPush(payload1)
16
+ logger.debug("Got result " + result.toJSON)
17
+
18
+ #send winphone
19
+ payload1 = JPush::PushPayload.build(
20
+ platform: JPush::Platform.all,
21
+ audience: JPush::Audience.all,
22
+ notification: JPush::Notification.build(
23
+ alert: 'alert meassage',
24
+ winphone: JPush::WinphoneNotification.build(
25
+ alert: "winphone notification alert test",
26
+ title: "winphone notification title test",
27
+ _open_page: "/friends.xaml",
28
+ extras: {"key1" => "value1", "key2" => "value2"})))
29
+ result = client.sendPush(payload1)
30
+ logger.debug("Got result " + result.toJSON)
31
+
32
+ #send android WithExtrasMessage
33
+ payload1 = JPush::PushPayload.build(
34
+ platform: JPush::Platform.all,
35
+ audience: JPush::Audience.all,
36
+ notification: JPush::Notification.build(
37
+ alert: 'alert meassage',
38
+ android: JPush::AndroidNotification.build(
39
+ alert: "android notification alert test",
40
+ title: "android notification title test",
41
+ builder_id: 1,
42
+ extras: {"key1" => "value1", "key2" => "value2"})),
43
+ message: JPush::Message.build(
44
+ msg_content: "message content test",
45
+ title: "message title test",
46
+ content_type: "message content type test",
47
+ extras: {"key1" => "value1", "key2" => "value2"})
48
+ )
49
+ result = client.sendPush(payload1)
50
+ logger.debug("Got result " + result.toJSON)
51
+
52
+ #send ios WithExtrasMessageANDoptions
53
+ payload1 = JPush::PushPayload.build(
54
+ platform: JPush::Platform.all,
55
+ audience: JPush::Audience.all,#audience: JPush::Audience.build{ _alias : "your alias"}
56
+ notification: JPush::Notification.build(
57
+ alert: 'alert meassage',
58
+ ios: JPush::IOSNotification.build(
59
+ alert: "ios notification alert test",
60
+ title: "ios notification title test",
61
+ badge: 1,
62
+ sound: "happy",
63
+ extras: {"key1" => "value1", "key2" => "value2"})),
64
+ message: JPush::Message.build(
65
+ msg_content: "message content test",
66
+ title: "message title test",
67
+ content_type: "message content type test",
68
+ extras: {"key1" => "value1", "key2" => "value2"}),
69
+ options:JPush::Options.build(
70
+ sendno: 1,
71
+ apns_production: true))
72
+ result = client.sendPush(payload1)
73
+ logger.debug("Got result " + result.toJSON)
74
+
75
+ #sendByTag
76
+ payload = JPush::PushPayload.build(
77
+ platform: JPush::Platform.all,
78
+ notification: JPush::Notification.build(
79
+ alert: 'alert'),
80
+ audience: JPush::Audience.build(
81
+ tag: ["tag1"]))
82
+ res = client.sendPush(payload)
83
+ logger.debug("Got result " + res.toJSON)
84
+ #sendByTagAnd
85
+ payload = JPush::PushPayload.build(
86
+ platform: JPush::Platform.all,
87
+ notification: JPush::Notification.build(
88
+ alert: 'alert'),
89
+ audience: JPush::Audience.build(
90
+ tag_and: ["tag1"]))
91
+ res = client.sendPush(payload)
92
+ logger.debug("Got result " + res.toJSON)
93
+ #sendByAlias
94
+ payload = JPush::PushPayload.build(
95
+ platform: JPush::Platform.all,
96
+ notification: JPush::Notification.build(
97
+ alert: 'alert'),
98
+ audience: JPush::Audience.build(
99
+ _alias: ["alias1"]))
100
+ res = client.sendPush(payload)
101
+ logger.debug("Got result " + res.toJSON)
@@ -0,0 +1,18 @@
1
+ require 'jpush'
2
+
3
+ master_secret = '2b38ce69b1de2a7fa95706ea'
4
+ app_key = 'dd1066407b044738b6479275'
5
+ client = JPush::JPushClient.new(app_key, master_secret)
6
+ logger = Logger.new(STDOUT)
7
+
8
+ #getReceiveds
9
+ result = client.getReportReceiveds('1942377665')
10
+ logger.debug("Got result - " + result.toJSON)
11
+
12
+ #getMessages
13
+ #result = client.getReportMessages('1942377665')
14
+ #logger.debug("Got result - " + result)
15
+
16
+ #getUsers
17
+ #result = client.getReportUsers('DAY',"2014-06-10",'3')
18
+ #logger.debug("Got result - " + result)
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "jpush"
7
+ spec.version = "3.1.1"
8
+ spec.authors = ['JPush Offical']
9
+ spec.email = ['support@jpush.cn']
10
+ spec.description = "JPush's officially supported Ruby client library for accessing JPush APIs. http://jpush.cn"
11
+ spec.summary = "JPush's officially supported Ruby client library for accessing JPush APIs."
12
+ spec.homepage = "https://github.com/jpush/jpush-api-ruby-client"
13
+ spec.license = "GNU"
14
+
15
+ spec.files = `git ls-files`.split($/)
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_development_dependency "rspec", "~> 2.6"
21
+ #spec.add_runtime_dependency "rest-client","~> 1.6.7"
22
+ #spec.add_runtime_dependency "json", "~> 1.7.7"
23
+
24
+ end
@@ -0,0 +1,20 @@
1
+
2
+ module JPush
3
+
4
+ #V3
5
+ autoload :JPushClient,'jpush/jpush_client.rb'
6
+ autoload :NativeHttpClient,'jpush/http_client'
7
+ autoload :PushClient,'jpush/push_client'
8
+ autoload :ReportClient,'jpush/report_client'
9
+ autoload :Audience,'jpush/model/audience'
10
+ autoload :Message,'jpush/model/message'
11
+ autoload :Options,'jpush/model/options'
12
+ autoload :Platform,'jpush/model/platform'
13
+ autoload :PushPayload,'jpush/model/push_payload'
14
+ autoload :AndroidNotification,'jpush/model/notification/android_notification'
15
+ autoload :IOSNotification,'jpush/model/notification/ios_notification'
16
+ autoload :WinphoneNotification,'jpush/model/notification/winphone_notification'
17
+ autoload :Notification,'jpush/model/notification/notification'
18
+ autoload :PushResult, 'jpush/model/push_result'
19
+ autoload :ReceivedsResult, 'jpush/model/receiveds_result'
20
+ end
@@ -0,0 +1,124 @@
1
+ path = File.expand_path('../', __FILE__)
2
+ require File.join(path, 'response_wrapper.rb')
3
+ require 'net/http'
4
+ require 'json'
5
+ require 'logger'
6
+ require 'uri'
7
+ require 'net/https'
8
+
9
+ module JPush
10
+ class NativeHttpClient
11
+ def initialize(maxRetryTimes = 5)
12
+ @maxRetryTimes = maxRetryTimes
13
+ @logger = Logger.new(STDOUT)
14
+ end
15
+
16
+ def sendPost(url, content, authCode)
17
+ return sendRequest(url, content, 'POST', authCode)
18
+ end
19
+
20
+ def sendGet(url, content, authCode)
21
+ return sendRequest(url, content, 'GET', authCode)
22
+ end
23
+ private
24
+
25
+ def sendRequest(url, content, method, authCode)
26
+ wrapper = _sendRequest(url, content, method, authCode)
27
+ retryTimes = 0
28
+ while retryTimes < @maxRetryTimes
29
+ begin
30
+ response = _sendRequest(url, content, method, authCode)
31
+ break
32
+ rescue
33
+ if retryTimes > @maxRetryTimes
34
+ raise RuntimeError.new('connect error')
35
+ else
36
+ @logger.debug('Retry again - ' + (retryTimes + 1))
37
+ retryTimes = retryTimes + 1
38
+ end
39
+ end
40
+ end
41
+ return wrapper
42
+ end
43
+ private
44
+
45
+ def _sendRequest(url, content, method, authCode)
46
+ begin
47
+
48
+ header = {}
49
+ header['User-Agent'] = 'JPush-API-Ruby-Client'
50
+ header['Connection'] = 'Keep-Alive'
51
+ header['Charset'] = 'UTF-8'
52
+ header['Content-Type'] = 'application/json'
53
+ header['Authorization'] = authCode
54
+ #url = url+content
55
+ uri = URI.parse(url)
56
+ http = Net::HTTP.new(uri.host, uri.port)
57
+ http.use_ssl = true
58
+ #http.verify_mode = OpenSSL::SSL::VERIFY_NONE
59
+ http.open_timeout = 5
60
+ http.read_timeout = 30
61
+ use_ssl = true
62
+ if method == 'POST' && use_ssl == true
63
+ req = Net::HTTP::Post.new(uri.path, initheader = header)
64
+ req.body = content
65
+ response = http.request(req)
66
+ elsif method == 'GET' && use_ssl == true
67
+ request = Net::HTTP::Get.new(uri.request_uri, initheader = header)
68
+ response = http.request(request)
69
+ end
70
+ #if method == 'POST'
71
+ # @response = http.post(path,content,header)
72
+ #elsif method == 'GET'
73
+ # @response = http.get2(path,content,header)
74
+ # end
75
+ code = response.code
76
+ code = Integer(code)
77
+ wrapper = JPush::ResponseWrapper.new
78
+ wrapper.code = code
79
+ wrapper.setResponseContent(response.body)
80
+ headers = response.header.to_hash
81
+ quota = response['X-Rate-Limit-Limit']
82
+ remaining = response['X-Rate-Limit-Remaining']
83
+ reset = response['X-Rate-Limit-Reset']
84
+
85
+ wrapper.setRateLimit(Integer(quota), Integer(remaining), Integer(reset))
86
+ if code == 200
87
+ @logger.debug('Succeed to get response - 200 OK')
88
+ if content != nil
89
+ @logger.debug('Response Content -' + response.body)
90
+ end
91
+ elsif code > 200 && code < 400
92
+ @logger.warn('Normal response but unexpected - responseCode:' + code.to_s + ',responseContent = ' + response.body)
93
+ else
94
+ @logger.error('Got error response - responseCode:' + code.to_s + ', responseContent:' + response.body)
95
+ case code
96
+ when 400
97
+ @logger.error('Your request params is invalid. Please check them according to error message.')
98
+ wrapper.setErrorObject()
99
+ when 401
100
+ @logger.error('Authentication failed! Please check authentication params according to docs.')
101
+ wrapper.setErrorObject()
102
+ when 403
103
+ @logger.error('Request is forbidden! Maybe your appkey is listed in blacklist?')
104
+ wrapper.setErrorObject()
105
+ when 410
106
+ @logger.error('Request resource is no longer in service. Please according to notice on official website.')
107
+ wrapper.setErrorObject()
108
+ when 429
109
+ @logger.error('Too many requests! Please review your appkey request quota.')
110
+ wrapper.setErrorObject()
111
+ when 501..504
112
+ @logger.error('Seems encountered server error. Maybe JPush is in maintenance? Please retry later.')
113
+ else
114
+ @logger.error('Unexpected response.')
115
+ end
116
+ end
117
+ rescue SocketError => ex
118
+ raise SocketError.new('socket build error')
119
+ end
120
+ return wrapper
121
+ end
122
+ end
123
+ end
124
+
@@ -0,0 +1,75 @@
1
+ path = File.expand_path('../', __FILE__)
2
+ require File.join(path, 'http_client.rb')
3
+ require File.join(path, 'push_client.rb')
4
+ require File.join(path, 'report_client.rb')
5
+ require File.join(path, 'util/service_helper.rb')
6
+
7
+ module JPush
8
+ =begin
9
+ The global entrance of JPush API library.
10
+ =end
11
+ class JPushClient
12
+ =begin
13
+ Create a JPush Client.
14
+ masterSecret API access secret of the appKey.
15
+ appKey The KEY of one application on JPush.
16
+ =end
17
+
18
+ def initialize(appkey, masterSecret, maxRetryTimes = 5)
19
+ begin
20
+ @logger = Logger.new(STDOUT)
21
+ if appkey.class == String && appkey.length == 24 then
22
+ @appkey = appkey
23
+ else
24
+ @logger.error('appkey is format error ')
25
+ end
26
+ if masterSecret.class == String && masterSecret.length == 24 then
27
+ @masterSecret = masterSecret
28
+
29
+ else
30
+ @logger.error('masterSecret is format error')
31
+ end
32
+ end
33
+ @masterSecret = masterSecret
34
+ @pushClient = JPush::PushClient.new(maxRetryTimes = maxRetryTimes)
35
+ @reportClient = JPush::ReportClient.new(maxRetryTimes = maxRetryTimes)
36
+ @authcode = ServiceHelper.getAuthorizationBase64(appkey, masterSecret)
37
+ end
38
+
39
+ # Send a push with object.
40
+ # @param pushPayload payload object of a push.
41
+ # @return JSON data.
42
+ def sendPush(payload)
43
+ result = @pushClient.sendPush(payload, @authcode)
44
+ return result
45
+ end
46
+
47
+ #Get received report.
48
+ # @param msgIds 100 msgids to batch getting is supported.
49
+ # @return JSON data.
50
+ def getReportReceiveds(msgIds)
51
+ result = @reportClient.getReceiveds(msgIds, @authcode)
52
+ return result
53
+ end
54
+
55
+ #Get message report.
56
+ # @param msgIds 100 msgids to batch getting is supported.
57
+ # @return JSON data.
58
+ def getReportMessages(msgIds)
59
+ result = @reportClient.getMessages(msgIds, @authcode)
60
+ return result
61
+ end
62
+
63
+ #Get user report.
64
+ #@param timeunit is 'DAY','HOUR' or 'MONTH'
65
+ #@param start is a string for example 2014-06-10
66
+ #@duration
67
+ # @return JSON data.
68
+
69
+ def getReportUsers(timeUnit, start, duration)
70
+ result = @reportClient.getUsers(timeUnit, start, duration, @authcode)
71
+ return result
72
+ end
73
+
74
+ end
75
+ end
@@ -0,0 +1,67 @@
1
+ module JPush
2
+ class Audience
3
+ attr_accessor :tag, :tag_and, :_alias, :registration_id, :segment, :all
4
+ def initialize(opts = {})
5
+ @all = false
6
+ @tag = opts[:tag]
7
+ @tag_and = opts[:tag_and]
8
+ @_alias = opts[:_alias]
9
+ @registration_id = opts[:registration_id]
10
+ @segment = opts[:segment]
11
+ end
12
+
13
+ def toJSON
14
+ if @all == true then
15
+ return 'all'
16
+ end
17
+ array = {}
18
+ if @tag != nil then
19
+ if @tag.class != Array
20
+ raise ArgumentError.new('tag is not Array')
21
+ end
22
+ array['tag'] = @tag
23
+ end
24
+ if @_alias != nil then
25
+ if @_alias.class != Array
26
+ raise ArgumentError.new('alias is not Array')
27
+ end
28
+ array['alias'] = @_alias
29
+ end
30
+ if @tag_and != nil then
31
+ if @tag_and.class != Array
32
+ raise ArgumentError.new('tag_end is not Array')
33
+ end
34
+ array['tag_and'] = @tag_and
35
+ end
36
+ if @registration_id != nil then
37
+ if @registration_id.class != Array
38
+ raise ArgumentError.new('registration_id is not Array')
39
+ end
40
+ array['registration_id'] = @registration_id
41
+ end
42
+ return array
43
+ end
44
+
45
+ def self.all
46
+ au = JPush::Audience.new
47
+ au.all = true
48
+ return au
49
+ end
50
+
51
+ def self.build(opts = {})
52
+ audience = JPush::Audience.new(opts)
53
+ if audience.all == true
54
+ if audience.tag != nil || audience._alias != nil || audience.tag_and != nil || audience.registration_id != nil
55
+ raise ArgumentError.new('If audience is all, no any other audience may be set.')
56
+ end
57
+ end
58
+ if audience.all == false
59
+ if audience.tag == nil && audience._alias == nil && audience.tag_and == nil && audience.registration_id == nil
60
+ raise ArgumentError.new('No any audience target is set.')
61
+ end
62
+ end
63
+ return audience
64
+ end
65
+
66
+ end
67
+ end
@@ -0,0 +1,37 @@
1
+ module JPush
2
+ class Message
3
+ attr_accessor :title, :msg_content, :content_type, :extras
4
+ def initialize(opts = {})
5
+ @title = opts[:title]
6
+ @msg_content = opts[:msg_content]
7
+ @content_type = opts[:content_type]
8
+ @extras = opts[:extras]
9
+ end
10
+
11
+ def toJSON
12
+ array = {}
13
+ if @title != nil then
14
+ array['title'] = @title
15
+ end
16
+ if @msg_content != nil then
17
+ array['msg_content'] = @msg_content
18
+ end
19
+ if @content_type != nil then
20
+ array['content_type'] = @content_type
21
+ end
22
+ if @extras != nil then
23
+ array['extras'] = @extras
24
+ end
25
+ return array
26
+ end
27
+
28
+ def self.build(opts = {})
29
+ message = JPush::Message.new(opts)
30
+ if nil == message.msg_content
31
+ raise ArgumentError.new('msgContent should be set')
32
+ end
33
+ return message
34
+ end
35
+
36
+ end
37
+ end