jera_push 1.2.3 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (69) hide show
  1. checksums.yaml +4 -4
  2. data/Rakefile +12 -18
  3. data/app/controllers/jera_push/admin/messages_controller.rb +23 -10
  4. data/app/presenters/jera_push/message_presenter.rb +22 -18
  5. data/app/views/jera_push/admin/messages/show.html.erb +7 -4
  6. data/config/locale/jera_push.pt-BR.yml +7 -0
  7. data/config/routes.rb +1 -0
  8. data/lib/generators/active_record/templates/create_jera_push_devices.rb +2 -1
  9. data/lib/generators/active_record/templates/create_jera_push_messages.rb +6 -3
  10. data/lib/generators/active_record/templates/create_jera_push_messages_devices.rb +3 -3
  11. data/lib/generators/jera_push/templates/jera_push.rb +2 -1
  12. data/lib/jera_push/firebase/client.rb +56 -59
  13. data/lib/jera_push/models/device.rb +3 -31
  14. data/lib/jera_push/models/message.rb +2 -142
  15. data/lib/jera_push/models/message_device.rb +1 -0
  16. data/lib/jera_push/services/base_service.rb +23 -0
  17. data/lib/jera_push/services/send_message.rb +23 -8
  18. data/lib/jera_push/services/send_push_service.rb +99 -0
  19. data/lib/jera_push/services/send_to_device_service.rb +52 -0
  20. data/lib/jera_push/services/send_to_devices_service.rb +54 -0
  21. data/lib/jera_push/services/send_to_everyone_service.rb +40 -0
  22. data/lib/jera_push/services/send_to_topic_service.rb +37 -0
  23. data/lib/jera_push/services/topic_service.rb +18 -0
  24. data/lib/jera_push/version.rb +1 -1
  25. data/lib/jera_push.rb +19 -5
  26. metadata +71 -116
  27. data/test/dummy/README.rdoc +0 -28
  28. data/test/dummy/Rakefile +0 -6
  29. data/test/dummy/app/assets/javascripts/application.js +0 -13
  30. data/test/dummy/app/assets/stylesheets/application.css +0 -15
  31. data/test/dummy/app/controllers/application_controller.rb +0 -5
  32. data/test/dummy/app/helpers/application_helper.rb +0 -2
  33. data/test/dummy/app/models/user.rb +0 -3
  34. data/test/dummy/app/views/layouts/application.html.erb +0 -14
  35. data/test/dummy/bin/bundle +0 -3
  36. data/test/dummy/bin/rails +0 -4
  37. data/test/dummy/bin/rake +0 -4
  38. data/test/dummy/bin/setup +0 -29
  39. data/test/dummy/config/application.rb +0 -25
  40. data/test/dummy/config/boot.rb +0 -5
  41. data/test/dummy/config/database.yml +0 -16
  42. data/test/dummy/config/environment.rb +0 -5
  43. data/test/dummy/config/environments/development.rb +0 -41
  44. data/test/dummy/config/environments/production.rb +0 -79
  45. data/test/dummy/config/environments/test.rb +0 -42
  46. data/test/dummy/config/initializers/assets.rb +0 -12
  47. data/test/dummy/config/initializers/backtrace_silencers.rb +0 -7
  48. data/test/dummy/config/initializers/cookies_serializer.rb +0 -3
  49. data/test/dummy/config/initializers/filter_parameter_logging.rb +0 -4
  50. data/test/dummy/config/initializers/inflections.rb +0 -16
  51. data/test/dummy/config/initializers/mime_types.rb +0 -4
  52. data/test/dummy/config/initializers/session_store.rb +0 -3
  53. data/test/dummy/config/initializers/wrap_parameters.rb +0 -14
  54. data/test/dummy/config/locales/en.yml +0 -23
  55. data/test/dummy/config/locales/pt-BR.yml +0 -23
  56. data/test/dummy/config/routes.rb +0 -57
  57. data/test/dummy/config/secrets.yml +0 -22
  58. data/test/dummy/config.ru +0 -4
  59. data/test/dummy/db/migrate/20161005121035_create_users.rb +0 -10
  60. data/test/dummy/db/schema.rb +0 -61
  61. data/test/dummy/db/seeds.rb +0 -70
  62. data/test/dummy/public/404.html +0 -67
  63. data/test/dummy/public/422.html +0 -67
  64. data/test/dummy/public/500.html +0 -66
  65. data/test/dummy/public/favicon.ico +0 -0
  66. data/test/dummy/test/fixtures/users.yml +0 -7
  67. data/test/dummy/test/models/user_test.rb +0 -7
  68. data/test/jera_push_test.rb +0 -7
  69. data/test/test_helper.rb +0 -20
