maia 0.0.8 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- NzVlMDhkY2RiZjA5YjlhODlhZmFiODM0NmU4NDJhZWQxY2M4NGFhMA==
4
+ NjUwMmY4YjUwNzE4YmU2ZWZkNjUxNTA2MWUyZTllYTE2MTI3OTI4Nw==
5
5
  data.tar.gz: !binary |-
6
- NDVlNjBhZGY3MTBhYjI4MDJkYmM4NDIwN2IwMmFlM2M4ZGJjMjQyZA==
6
+ NGZlYzZmODgwYjAxNTQzOWU5ZDk5MWQ5NDJmNjU2ZjY4M2IyOWNkOA==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- YjI4MDhjMWM1ODgzMjA3MTAzMzU5ZTk4MDE2ZGQzZjViZTFmNjQyYTczZjk4
10
- NDlkMDY5MmM0MWJiNTdhMmY3ZDJhZDI2ZTZhZmIxNjcyYTI5YjA3OGZhMjk1
11
- OTZlODg0MGMyYjY5MGEyMGRjZTg2MGVhMTM4NDk5YzQ3YWEwMjI=
9
+ NjQ2ODJjNWY1NzkxMDZjY2I2MzMxMDhjYjZjM2JlMTBiZWI2M2NlN2ZjMDIx
10
+ ZWJmNGEwZTUwNGEzODU3ZmEzNDQ0NDBjYjNkYzYxNmE4NmI4MDJjMWVjZDQ4
11
+ MmU5ZWYxOTRmNjFlMzFiNzhkZDFmNzBmYTRmOTk3NDBmNTNhNmE=
12
12
  data.tar.gz: !binary |-
13
- NWViZTVlODljZjVjZDNiMjQzYWRmYjcyYWU5YzE4ZWJiNmQ5Nzk0MDAxNDg3
14
- NDFkYjJmOWRhOGVlMmEwMjdkMTY5YzIzZDc4YjQzY2Q5MmMyODkxODhhMGYx
15
- NzI1ODI5NDFmNGYyYzc3YjFkYjc1ODU4ZTY5ZmU1NGM4NGVmNGM=
13
+ N2JhMDJiMzYwNDQ3MWI1NWMyZGI1MTAyNWRjMzhjNjFiNDM0NmU4ODFkMjQz
14
+ ODliOTE0NmY0OTYzNTllMjhhYzlkYzBiZDZiY2RjYjY0ZTY3ZjMxNDc1MzBl
15
+ YWVkMGJmNGM2NTEzMzIyN2IzMjc3YTA1YzMxZTU2MTI1ODQ4Yzk=
@@ -42,7 +42,7 @@ module Maia
42
42
  end
43
43
 
44
44
  def permitted_params
45
- params.require(:device).permit :token
45
+ params.require(:device).permit :token, :platform
46
46
  end
47
47
  end
48
48
  end
@@ -3,6 +3,7 @@ module Maia
3
3
  belongs_to :pushable, polymorphic: true
4
4
 
5
5
  validates :token, presence: true, uniqueness: { scope: :pushable }
6
+ validates :platform, inclusion: { in: %w(ios android) }, allow_nil: true
6
7
 
7
8
  before_save :reset_token_expiry
8
9
 
@@ -19,13 +20,19 @@ module Maia
19
20
  end
20
21
 
21
22
  def self.owned_by(pushable)
22
- klass = pushable.klass.name
23
- table = pushable.table.name
24
- joins("JOIN #{table} ON #{table}.id = #{table_name}.pushable_id").
25
- where(pushable_type: klass).
26
- merge(pushable).
27
- uniq
23
+ where(pushable: pushable).uniq
28
24
  end
29
25
 
26
+ def self.ios
27
+ where platform: 'ios'
28
+ end
29
+
30
+ def self.android
31
+ where platform: 'android'
32
+ end
33
+
34
+ def self.unknown
35
+ where platform: nil
36
+ end
30
37
  end
31
38
  end
@@ -3,6 +3,7 @@ class CreateMaiaDevices < ActiveRecord::Migration
3
3
  create_table :maia_devices do |t|
4
4
  t.references :pushable, polymorphic: true, index: true
5
5
  t.string :token
6
+ t.string :platform
6
7
  t.datetime :token_expires_at
7
8
 
8
9
  t.timestamps null: false
data/lib/maia/dry_run.rb CHANGED
@@ -1,6 +1,10 @@
1
1
  module Maia
2
2
  class DryRun < Message
3
- def alert
3
+ def title
4
+ ''
5
+ end
6
+
7
+ def body
4
8
  ''
5
9
  end
6
10
 
data/lib/maia/message.rb CHANGED
@@ -3,43 +3,55 @@ module Maia
3
3
  MAX_TOKENS_AT_ONCE = 999
