jpush 4.0.10 → 4.0.11

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,8 +1,8 @@
1
- module JPush
2
- class Handler
3
-
4
- def initialize(jpush)
5
- @jpush = jpush
6
- end
7
- end
1
+ module JPush
2
+ class Handler
3
+
4
+ def initialize(jpush)
5
+ @jpush = jpush
6
+ end
7
+ end
8
8
  end
@@ -1,100 +1,100 @@
1
- require_relative 'response'
2
- require 'net/http'
3
- require 'json'
4
- require 'jpush/utils/exceptions'
5
-
6
- module JPush
7
- module Http
8
- class Client
9
-
10
- class << self
11
-
12
- def get(jpush, url, params: nil, headers: {})
13
- request(jpush, :get, url, params: params, headers: headers)
14
- end
15
-
16
- def post(jpush, url, body: , headers: {})
17
- request(jpush, :post, url, body: body, headers: headers)
18
- end
19
-
20
- def put(jpush, url, body: , headers: {})
21
- request(jpush, :put, url, body: body, headers: headers)
22
- end
23
-
24
- def delete(jpush, url, params: nil, headers: {})
25
- request(jpush, :delete, url, params: params, headers: headers)
26
- end
27
-
28
- def request(jpush, method, url, params: nil, body: nil, headers: {}, opts: {})
29
- raw_response = self.new(
30
- jpush,
31
- method,
32
- url,
33
- params: params,
34
- body: body,
35
- headers: headers,
36
- opts: opts
37
- ).send_request
38
-
39
- Response.new(raw_response)
40
- end
41
-
42
- end
43
-
44
- DEFAULT_USER_AGENT = 'jpush-api-ruby-client/' + JPush::VERSION
45
- DEFAULT_OPEN_TIMEOUT = 20
46
- DEFAULT_READ_TIMEOUT = 120
47
- DEFAULT_RETRY_TIMES = 3
48
- RETRY_SLEEP_TIME = 3
49
-
50
- HTTP_VERB_MAP = {
51
- get: Net::HTTP::Get,
52
- post: Net::HTTP::Post,
53
- put: Net::HTTP::Put,
54
- delete: Net::HTTP::Delete
55
- }
56
-
57
- DEFAULT_HEADERS = {
58
- 'user-agent' => DEFAULT_USER_AGENT,
59
- 'accept' => 'application/json',
60
- 'content-type' => 'application/json',
61
- 'connection' => 'close'
62
- }
63
-
64
- def initialize(jpush, method, url, params: nil, body: nil, headers: {}, opts: {})
65
- method = method.downcase.to_sym
66
- @uri = URI(url)
67
- @uri.query = URI.encode_www_form(params) unless params.nil?
68
- @request = prepare_request(method, body, headers)
69
- @request.basic_auth(jpush.app_key, jpush.master_secret)
70
- @opts = opts
71
- end
72
-
73
- def send_request
74
- tries ||= DEFAULT_RETRY_TIMES
75
- opts ||= {
76
- use_ssl: 'https' == @uri.scheme,
77
- open_timeout: DEFAULT_OPEN_TIMEOUT,
78
- read_timeout: DEFAULT_READ_TIMEOUT
79
- }.merge @opts
80
- Net::HTTP.start(@uri.host, @uri.port, opts) do |http|
81
- http.request(@request)
82
- end
83
- # if raise Timeout::Error retry it for 3 times
84
- rescue Net::OpenTimeout, Net::ReadTimeout => e
85
- (tries -= 1).zero? ? (raise Utils::Exceptions::TimeOutError.new(e)) : retry
86
- end
87
-
88
- private
89
-
90
- def prepare_request(method, body, headers)
91
- headers = DEFAULT_HEADERS.merge(headers)
92
- request = HTTP_VERB_MAP[method].new @uri
93
- request.initialize_http_header(headers)
94
- request.body = body.to_json unless body.nil?
95
- request
96
- end
97
-
98
- end
99
- end
100
- end
1
+ require_relative 'response'
2
+ require 'net/http'
3
+ require 'json'
4
+ require 'jpush/utils/exceptions'
5
+
6
+ module JPush
7
+ module Http
8
+ class Client
9
+
10
+ class << self
11
+
12
+ def get(jpush, url, params: nil, headers: {})
13
+ request(jpush, :get, url, params: params, headers: headers)
14
+ end
15
+
16
+ def post(jpush, url, body: , headers: {})
17
+ request(jpush, :post, url, body: body, headers: headers)
18
+ end
19
+
20
+ def put(jpush, url, body: , headers: {})
21
+ request(jpush, :put, url, body: body, headers: headers)
22
+ end
23
+
24
+ def delete(jpush, url, params: nil, headers: {})
25
+ request(jpush, :delete, url, params: params, headers: headers)
26
+ end
27
+
28
+ def request(jpush, method, url, params: nil, body: nil, headers: {}, opts: {})
29
+ raw_response = self.new(
30
+ jpush,
31
+ method,
32
+ url,
33
+ params: params,
34
+ body: body,
35
+ headers: headers,
36
+ opts: opts
37
+ ).send_request
38
+
39
+ Response.new(raw_response)
40
+ end
41
+
42
+ end
43
+
44
+ DEFAULT_USER_AGENT = 'jpush-api-ruby-client/' + JPush::VERSION
45
+ DEFAULT_OPEN_TIMEOUT = 20
46
+ DEFAULT_READ_TIMEOUT = 120
47
+ DEFAULT_RETRY_TIMES = 3
48
+ RETRY_SLEEP_TIME = 3
49
+
50
+ HTTP_VERB_MAP = {
51
+ get: Net::HTTP::Get,
52
+ post: Net::HTTP::Post,
53
+ put: Net::HTTP::Put,
54
+ delete: Net::HTTP::Delete
55
+ }
56
+
57
+ DEFAULT_HEADERS = {
58
+ 'user-agent' => DEFAULT_USER_AGENT,
59
+ 'accept' => 'application/json',
60
+ 'content-type' => 'application/json',
61
+ 'connection' => 'close'
62
+ }
63
+
64
+ def initialize(jpush, method, url, params: nil, body: nil, headers: {}, opts: {})
65
+ method = method.downcase.to_sym
66
+ @uri = URI(url)
67
+ @uri.query = URI.encode_www_form(params) unless params.nil?
68
+ @request = prepare_request(method, body, headers)
69
+ @request.basic_auth(jpush.app_key, jpush.master_secret)
70
+ @opts = opts
71
+ end
72
+
73
+ def send_request
74
+ tries ||= DEFAULT_RETRY_TIMES
75
+ opts ||= {
76
+ use_ssl: 'https' == @uri.scheme,
77
+ open_timeout: DEFAULT_OPEN_TIMEOUT,
78
+ read_timeout: DEFAULT_READ_TIMEOUT
79
+ }.merge @opts
80
+ Net::HTTP.start(@uri.host, @uri.port, opts) do |http|
81
+ http.request(@request)
82
+ end
83
+ # if raise Timeout::Error retry it for 3 times
84
+ rescue Net::OpenTimeout, Net::ReadTimeout => e
85
+ (tries -= 1).zero? ? (raise Utils::Exceptions::TimeOutError.new(e)) : retry
86
+ end
87
+
88
+ private
89
+
90
+ def prepare_request(method, body, headers)
91
+ headers = DEFAULT_HEADERS.merge(headers)
92
+ request = HTTP_VERB_MAP[method].new @uri
93
+ request.initialize_http_header(headers)
94
+ request.body = body.to_json unless body.nil?
95
+ request
96
+ end
97
+
98
+ end
99
+ end
100
+ end
@@ -1,33 +1,33 @@
1
- module JPush
2
- module Http
3
- class Response
4
-
5
- attr_accessor :http_code, :body, :heades
6
-
7
- def initialize(raw_response)
8
- @http_code = raw_response.code.to_i
9
- @body = parse_body(raw_response.body)
10
- build_error unless raw_response.kind_of? Net::HTTPSuccess
11
- end
12
-
13
- private
14
-
15
- def parse_body(body)
16
- body = JSON.parse(body)
17
- rescue JSON::ParserError
18
- body
19
- end
20
-
21
- def build_error
22
- error_code, error_message =
23
- if @body.has_key?('error')
24
- [@body['error']['code'], @body['error']['message']]
25
- else
26
- [@body['code'], @body['message']]
27
- end
28
- raise Utils::Exceptions::JPushResponseError.new(http_code, error_code, error_message)
29
- end
30
-
31
- end
32
- end
33
- end
1
+ module JPush
2
+ module Http
3
+ class Response
4
+
5
+ attr_accessor :http_code, :body, :heades
6
+
7
+ def initialize(raw_response)
8
+ @http_code = raw_response.code.to_i
9
+ @body = parse_body(raw_response.body)
10
+ build_error unless raw_response.kind_of? Net::HTTPSuccess
11
+ end
12
+
13
+ private
14
+
15
+ def parse_body(body)
16
+ body = JSON.parse(body)
17
+ rescue JSON::ParserError
18
+ raise Utils::Exceptions::JPushResponseError.new(http_code, http_code, body)
19
+ end
20
+
21
+ def build_error
22
+ error_code, error_message =
23
+ if @body.has_key?('error')
24
+ [@body['error']['code'], @body['error']['message']]
25
+ else
26
+ [@body['code'], @body['message']]
27
+ end
28
+ raise Utils::Exceptions::JPushResponseError.new(http_code, error_code, error_message)
29
+ end
30
+
31
+ end
32
+ end
33
+ end
@@ -1,55 +1,55 @@
1
- module JPush
2
- module Push
3
- class Audience
4
-
5
- def set_tag(tags)
6
- @tag = [tags].flatten
7
- self
8
- end
9
-
10
- def set_tag_and(tags)
11
- @tag_and = [tags].flatten
12
- self
13
- end
14
-
15
- def set_tag_not(tags)
16
- @tag_not = [tags].flatten
17
- self
18
- end
19
-
20
- def set_alias(alis)
21
- @alias = [alis].flatten
22
- self
23
- end
24
-
25
- def set_registration_id(registration_ids)
26
- @registration_id = [registration_ids].flatten
27
- self
28
- end
29
-
30
- def set_segment(segment)
31
- @segment = [segment].flatten
32
- self
33
- end
34
-
35
- def set_abtest(abtest)
36
- @abtest = [abtest].flatten
37
- self
38
- end
39
-
40
- def to_hash
41
- @audience = {
42
- tag: @tag,
43
- tag_and: @tag_and,
44
- tag_not: @tag_not,
45
- alias: @alias,
46
- registration_id: @registration_id,
47
- segment: @segment,
48
- abtest: @abtest
49
- }.select { |_, value| !value.nil? }
50
- @audience
51
- end
52
-
53
- end
54
- end
55
- end
1
+ module JPush
2
+ module Push
3
+ class Audience
4
+
5
+ def set_tag(tags)
6
+ @tag = [tags].flatten
7
+ self
8
+ end
9
+
10
+ def set_tag_and(tags)
11
+ @tag_and = [tags].flatten
12
+ self
13
+ end
14
+
15
+ def set_tag_not(tags)
16
+ @tag_not = [tags].flatten
17
+ self
18
+ end
19
+
20
+ def set_alias(alis)
21
+ @alias = [alis].flatten
22
+ self
23
+ end
24
+
25
+ def set_registration_id(registration_ids)
26
+ @registration_id = [registration_ids].flatten
27
+ self
28
+ end
29
+
30
+ def set_segment(segment)
31
+ @segment = [segment].flatten
32
+ self
33
+ end
34
+
35
+ def set_abtest(abtest)
36
+ @abtest = [abtest].flatten
37
+ self
38
+ end
39
+
40
+ def to_hash
41
+ @audience = {
42
+ tag: @tag,
43
+ tag_and: @tag_and,
44
+ tag_not: @tag_not,
45
+ alias: @alias,
46
+ registration_id: @registration_id,
47
+ segment: @segment,
48
+ abtest: @abtest
49
+ }.select { |_, value| !value.nil? }
50
+ @audience
51
+ end
52
+
53
+ end
54
+ end
55
+ end
@@ -1,66 +1,66 @@
1
- module JPush
2
- module Push
3
- class Notification
4
-
5
- def set_alert(alert)
6
- @alert = alert
7
- self
8
- end
9
-
10
- def set_not_alert
11
- @alert = ''
12
- self
13
- end
14
-
15
- def set_android(alert: , title: nil, builder_id: nil, channel_id: nil,
16
- priority: nil, category: nil, style: nil, alert_type: nil, big_text: nil, inbox: nil, big_pic_path: nil, extras: nil,
17
- large_icon: nil, intent: nil)
18
- @android = {
19
- alert: alert,
20
- title: title,
21
- builder_id: builder_id,
22
- channel_id: channel_id,
23
- priority: priority,
24
- category: category,
25
- style: style,
26
- alert_type: alert_type,
27
- big_text: big_text,
28
- inbox: inbox,
29
- big_pic_path: big_pic_path,
30
- extras: extras,
31
- large_icon: large_icon,
32
- intent: intent
33
- }.select { |_, value| !value.nil? }
34
- self
35
- end
36
-
37
- def set_ios(alert: , sound: nil, badge: '+1', available: nil, category:nil, extras: nil, contentavailable: nil, mutablecontent: nil, thread: nil)
38
- contentavailable = available if contentavailable.nil?
39
- contentavailable = nil unless contentavailable.is_a? TrueClass
40
- mutablecontent = nil unless mutablecontent.is_a? TrueClass
41
- @ios = {
42
- alert: alert,
43
- sound: sound,
44
- badge: badge,
45
- 'content-available': contentavailable,
46
- 'mutable-content': mutablecontent,
47
- category: category,
48
- extras: extras,
49
- 'thread-id': thread
50
- }.select { |_, value| !value.nil? }
51
- self
52
- end
53
-
54
- def to_hash
55
- @notification = {
56
- alert: @alert,
57
- android: @android,
58
- ios: @ios
59
- }.select { |_, value| !value.nil? }
60
- raise Utils::Exceptions::JPushError, 'Notification can not be empty.' if @notification.empty?
61
- @notification
62
- end
63
-
64
- end
65
- end
66
- end
1
+ module JPush
2
+ module Push
3
+ class Notification
4
+
5
+ def set_alert(alert)
6
+ @alert = alert
7
+ self
8
+ end
9
+
10
+ def set_not_alert
11
+ @alert = ''
12
+ self
13
+ end
14
+
15
+ def set_android(alert: , title: nil, builder_id: nil, channel_id: nil,
16
+ priority: nil, category: nil, style: nil, alert_type: nil, big_text: nil, inbox: nil, big_pic_path: nil, extras: nil,
17
+ large_icon: nil, intent: nil)
18
+ @android = {
19
+ alert: alert,
20
+ title: title,
21
+ builder_id: builder_id,
22
+ channel_id: channel_id,
23
+ priority: priority,
24
+ category: category,
25
+ style: style,
26
+ alert_type: alert_type,
27
+ big_text: big_text,
28
+ inbox: inbox,
29
+ big_pic_path: big_pic_path,
30
+ extras: extras,
31
+ large_icon: large_icon,
32
+ intent: intent
33
+ }.select { |_, value| !value.nil? }
34
+ self
35
+ end
36
+
37
+ def set_ios(alert: , sound: nil, badge: '+1', available: nil, category:nil, extras: nil, contentavailable: nil, mutablecontent: nil, thread: nil)
38
+ contentavailable = available if contentavailable.nil?
39
+ contentavailable = nil unless contentavailable.is_a? TrueClass
40
+ mutablecontent = nil unless mutablecontent.is_a? TrueClass
41
+ @ios = {
42
+ alert: alert,
43
+ sound: sound,
44
+ badge: badge,
45
+ 'content-available': contentavailable,
46
+ 'mutable-content': mutablecontent,
47
+ category: category,
48
+ extras: extras,
49
+ 'thread-id': thread
50
+ }.select { |_, value| !value.nil? }
51
+ self
52
+ end
53
+
54
+ def to_hash
55
+ @notification = {
56
+ alert: @alert,
57
+ android: @android,
58
+ ios: @ios
59
+ }.select { |_, value| !value.nil? }
60
+ raise Utils::Exceptions::JPushError, 'Notification can not be empty.' if @notification.empty?
61
+ @notification
62
+ end
63
+
64
+ end
65
+ end
66
+ end