@@ -1,22 +1,29 @@
1
1
  module JeraPush
2
- module Service
2
+ module Services
3
3
  class SendMessage
4
4
 
5
5
  def initialize(*args)
6
- args[0].map { |attr_name, value| instance_variable_set("@#{attr_name}", value) }
6
+ @type = args.first[:type].to_sym
7
+ @message = args.first[:message]
8
+ @devices = args.first[:devices]
7
9
  end
8
10
 
9
11
  def call
10
12
  return false unless valid?
11
13
 
12
- message_content = JeraPush::Message.format_hash @message
13
-
14
- case @type.to_sym
14
+ message_content = format_hash(@message)
15
+ case @type
15
16
  when :broadcast
16
- JeraPush::Message.send_broadcast(content: message_content)
17
+ # Topicos não estão funcionando.
18
+ # JeraPush::Message.send_broadcast(content: message_content)
17
19
  when :specific
18
20
  target_devices = JeraPush::Device.where(id: @devices.uniq.map(&:to_i))
19
- JeraPush::Message.send_to(target_devices, content: message_content)
21
+ result = JeraPush::Services::SendToDevicesService.new(devices: target_devices,
22
+ title: message_content[:title],
23
+ body: message_content[:body],
24
+ data: message_content
25
+ ).call
26
+ result
20
27
  end
21
28
  end
22
29
 
@@ -25,6 +32,14 @@ module JeraPush
25
32
  @type.to_sym == :broadcast ? valid : valid && @devices.present?
26
33
  end
27
34
 
35
+ def format_hash(messages = [])
36
+ return false if messages.blank?
37
+ messages.collect do |obj|
38
+ hash = { obj[:key].to_sym => obj[:value] }
39
+ hash.delete_if { |key, value| key.blank? || value.blank? }
40
+ end.reduce(:merge)
41
+ end
42
+
28
43
  end
29
44
  end