4
4
 
5
5
  def send_to(pushable, wait: false)
6
- devices =
7
- case pushable
8
- when ActiveRecord::Relation
9
- Device.owned_by pushable
10
- when ActiveRecord::Base
11
- pushable.devices
12
- else
13
- fail 'Maia can only send to ActiveRecord objects!'
14
- end
6
+ devices = Device.owned_by pushable
7
+ worker = wait ? Maia::Messenger.set(wait: wait) : Maia::Messenger
15
8
 
16
- enqueue devices, to_h, wait: wait
9
+ enqueue worker, devices.android, to_h(notify: notify?(:android))
10
+ enqueue worker, devices.ios, to_h(notify: notify?(:ios))
11
+ enqueue worker, devices.unknown, to_h(notify: notify?(:unknown))
17
12
  end
18
13
 
19
- def enqueue(devices, payload, wait: false)
20
- worker = wait ? Maia::Messenger.set(wait: wait) : Maia::Messenger
21
-
22
- devices.pluck(:token).each_slice(MAX_TOKENS_AT_ONCE) do |tokens|
14
+ def enqueue(worker, devices, payload)
15
+ devices.pluck(:token).each_slice MAX_TOKENS_AT_ONCE do |tokens|
23
16
  worker.perform_later tokens, payload.deep_stringify_keys
24
17
  end
25
18
  end
26
19
 
27
- def alert
28
- ''
20
+ def title
29
21
  end
30
22
 
31
- def badge
23
+ def body
24
+ end
25
+
26
+ def icon
32
27
  end
33
28
 
34
29
  def sound
35
30
  'default'
36
31
  end
37
32
 
38
- def other
39
- {}
33
+ def badge
34
+ end
35
+
36
+ def color
37
+ end
38
+
39
+ def action
40
+ end
41
+
42
+ def title_i18n
43
+ []
44
+ end
45
+
46
+ def body_i18n
47
+ []
48
+ end
49
+
50
+ def data
40
51
  end
41
52
 
42
53
  def priority
54
+ :normal
43
55
  end
44
56
 
45
57
  def content_available?
@@ -50,21 +62,60 @@ module Maia
50
62
  false
51
63
  end
52
64
 
53
- def to_h
54
- hash = {
55
- data: other,
56
- notification: {
57
- title: alert,
58
- body: alert,
59
- sound: sound,
60
- badge: badge
61
- }.compact
65
+ def notify?(_platform = :unknown)
66
+ true
67
+ end
68
+
69
+ def notification
70
+ {
71
+ title: title,
72
+ body: body,
73
+ icon: icon,
74
+ sound: sound,
75
+ badge: badge,
76
+ color: color,
77
+ click_action: action
78
+ }.merge(i18n).compact
79
+ end
80
+
81
+ def to_h(notify: true)
82
+ payload = {
83
+ priority: priority.to_s,
84
+ dry_run: dry_run?,
85
+ content_available: content_available?,
86
+ data: data
62
87
  }
63
88
 
64
- hash.merge!(priority: priority.to_s) if priority
65
- hash.merge!(dry_run: true) if dry_run?
66
- hash.merge!(content_available: true) if content_available?
67
- hash
89
+ payload[:notification] = notification if notify
90
+ payload.compact
68
91
  end
92
+
93
+ private
94
+ def i18n
95
+ {}.tap do |hash|
96
+ hash[:body_loc_key] = body_i18n.first
97
+ hash[:body_loc_args] = body_i18n_args
98
+ hash[:title_loc_key] = title_i18n_key
99
+ hash[:title_loc_args] = title_i18n_args
100
+ end.compact
101
+ end
102
+
103
+ def body_i18n_key
104
+ body_i18n.first
105
+ end
106
+
107
+ def body_i18n_args
108
+ args = body_i18n.drop 1
109
+ args if args.any?
110
+ end
111
+
112
+ def title_i18n_key
113
+ title_i18n.first
114
+ end
115
+
116
+ def title_i18n_args
117
+ args = title_i18n.drop 1
118
+ args if args.any?
119
+ end
69
120
  end
70
121
  end
data/lib/maia/poke.rb CHANGED
@@ -1,6 +1,10 @@
1
1
  module Maia
2
2
  class Poke < Message
3
- def alert
3
+ def title
4
+ 'Poke'
5
+ end
6
+
7
+ def body
4
8
  'Poke'
5
9
  end
6
10
  end
data/lib/maia/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Maia
2
- VERSION = '0.0.8'
2
+ VERSION = '1.0.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: maia
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.8
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Logan Serman
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-01-29 00:00:00.000000000 Z
11
+ date: 2016-05-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails