hey-you-fcm-push 0.1.2 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5106b508a9c987afbfb4597e104b975ce8cda559d255beb39955302352e76fa8
4
- data.tar.gz: '085dc0e137957caa88c8e1678321b6f4307f6e7a4f3c07737764c7b496edd8d0'
3
+ metadata.gz: 9bab78f013de7f0d692033399b68f54e247a4419222ea6f588cf985dc5f295df
4
+ data.tar.gz: 481bdaa3790aa6a3ca84eb70d465967712939b3fc21512b99544a757e3ac14b0
5
5
  SHA512:
6
- metadata.gz: 0dab0074971c66c4b6b69778e5f6488b34481ebbfb7b98099cc7fc4af77e91fa7a6dc9f42103d5896d7b92efca67ad0e4d9edafa5ee12fc84ad49cadcf119ed1
7
- data.tar.gz: 1bdc0ecddf136907f145334ce58095dac3134ef486472e4ae55c34af26fdf44d830c613abf2c9e24ef2218266b6e517f2b8a7e9002488fb497e7e7018ddaa857
6
+ metadata.gz: 5451884e942afbf123af82cf6734ba8d7c4a34b461a9934bd903c72ea371a8a60cb5fd87476695be6ed29f312274ff9282f08550b19fdd1543b255fa1bb9a47b
7
+ data.tar.gz: ed7db9bf947e41d1fe84dc1f75a767c0cc857a46e8b6e37445fcd58d05d7e4c7304ac24672c26aa9275c482b05308608485f23388fd2b3081a2fde7af610516f
data/bin/console CHANGED
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  require "bundler/setup"
4
- require "hey_you_fcm_push"
4
+ require "hey-you-fcm-push"
5
5
 
6
6
  # You can add fixtures and/or initialization code here to make experimenting
7
7
  # with your gem easier. You can also use a different console, if you like.
@@ -26,9 +26,10 @@ Gem::Specification.new do |spec|
26
26
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
27
27
  spec.require_paths = ["lib"]
28
28
 
29
- spec.add_runtime_dependency "hey-you", '>= 1.4'
29
+ spec.add_runtime_dependency "hey-you", '>= 1.4.2'
30
30
  spec.add_runtime_dependency "googleauth", '~> 0.14'
31
31
  spec.add_runtime_dependency "httparty", '~> 0.18'
32
+ spec.add_runtime_dependency "activesupport", '>= 5'
32
33
 
33
34
  spec.add_development_dependency "rake", "~> 13.0"
34
35
  spec.add_development_dependency "rspec", "~> 3.0"
@@ -1,4 +1,5 @@
1
1
  require 'hey_you_fcm_push/message_object'
2
+ require 'active_support/core_ext/object/deep_dup'
2
3
 
3
4
  module HeyYou
4
5
  class Builder
@@ -12,15 +13,17 @@ module HeyYou
12
13
  @condition = ch_data.fetch('condition', nil)
13
14
  @name = ch_data.fetch('name', nil)
14
15
 
15
- @notification = interpolate_notification(ch_data.fetch('notification', nil))
16
+ @notification = interpolate_notification(ch_data.fetch('notification', nil)&.deep_dup)
16
17
 
17
- @android = interpolate_notification(ch_data.fetch('android', nil))
18
- @webpush = interpolate_notification(ch_data.fetch('webpush', nil))
19
- @apns = interpolate_notification(ch_data.fetch('apns', nil))
20
- @fcm_options = interpolate_notification(ch_data.fetch('fcm_options', nil))
21
- @push_data = ch_data.fetch('push_data', nil)
18
+ @android = interpolate_notification(ch_data.fetch('android', nil)&.deep_dup)
19
+ @webpush = interpolate_notification(ch_data.fetch('webpush', nil)&.deep_dup)
20
+ @apns = interpolate_notification(ch_data.fetch('apns', nil)&.deep_dup)
21
+ @fcm_options = interpolate_notification(ch_data.fetch('fcm_options', nil)&.deep_dup)
22
+ @push_data = ch_data.fetch('push_data', nil)&.deep_dup
22
23
  end
23
24
 
25
+ private
26
+
24
27
  def interpolate_notification(data)
25
28
  return unless data
26
29
 
@@ -10,6 +10,7 @@ module HeyYou
10
10
  # @param [HeyYou::Builder] builder - builder with notifications texts and settings
11
11
  # @option [String] name - Output Only. The identifier of the message sent
12
12
  # @option [String] token - Registration token to send a message to.
13
+ # @option [String] to - Alias for `token`. `token` in high priority.
13
14
  # @option [String] topic - Topic name to send a message to, e.g. "weather".
14
15
  # @option [String] condition - Condition to send a message to, e.g. "'foo' in topics && 'bar' in topics".
15
16
  # @option [Hash] notification - Basic notification template to use across all platforms.
@@ -19,12 +20,24 @@ module HeyYou
19
20
  # @option [Hash] fcm_options - Platform independent options for features provided by the FCM SDKs.
20
21
  # @option [Hash] push_data - Input only. Arbitrary key/value payload. The key should not be a reserved word ("from", "message_type", or any word starting with "google" or "gcm").
21
22
  #
22
- # @return [Hash] - FCM response
23
+ # @return [Array{Hash}] - FCM responses
23
24
  #
24
25
  # @see: https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages#resource:-message
25
26
  def send!(builder, **options)
26
- message = build_message(builder, **options)
27
- HeyYouFcmPush::Connection.instance.send_notification(message, validate_only: options[:validate_only])
27
+ options[:token] ||= options[:to]
28
+
29
+ if options[:token].is_a?(Array)
30
+ messages = options[:token].map { |token| build_message(builder, **options.merge(token: token)) }
31
+ end
32
+
33
+ messages ||= [build_message(builder, **options)]
34
+
35
+ messages.map do |message|
36
+ {
37
+ receiver: message.receiver_hash,
38
+ response: HeyYouFcmPush::Connection.instance.send_notification(message.to_h, validate_only: options[:validate_only])
39
+ }
40
+ end
28
41
  end
29
42
 
30
43
  private
@@ -41,7 +54,7 @@ module HeyYou
41
54
  apns: options[:apns] || builder.fcm_push.apns,
42
55
  fcm_options: options[:fcm_options]|| builder.fcm_push.fcm_options,
43
56
  data: options[:push_data] || builder.fcm_push.push_data
44
- ).to_h
57
+ )
45
58
  end
46
59
  end
47
60
  end
@@ -6,11 +6,11 @@ class HeyYou::Config::FcmPush
6
6
  REQUIRED_CONFIGS = %i[credentials_file project_id]
7
7
 
8
8
  def credentials_file=(filepath)
9
- @credentials_file = filepath
10
- ENV[Google::Auth::CredentialsLoader::ENV_VAR] = credentials_file
9
+ ENV[Google::Auth::CredentialsLoader::ENV_VAR] = filepath
11
10
  end
12
11
 
13
12
  def validate_config
13
+ @credentials_file = ENV[Google::Auth::CredentialsLoader::ENV_VAR]
14
14
  return if REQUIRED_CONFIGS.all? { |conf| send(conf) }
15
15
 
16
16
  raise(
@@ -10,7 +10,7 @@ module HeyYouFcmPush
10
10
  RETRY_COUNT = 1
11
11
  HTTP_CODES = {
12
12
  not_auth: 401,
13
- success: 200
13
+ server_error: 500
14
14
  }
15
15
 
16
16
  attr_reader :authorization, :options, :project_id, :uri
@@ -44,13 +44,15 @@ module HeyYouFcmPush
44
44
  private
45
45
 
46
46
  def make_request(request_options)
47
+ HeyYou::Config.instance.logger&.debug("Send request to #{uri} with options #{request_options}")
47
48
  response = HTTParty.post(uri, request_options)
49
+ HeyYou::Config.instance.logger&.debug("Response: #{response}")
48
50
  process_response(response)
49
51
  end
50
52
 
51
53
  def process_response(response)
52
54
  raise AuthError if response.code == HTTP_CODES[:not_auth]
53
- return response.to_hash if response.code == HTTP_CODES[:success]
55
+ return response.to_hash if response.code <= HTTP_CODES[:server_error]
54
56
 
55
57
  raise ResponseError, "response code: #{response.code}"
56
58
  end
@@ -34,7 +34,7 @@ module HeyYouFcmPush
34
34
  @web_push_config = WebPushConfig.new(options[:webpush].transform_keys(&:to_sym)) if options[:web_push_config]
35
35
  @apns_config = ApnsConfig.new(options[:apns].transform_keys(&:to_sym)) if options[:apns_config]
36
36
  @fcm_options = FcmOptions.new(**options[:fcm_options].transform_keys(&:to_sym)) if options[:fcm_options]
37
- @push_data = options[:data]
37
+ @push_data = { data: options[:data].to_json }
38
38
  end
39
39
 
40
40
  def to_h
@@ -1,3 +1,3 @@
1
1
  module HeyYouFcmPush
2
- VERSION = "0.1.2"
2
+ VERSION = "0.3.2"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hey-you-fcm-push
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sergey Nesterov
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-12-26 00:00:00.000000000 Z
11
+ date: 2021-02-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: hey-you
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '1.4'
19
+ version: 1.4.2
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: '1.4'
26
+ version: 1.4.2
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: googleauth
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0.18'
55
+ - !ruby/object:Gem::Dependency
56
+ name: activesupport
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '5'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '5'
55
69
  - !ruby/object:Gem::Dependency
56
70
  name: rake
57
71
  requirement: !ruby/object:Gem::Requirement