maia 3.0.0 → 5.0.0

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.
@@ -1,26 +1,28 @@
1
1
  module Maia
2
2
  module FCM
3
3
  class Notification
4
- attr_accessor :attributes
5
-
6
- def initialize(attributes = {})
7
- @attributes = attributes
4
+ def initialize(message)
5
+ @message = message
8
6
  end
9
7
 
10
- def to_h
11
- @attributes
8
+ def title
9
+ @message.title
12
10
  end
13
11
 
14
- def ==(other)
15
- attributes == other.attributes
12
+ def body
13
+ @message.body
16
14
  end
17
15
 
18
- def method_missing(method_name, *args, &block)
19
- @attributes.fetch(method_name) { super }
16
+ def image
17
+ @message.image
20
18
  end
21
19
 
22
- def respond_to_missing?(method_name)
23
- @attributes.include? method_name
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
@@ -1,15 +1,16 @@
1
1
  module Maia
2
2
  module FCM
3
3
  class Response
4
- attr_reader :http_response, :tokens
4
+ def initialize(response)
5
+ @response = response
6
+ end
5
7
 
6
- def initialize(http_response, tokens = [])
7
- @http_response = http_response
8
- @tokens = tokens
8
+ def body
9
+ @response.body
9
10
  end
10
11
 
11
12
  def status
12
- http_response.code
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 400
36
- 'Invalid JSON was sent to FCM.'
37
- when 401
38
- 'Authentication error with FCM. Check the server whitelist and the validity of your project key.'
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
- def retry_after
45
- http_response.headers['Retry-After']
46
- end
47
-
48
- def to_h
49
- JSON.parse http_response.body
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 send_to(pushable, job_options = {})
4
- devices = Device.owned_by pushable
5
- worker = Messenger.set job_options
6
-
7
- enqueue worker, devices.android
8
- enqueue worker, devices.ios
9
- enqueue worker, devices.unknown
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 priority
44
- :normal
15
+ def sound
16
+ 'default'
45
17
  end
46
18
 
47
- def content_available?
48
- false
19
+ def targeting(target)
20
+ tap { @target = target }
49
21
  end
50
22
 
51
- def content_mutable?
52
- false
23
+ def to_json
24
+ to_h.to_json
53
25
  end
54
26
 
55
- def dry_run?
56
- false
57
- end
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
- def notification
60
- {
61
- title: title,
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
- def to_h
72
- {
73
- priority: priority.to_s,
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
@@ -0,0 +1,21 @@
1
+ module Maia
2
+ class Token
3
+ include Enumerable
4
+
5
+ def initialize(token)
6
+ @token = token
7
+ end
8
+
9
+ def each(&block)
10
+ [self].each(&block)
11
+ end
12
+
13
+ def to_s
14
+ @token
15
+ end
16
+
17
+ def to_h
18
+ { token: @token }
19
+ end
20
+ end
21
+ end
data/lib/maia/topic.rb ADDED
@@ -0,0 +1,21 @@
1
+ module Maia
2
+ class Topic
3
+ include Enumerable
4
+
5
+ def initialize(topic)
6
+ @topic = topic
7
+ end
8
+
9
+ def each(&block)
10
+ [self].each(&block)
11
+ end
12
+
13
+ def to_s
14
+ @topic
15
+ end
16
+
17
+ def to_h
18
+ { topic: @topic }
19
+ end
20
+ end
21
+ end
data/lib/maia/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Maia
2
- VERSION = '3.0.0'.freeze
2
+ VERSION = '5.0.0'.freeze
3
3
  end