30
- end
45
+ end
@@ -0,0 +1,99 @@
1
+ module JeraPush::Services
2
+ class SendPushService < JeraPush::Services::BaseService
3
+ def initialize(device:, message:, message_device:, android_config: {}, ios_config: {})
4
+ super
5
+ @device = device
6
+ @message = message
7
+ @message_device = message_device
8
+ @android_config = android_config
9
+ @ios_config = ios_config
10
+ end
11
+
12
+ def call
13
+ return @errors if @errors.present?
14
+
15
+ send_push
16
+
17
+ @message
18
+ end
19
+
20
+ private
21
+
22
+ def send_push
23
+ @response = @firebase.send_to_device(message: message_params(@device))
24
+ @message_device.update(status: :success)
25
+ @message.update(success_count: @message.success_count + 1)
26
+ rescue Google::Apis::AuthorizationError => e
27
+ message = JSON.parse(e.body)
28
+ @message_device.update(status: :error, error_message: message.to_json)
29
+ @message.update(failure_count: @message.failure_count + 1)
30
+ rescue Google::Apis::ClientError => e
31
+ response = JSON.parse(e.body, symbolize_names: true)
32
+ error_code = response[:error][:details][0][:errorCode]
33
+ @message_device.update(status: :error, error_message: response.to_json)
34
+ @message.update(failure_count: @message.failure_count + 1)
35
+ end
36
+
37
+ # Documentação com o atributos que podem ser usados em { message: }
38
+ # https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages?hl=pt-br
39
+ def message_params(device)
40
+ case device.platform.to_sym
41
+ when :android
42
+ android_params(device.token)
43
+ when :ios
44
+ ios_params(device.token)
45
+ else
46
+ {}
47
+ end
48
+ end
49
+
50
+ def android_params(token)
51
+ {
52
+ message: {
53
+ token: token,
54
+ notification: notification,
55
+ data: @message.data,
56
+ android: android_configs
57
+ },
58
+ validate_only: false
59
+ }
60
+ end
61
+
62
+ def ios_params(token)
63
+ {
64
+ message: {
65
+ token: token,
66
+ notification: notification,
67
+ data: @message.data,
68
+ apns: apns_params
69
+ },
70
+ validate_only: false
71
+ }
72
+ end
73
+
74
+ def notification
75
+ { title: @message.title, body: @message.body }
76
+ end
77
+
78
+ def apns_params
79
+ @ios_config.empty? ? default_apns_params : @ios_config
80
+ end
81
+
82
+ def android_configs
83
+ @android_config.merge({ priority: 'high'})
84
+ end
85
+
86
+ def default_apns_params
87
+ {
88
+ headers: {
89
+ 'apns-priority': '5'
90
+ },
91
+ payload: {
92
+ aps: {
93
+ 'content-available': 1
94
+ }
95
+ }
96
+ }
97
+ end
98
+ end
99
+ end
@@ -0,0 +1,52 @@
1
+ module JeraPush::Services
2
+ class SendToDeviceService < JeraPush::Services::BaseService
3
+ def initialize(device:, title:, body:, data:, android: {}, ios: {})
4
+ super
5
+ @device = device
6
+ @title = title
7
+ @body = body
8
+ @data = data
9
+ @android_config = android
10
+ @ios_config = ios
11
+ end
12
+
13
+ def call
14
+ validate_device
15
+
16
+ return @errors if @errors.present?
17
+
18
+ create_message
19
+ add_devices_to_message
20
+ send_push
21
+
22
+ @message
23
+ end
24
+
25
+ private
26
+
27
+ def validate_device
28
+ @errors.add(:base, message: I18n.t('jera_push.services.errors.not_found.device')) if @device.blank?
29
+ end
30
+
31
+ def create_message
32
+ @message = JeraPush::Message.create(title: @title, body: @body, data: data_params)
33
+ end
34
+
35
+ def add_devices_to_message
36
+ @message_device = @message.message_devices.create(device: @device)
37
+ end
38
+
39
+ def send_push
40
+ JeraPush::Services::SendPushService.new(device: @device,
41
+ message: @message,
42
+ message_device: @message_device,
43
+ ios_config: @ios_config,
44
+ android_config: @android_config
45
+ ).call
46
+ end
47
+
48
+ def data_params
49
+ @data.merge({ title: @title, body: @body })
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,54 @@
1
+ module JeraPush::Services
2
+ class SendToDevicesService < JeraPush::Services::BaseService
3
+ def initialize(devices:, title:, body:, data:, android: {}, ios: {})
4
+ super
5
+ @devices = devices
6
+ @title = title
7
+ @body = body
8
+ @data = data
9
+ @android_config = android
10
+ @ios_config = ios
11
+ end
12
+
13
+ def call
14
+ validate_device
15
+
16
+ return @errors if @errors.present?
17
+
18
+ create_message
19
+ send_push_to_devices
20
+
21
+ @message
22
+ end
23
+
24
+ private
25
+
26
+ def validate_device
27
+ @errors.add(:base, message: I18n.t('jera_push.services.errors.not_found.device')) if @devices.empty?
28
+ end
29
+
30
+ def create_message
31
+ @message = JeraPush::Message.create(title: @title, body: @body, data: data_params)
32
+ end
33
+
34
+ def send_push_to_devices
35
+ @devices.each do |device|
36
+ message_device = @message.message_devices.create(device: device)
37
+ send_push(device, message_device)
38
+ end
39
+ end
40
+
41
+ def send_push(device, message_device)
42
+ JeraPush::Services::SendPushService.new(device: device,
43
+ message: @message,
44
+ message_device: message_device,
45
+ ios_config: @ios_config,
46
+ android_config: @android_config
47
+ ).call
48
+ end
49
+
50
+ def data_params
51
+ @data.merge({ title: @title, body: @body })
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,40 @@
1
+ module JeraPush::Services
2
+ class SendToEveryoneService < JeraPush::Services::BaseService
3
+ def initialize(title:, body:)
4
+ super
5
+ @title = title
6
+ @body = body
7
+ end
8
+
9
+ def call
10
+ create_message
11
+ send_android
12
+ send_ios
13
+ update_message
14
+
15
+ @message
16
+ end
17
+
18
+ private
19
+
20
+ def create_message
21
+ @message = JeraPush::Message.create(title: @title, body: @body, kind: :everyone)
22
+ end
23
+
24
+ def send_android
25
+ @response_android = @firebase.send_message_to_topic(message: @message, topic: JeraPush.send('topic_android'))
26
+ end
27
+
28
+ def send_ios
29
+ @response_ios = @firebase.send_message_to_topic(message: @message, topic: JeraPush.send('topic_ios'))
30
+ end
31
+
32
+ def update_message
33
+ @message.update(
34
+ success_count: @response_android["success"] + @response_ios["success"],
35
+ failure_count: @response_android["failure"] + @response_ios["failure"]
36
+ )
37
+ end
38
+
39
+ end
40
+ end
@@ -0,0 +1,37 @@
1
+ module JeraPush::Services
2
+ class SendToTopicService < JeraPush::Services::BaseService
3
+ def initialize(title:, body:, topic:)
4
+ super
5
+ @title = title
6
+ @body = body
7
+ @topic = topic
8
+ end
9
+
10
+ def call
11
+ create_message
12
+ send_topic
13
+ update_message
14
+
15
+ @message
16
+ end
17
+
18
+ private
19
+
20
+ def create_message
21
+ @message = JeraPush::Message.create(title: @title, body: @body, kind: :topic)
22
+ end
23
+
24
+ def send_topic
25
+ @response = @firebase.send_message_to_topic(message: @message, topic: @topic)
26
+ end
27
+
28
+ def update_message
29
+ @message.update(
30
+ multicast_id: @response["multicast_id"],
31
+ success_count: @response["success"],
32
+ failure_count: @response["failure"]
33
+ )
34
+ end
35
+
36
+ end
37
+ end
@@ -0,0 +1,18 @@
1
+ module JeraPush::Services
2
+ class TopicService < JeraPush::Services::BaseService
3
+ DEFAULT_TOPIC = 'general'
4
+
5
+ def initialize(*)
6
+ super
7
+ end
8
+
9
+ def subscribe(device:, topic: DEFAULT_TOPIC)
10
+ @firebase.add_device_to_topic(device: device, topic: topic)
11
+ end
12
+
13
+ def unsubscribe(device:, topic: DEFAULT_TOPIC)
14
+ @firebase.remove_device_from_topic(devices: [device], topic: topic)
15
+ end
16
+
17
+ end
18
+ end
@@ -1,3 +1,3 @@
1
1
  module JeraPush
