maia 3.0.0 → 5.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +35 -59
- data/app/controllers/concerns/maia/controller.rb +0 -5
- data/app/models/maia/device.rb +4 -0
- data/lib/maia.rb +23 -11
- data/lib/maia/devices.rb +21 -0
- data/lib/maia/error/generic.rb +7 -0
- data/lib/maia/error/no_credentials.rb +9 -0
- data/lib/maia/error/unregistered.rb +12 -0
- data/lib/maia/fcm/connection.rb +18 -18
- data/lib/maia/fcm/credentials.rb +41 -0
- data/lib/maia/fcm/gateway.rb +30 -0
- data/lib/maia/fcm/notification.rb +14 -12
- data/lib/maia/fcm/platform/android.rb +37 -0
- data/lib/maia/fcm/platform/apns.rb +42 -0
- data/lib/maia/fcm/response.rb +17 -31
- data/lib/maia/fcm/serializer.rb +38 -0
- data/lib/maia/message.rb +24 -65
- data/lib/maia/messengers/activejob.rb +19 -0
- data/lib/maia/messengers/array.rb +21 -0
- data/lib/maia/messengers/inline.rb +13 -0
- data/lib/maia/token.rb +21 -0
- data/lib/maia/topic.rb +21 -0
- data/lib/maia/version.rb +1 -1
- metadata +29 -65
- data/lib/maia/dry_run.rb +0 -15
- data/lib/maia/error.rb +0 -4
- data/lib/maia/fcm.rb +0 -7
- data/lib/maia/fcm/response_collection.rb +0 -34
- data/lib/maia/fcm/result.rb +0 -31
- data/lib/maia/fcm/result_collection.rb +0 -35
- data/lib/maia/fcm/service.rb +0 -48
- data/lib/maia/messenger.rb +0 -61
@@ -1,26 +1,28 @@
|
|
1
1
|
module Maia
|
2
2
|
module FCM
|
3
3
|
class Notification
|
4
|
-
|
5
|
-
|
6
|
-
def initialize(attributes = {})
|
7
|
-
@attributes = attributes
|
4
|
+
def initialize(message)
|
5
|
+
@message = message
|
8
6
|
end
|
9
7
|
|
10
|
-
def
|
11
|
-
@
|
8
|
+
def title
|
9
|
+
@message.title
|
12
10
|
end
|
13
11
|
|
14
|
-
def
|
15
|
-
|
12
|
+
def body
|
13
|
+
@message.body
|
16
14
|
end
|
17
15
|
|
18
|
-
def
|
19
|
-
@
|
16
|
+
def image
|
17
|
+
@message.image
|
20
18
|
end
|
21
19
|
|
22
|
-
def
|
23
|
-
|
20
|
+
def to_h
|
21
|
+
{
|
22
|
+
title: title,
|
23
|
+
body: body,
|
24
|
+
image: image
|
25
|
+
}.compact
|
24
26
|
end
|
25
27
|
end
|
26
28
|
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
module Maia
|
2
|
+
module FCM
|
3
|
+
module Platform
|
4
|
+
class Android
|
5
|
+
def initialize(message)
|
6
|
+
@message = message
|
7
|
+
end
|
8
|
+
|
9
|
+
def color
|
10
|
+
@message.color
|
11
|
+
end
|
12
|
+
|
13
|
+
def sound
|
14
|
+
@message.sound
|
15
|
+
end
|
16
|
+
|
17
|
+
def priority
|
18
|
+
if @message.priority == :high
|
19
|
+
:high
|
20
|
+
else
|
21
|
+
:normal
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def to_h
|
26
|
+
{
|
27
|
+
priority: priority.to_s,
|
28
|
+
notification: {
|
29
|
+
color: color,
|
30
|
+
sound: sound,
|
31
|
+
}.compact
|
32
|
+
}
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
module Maia
|
2
|
+
module FCM
|
3
|
+
module Platform
|
4
|
+
class APNS
|
5
|
+
def initialize(message)
|
6
|
+
@message = message
|
7
|
+
end
|
8
|
+
|
9
|
+
def badge
|
10
|
+
@message.badge
|
11
|
+
end
|
12
|
+
|
13
|
+
def sound
|
14
|
+
@message.sound
|
15
|
+
end
|
16
|
+
|
17
|
+
def priority
|
18
|
+
if @message.priority == :high && !@message.background?
|
19
|
+
10
|
20
|
+
else
|
21
|
+
5
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def to_h
|
26
|
+
{
|
27
|
+
headers: {
|
28
|
+
'apns-priority': priority.to_s
|
29
|
+
}.compact,
|
30
|
+
payload: {
|
31
|
+
aps: {
|
32
|
+
badge: badge,
|
33
|
+
sound: sound,
|
34
|
+
'content-available': (1 if @message.background?)
|
35
|
+
}.compact
|
36
|
+
}
|
37
|
+
}
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
data/lib/maia/fcm/response.rb
CHANGED
@@ -1,15 +1,16 @@
|
|
1
1
|
module Maia
|
2
2
|
module FCM
|
3
3
|
class Response
|
4
|
-
|
4
|
+
def initialize(response)
|
5
|
+
@response = response
|
6
|
+
end
|
5
7
|
|
6
|
-
def
|
7
|
-
@
|
8
|
-
@tokens = tokens
|
8
|
+
def body
|
9
|
+
@response.body
|
9
10
|
end
|
10
11
|
|
11
12
|
def status
|
12
|
-
|
13
|
+
@response.code.to_i
|
13
14
|
end
|
14
15
|
|
15
16
|
def success?
|
@@ -20,36 +21,21 @@ module Maia
|
|
20
21
|
!success?
|
21
22
|
end
|
22
23
|
|
23
|
-
def results
|
24
|
-
@_results ||= begin
|
25
|
-
results = to_h.fetch 'results', []
|
26
|
-
results.map!.with_index do |attributes, i|
|
27
|
-
Result.new attributes, tokens[i]
|
28
|
-
end
|
29
|
-
ResultCollection.new(results)
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
24
|
def error
|
34
|
-
case status
|
35
|
-
when
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
when 500..599
|
40
|
-
'FCM Internal server error.'
|
25
|
+
case json.dig('error', 'status')
|
26
|
+
when 'UNREGISTERED'
|
27
|
+
Maia::Error::Unregistered.new
|
28
|
+
else
|
29
|
+
Maia::Error::Generic.new json.dig('error', 'message')
|
41
30
|
end
|
42
31
|
end
|
43
32
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
rescue JSON::ParserError
|
51
|
-
{}
|
52
|
-
end
|
33
|
+
private
|
34
|
+
def json
|
35
|
+
JSON.parse body
|
36
|
+
rescue JSON::ParserError
|
37
|
+
{}
|
38
|
+
end
|
53
39
|
end
|
54
40
|
end
|
55
41
|
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
module Maia
|
2
|
+
module FCM
|
3
|
+
class Serializer
|
4
|
+
def initialize(message, target)
|
5
|
+
@message = message
|
6
|
+
@target = target
|
7
|
+
end
|
8
|
+
|
9
|
+
def to_h
|
10
|
+
{
|
11
|
+
message: {
|
12
|
+
data: @message.data.to_h,
|
13
|
+
notification: notification.to_h,
|
14
|
+
android: android.to_h,
|
15
|
+
apns: apns.to_h
|
16
|
+
}.merge(@target.to_h)
|
17
|
+
}
|
18
|
+
end
|
19
|
+
|
20
|
+
def to_json
|
21
|
+
to_h.to_json
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
def notification
|
26
|
+
Maia::FCM::Notification.new @message
|
27
|
+
end
|
28
|
+
|
29
|
+
def android
|
30
|
+
Maia::FCM::Platform::Android.new @message
|
31
|
+
end
|
32
|
+
|
33
|
+
def apns
|
34
|
+
Maia::FCM::Platform::APNS.new @message
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
data/lib/maia/message.rb
CHANGED
@@ -1,82 +1,41 @@
|
|
1
1
|
module Maia
|
2
2
|
class Message
|
3
|
-
def
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
end
|
11
|
-
|
12
|
-
def enqueue(worker, devices)
|
13
|
-
devices.find_in_batches(batch_size: Maia::BATCH_SIZE) do |devices|
|
14
|
-
worker.perform_later devices.collect(&:token), to_h.deep_stringify_keys
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
def title
|
19
|
-
end
|
20
|
-
|
21
|
-
def body
|
22
|
-
end
|
23
|
-
|
24
|
-
def on_click
|
25
|
-
end
|
26
|
-
|
27
|
-
def icon
|
28
|
-
end
|
29
|
-
|
30
|
-
def sound
|
31
|
-
:default
|
32
|
-
end
|
33
|
-
|
34
|
-
def badge
|
35
|
-
end
|
36
|
-
|
37
|
-
def color
|
38
|
-
end
|
3
|
+
def title; end
|
4
|
+
def body; end
|
5
|
+
def image; end
|
6
|
+
def badge; end
|
7
|
+
def color; end
|
8
|
+
def background?; end
|
9
|
+
def priority; end
|
39
10
|
|
40
11
|
def data
|
12
|
+
{}
|
41
13
|
end
|
42
14
|
|
43
|
-
def
|
44
|
-
|
15
|
+
def sound
|
16
|
+
'default'
|
45
17
|
end
|
46
18
|
|
47
|
-
def
|
48
|
-
|
19
|
+
def targeting(target)
|
20
|
+
tap { @target = target }
|
49
21
|
end
|
50
22
|
|
51
|
-
def
|
52
|
-
|
23
|
+
def to_json
|
24
|
+
to_h.to_json
|
53
25
|
end
|
54
26
|
|
55
|
-
def
|
56
|
-
|
57
|
-
|
27
|
+
def send_to(*models, topic: nil, token: nil, messenger: Maia.messenger)
|
28
|
+
targets = []
|
29
|
+
targets << Maia::Topic.new(topic) if topic
|
30
|
+
targets << Maia::Token.new(token) if token
|
58
31
|
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
body: body,
|
63
|
-
icon: icon,
|
64
|
-
sound: sound.to_s,
|
65
|
-
badge: badge,
|
66
|
-
color: color,
|
67
|
-
click_action: on_click
|
68
|
-
}.compact
|
69
|
-
end
|
32
|
+
Maia::Devices.new(models).each do |t|
|
33
|
+
targets << t
|
34
|
+
end
|
70
35
|
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
dry_run: dry_run?,
|
75
|
-
content_available: content_available?,
|
76
|
-
mutable_content: content_mutable?,
|
77
|
-
data: data,
|
78
|
-
notification: notification
|
79
|
-
}.compact
|
36
|
+
targets.map do |target|
|
37
|
+
messenger.deliver Maia.gateway.serialize(self, target)
|
38
|
+
end
|
80
39
|
end
|
81
40
|
end
|
82
41
|
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module Maia
|
2
|
+
module Messengers
|
3
|
+
class ActiveJob
|
4
|
+
def initialize(options = {})
|
5
|
+
@options = options
|
6
|
+
end
|
7
|
+
|
8
|
+
def deliver(payload)
|
9
|
+
MessengerJob.set(@options).perform_later payload
|
10
|
+
end
|
11
|
+
|
12
|
+
class MessengerJob < ::ActiveJob::Base
|
13
|
+
def perform(payload)
|
14
|
+
Maia::Messengers::Inline.new.deliver payload
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module Maia
|
2
|
+
module Messengers
|
3
|
+
class Array
|
4
|
+
include Enumerable
|
5
|
+
|
6
|
+
attr_reader :messages
|
7
|
+
|
8
|
+
def initialize
|
9
|
+
@messages = []
|
10
|
+
end
|
11
|
+
|
12
|
+
def deliver(payload)
|
13
|
+
@messages << payload
|
14
|
+
end
|
15
|
+
|
16
|
+
def each(&block)
|
17
|
+
@messages.map { |msg| JSON.parse(msg) }.each(&block)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module Maia
|
2
|
+
module Messengers
|
3
|
+
class Inline
|
4
|
+
def deliver(payload, gateway: Maia.gateway)
|
5
|
+
gateway.deliver payload
|
6
|
+
rescue Maia::Error::Unregistered => e
|
7
|
+
device = Maia::Device.find_by(token: e.token)
|
8
|
+
device.destroy
|
9
|
+
raise
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
data/lib/maia/token.rb
ADDED
data/lib/maia/topic.rb
ADDED
data/lib/maia/version.rb
CHANGED