2
- VERSION = "1.2.3"
2
+ VERSION = "2.0.0"
3
3
  end
data/lib/jera_push.rb CHANGED
@@ -5,10 +5,19 @@ require 'kaminari'
5
5
  require 'jera_push/engine'
6
6
  require 'jera_push/firebase/client'
7
7
  require 'jera_push/firebase/api_result'
8
+ require 'googleauth'
9
+ require 'google/apis/fcm_v1'
10
+
11
+ require 'jera_push/services/base_service'
8
12
  require 'jera_push/services/send_message'
13
+ require 'jera_push/services/send_push_service'
14
+ require 'jera_push/services/send_to_device_service'
15
+ require 'jera_push/services/send_to_devices_service'
16
+ require 'jera_push/services/send_to_everyone_service'
17
+ require 'jera_push/services/send_to_topic_service'
18
+ require 'jera_push/services/topic_service'
9
19
 
10
20
  module JeraPush
11
-
12
21
  autoload :Device, 'jera_push/models/device.rb'
13
22
  autoload :Message, 'jera_push/models/message.rb'
14
23
  autoload :MessageDevice, 'jera_push/models/message_device.rb'
@@ -17,11 +26,20 @@ module JeraPush
17
26
  mattr_accessor :firebase_api_key
18
27
  @@firebase_api_key = nil
19
28
 
29
+ mattr_accessor :project_name
30
+ @@project_name = nil
31
+
32
+ mattr_accessor :project_id
33
+ @@project_id = nil
34
+
20
35
  mattr_accessor :default_topic
21
36
  @@default_topic = 'jera_push_development'
22
37
 
23
38
  mattr_accessor :resources_name
24
39
  @@resources_name = nil
40
+
41
+ mattr_accessor :credentials_path
42
+ @@credentials_path = nil
25
43
 
26
44
  mattr_accessor :resource_attributes
27
45
  @@resource_attributes = []
@@ -29,7 +47,6 @@ module JeraPush
29
47
  mattr_accessor :admin_login
30
48
  @@resources_name = { username: 'jera_push', password: 'JeraPushAdmin' }
31
49
 
32
-
33
50
  def self.setup
34
51
  yield self
35
52
  end
@@ -42,7 +59,4 @@ module JeraPush
42
59
  @@ios_topic ||= "#{@@default_topic}_ios"
43
60
  end
44
61
 
45
- def self.topic_chrome
46
- @@chrome_topic ||= "#{@@default_topic}_chrome"
47
- end
48
62
  end