fcmpush 1.0.0 → 1.3.0

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
- SHA1:
3
- metadata.gz: 327bff9a6b09181d0051f314ba3fc9216c71ffc8
4
- data.tar.gz: b2e7fcea6ba6f0af379309d6071faca78d20f482
2
+ SHA256:
3
+ metadata.gz: aec3e5ccc93c7aa90c5a47c29c86666f97a4c64b6c4699b2343b954c6167d4f9
4
+ data.tar.gz: 30ca2b1ebae21c4d5b1719a74bf7b53c19caaf35c0af8cd9407a8a8fde5d174d
5
5
  SHA512:
6
- metadata.gz: 828348d829eb7f3e18f351bbc33937ab4b5d7a767376ae27f6e2b46e2b0b38d9fa40d19323157f8ef1453f1f5b04ddda6f8c4bfe7025b6bf537f38452b85ae8c
7
- data.tar.gz: c2801903d5651c554cb11e076cd3d0c71088f88be0671d2d06f2c6b5f6b9c6aa7b2f4a544cbcc7e90c3ab5bbe0fc0b5bb3f3bc423954a7855ebddd4cf966987f
6
+ metadata.gz: 35aa9a63da743fd31d7ef83b24846cd2dbaf69f0a35edccfd27aa5bfddaebe0da2e4e95e7e0600a799ab220f1953ef8c18fcd92765a5bebb8bc0abc58ec684b3
7
+ data.tar.gz: f367ae2d0a2807a22d6efc6e5241540c8f05e894ad47dcd58735809f45d07f5c347eb571e86dcd33b992e5aa801f9004f4d440cfd377735b8de6c750a75c9ce5
@@ -22,12 +22,12 @@ Gem::Specification.new do |spec|
22
22
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
23
  spec.require_paths = ['lib']
24
24
 
25
- spec.required_ruby_version = '>= 2.3', '< 2.8'
25
+ spec.required_ruby_version = '>= 2.4', '<= 3.0'
26
26
 
27
27
  spec.add_dependency 'googleauth', '>= 0.9.0'
28
- spec.add_dependency 'net-http-persistent', '>= 3.1.0'
28
+ spec.add_dependency 'net-http-persistent', '~> 4.0.1'
29
29
 
30
30
  spec.add_development_dependency 'bundler', '~> 2.0'
31
- spec.add_development_dependency 'rake', '~> 10.0'
31
+ spec.add_development_dependency 'rake', '~> 13'
32
32
  spec.add_development_dependency 'rspec', '~> 3.0'
33
33
  end
@@ -0,0 +1,37 @@
1
+ module Fcmpush
2
+ module Batch
3
+ PART_BOUNDRY = '__END_OF_PART__'.freeze
4
+
5
+ def make_batch_payload(messages, headers)
6
+ uri = URI.join(domain, path)
7
+ subrequests = messages.map do |payload|
8
+ req = Net::HTTP::Post.new(uri, headers)
9
+ req.body = payload
10
+ req
11
+ end
12
+ subrequests.map.with_index { |req, idx| create_part(req, PART_BOUNDRY, idx) }.join + "--#{PART_BOUNDRY}\r\n"
13
+ end
14
+
15
+ def create_part(request, part_boundry, idx)
16
+ serialized_request = serialize_sub_request(request)
17
+ "--#{part_boundry}\r\n" \
18
+ "Content-Length: #{serialized_request.length}\r\n" \
19
+ "Content-Type: application/http\r\n" \
20
+ "Content-Id: #{idx + 1}\r\n" \
21
+ "Content-Transfer-Encoding: binary\r\n" \
22
+ "\r\n" \
23
+ "#{serialized_request}\r\n"
24
+ end
25
+
26
+ def serialize_sub_request(request)
27
+ body_str = request.body.is_a?(String) ? request.body : request.body.to_json
28
+ subreqest = "POST #{request.path} HTTP/1.1\r\n" \
29
+ "Content-Length: #{body_str.length}\r\n"
30
+ request.to_hash.each do |k, v|
31
+ subreqest += "#{k}: #{v.join(';')}\r\n"
32
+ end
33
+ subreqest += "\r\n" \
34
+ "#{body_str}\r\n"
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,39 @@
1
+ require 'delegate' if RUBY_VERSION >= '2.7'
2
+
3
+ module Fcmpush
4
+ class BatchResponse < DelegateClass(Net::HTTPResponse)
5
+ alias response __getobj__
6
+ alias headers to_hash
7
+ HAS_SYMBOL_GC = RUBY_VERSION > '2.2.0'
8
+
9
+ def json
10
+ parsable? ? @parsed ||= parse_body(body) : nil
11
+ end
12
+
13
+ def inspect
14
+ "#<BatchResponse response: #{response.inspect}, json: #{json}>"
15
+ end
16
+ alias to_s inspect
17
+
18
+ def parsable?
19
+ !body.nil? && !body.empty?
20
+ end
21
+
22
+ def success_count
23
+ @success_count ||= json.length - failure_count
24
+ end
25
+
26
+ def failure_count
27
+ @failure_count ||= json.select { |i| i[:error] }.size
28
+ end
29
+
30
+ private
31
+
32
+ def parse_body(raw_body)
33
+ devider = raw_body.match(/(\r\n--batch_.*)\r\n/)[1]
34
+ raw_body.split(devider)[1..-2].map do |response|
35
+ JSON.parse(response.split("\r\n\r\n")[2], symbolize_names: HAS_SYMBOL_GC)
36
+ end
37
+ end
38
+ end
39
+ end
@@ -1,13 +1,17 @@
1
1
  require 'fcmpush/exceptions'
2
+ require 'fcmpush/batch'
2
3
  require 'fcmpush/json_response'
4
+ require 'fcmpush/batch_response'
3
5
 
4
6
  module Fcmpush
5
7
  V1_ENDPOINT_PREFIX = '/v1/projects/'.freeze
6
8
  V1_ENDPOINT_SUFFIX = '/messages:send'.freeze
7
9
  TOPIC_DOMAIN = 'https://iid.googleapis.com'.freeze
8
10
  TOPIC_ENDPOINT_PREFIX = '/iid/v1'.freeze
11
+ BATCH_ENDPOINT = '/batch'.freeze
9
12
 
10
13
  class Client
14
+ include Batch
11
15
  attr_reader :domain, :path, :connection, :configuration, :server_key, :access_token, :access_token_expiry
12
16
 
13
17
  def initialize(domain, project_id, configuration, **options)
@@ -15,7 +19,7 @@ module Fcmpush
15
19
  @project_id = project_id
16
20
  @path = V1_ENDPOINT_PREFIX + project_id.to_s + V1_ENDPOINT_SUFFIX
17
21
  @options = {}.merge(options)
18
- @configuration = configuration
22
+ @configuration = configuration.dup
19
23
  access_token_response = v1_authorize
20
24
  @access_token = access_token_response['access_token']
21
25
  @access_token_expiry = Time.now.utc + access_token_response['expires_in']
@@ -25,8 +29,13 @@ module Fcmpush
25
29
 
26
30
  def v1_authorize
27
31
  @auth ||= if configuration.json_key_io
32
+ io = if configuration.json_key_io.respond_to?(:read)
33
+ configuration.json_key_io
34
+ else
35
+ File.open(configuration.json_key_io)
36
+ end
28
37
  Google::Auth::ServiceAccountCredentials.make_creds(
29
- json_key_io: File.open(configuration.json_key_io),
38
+ json_key_io: io,
30
39
  scope: configuration.scope
31
40
  )
32
41
  else
@@ -60,6 +69,14 @@ module Fcmpush
60
69
  raise NetworkError, "A network error occurred: #{e.class} (#{e.message})"
61
70
  end
62
71
 
72
+ def batch_push(messages, query: {}, headers: {})
73
+ uri, request = make_batch_request(messages, query, headers)
74
+ response = exception_handler(connection.request(uri, request))
75
+ BatchResponse.new(response)
76
+ rescue Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, EOFError, Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError => e
77
+ raise NetworkError, "A network error occurred: #{e.class} (#{e.message})"
78
+ end
79
+
63
80
  private
64
81
 
65
82
  def make_push_request(body, query, headers)
@@ -121,5 +138,18 @@ module Fcmpush
121
138
  registration_tokens: instance_ids
122
139
  }.to_json
123
140
  end
141
+
142
+ def make_batch_request(messages, query, headers)
143
+ uri = URI.join(domain, BATCH_ENDPOINT)
144
+ uri.query = URI.encode_www_form(query) unless query.empty?
145
+
146
+ access_token_refresh
147
+ headers = v1_authorized_header(headers)
148
+ post = Net::HTTP::Post.new(uri, headers)
149
+ post['Content-Type'] = "multipart/mixed; boundary=#{::Fcmpush::Batch::PART_BOUNDRY}"
150
+ post.body = make_batch_payload(messages, headers)
151
+
152
+ [uri, post]
153
+ end
124
154
  end
125
155
  end
@@ -1,3 +1,3 @@
1
1
  module Fcmpush
2
- VERSION = '1.0.0'.freeze
2
+ VERSION = '1.3.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fcmpush
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - miyataka
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-12-29 00:00:00.000000000 Z
11
+ date: 2021-01-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: googleauth
@@ -28,16 +28,16 @@ dependencies:
28
28
  name: net-http-persistent
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - ">="
31
+ - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: 3.1.0
33
+ version: 4.0.1
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - ">="
38
+ - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: 3.1.0
40
+ version: 4.0.1
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: bundler
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -58,14 +58,14 @@ dependencies:
58
58
  requirements:
59
59
  - - "~>"
60
60
  - !ruby/object:Gem::Version
61
- version: '10.0'
61
+ version: '13'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
- version: '10.0'
68
+ version: '13'
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: rspec
71
71
  requirement: !ruby/object:Gem::Requirement
@@ -80,7 +80,7 @@ dependencies:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
82
  version: '3.0'
83
- description:
83
+ description:
84
84
  email:
85
85
  - voyager.3taka28@gmail.com
86
86
  executables: []
@@ -89,6 +89,8 @@ extra_rdoc_files: []
89
89
  files:
90
90
  - fcmpush.gemspec
91
91
  - lib/fcmpush.rb
92
+ - lib/fcmpush/batch.rb
93
+ - lib/fcmpush/batch_response.rb
92
94
  - lib/fcmpush/client.rb
93
95
  - lib/fcmpush/configuration.rb
94
96
  - lib/fcmpush/exceptions.rb
@@ -100,7 +102,7 @@ licenses:
100
102
  metadata:
101
103
  homepage_uri: https://github.com/miyataka/fcmpush
102
104
  source_code_uri: https://github.com/miyataka/fcmpush
103
- post_install_message:
105
+ post_install_message:
104
106
  rdoc_options: []
105
107
  require_paths:
106
108
  - lib
@@ -108,19 +110,18 @@ required_ruby_version: !ruby/object:Gem::Requirement
108
110
  requirements:
109
111
  - - ">="
110
112
  - !ruby/object:Gem::Version
111
- version: '2.3'
112
- - - "<"
113
+ version: '2.4'
114
+ - - "<="
113
115
  - !ruby/object:Gem::Version
114
- version: '2.8'
116
+ version: '3.0'
115
117
  required_rubygems_version: !ruby/object:Gem::Requirement
116
118
  requirements:
117
119
  - - ">="
118
120
  - !ruby/object:Gem::Version
119
121
  version: '0'
120
122
  requirements: []
121
- rubyforge_project:
122
- rubygems_version: 2.6.14.4
123
- signing_key:
123
+ rubygems_version: 3.2.3
124
+ signing_key:
124
125
  specification_version: 4
125
126
  summary: Firebase Cloud Messaging API wrapper for ruby, supports HTTP v1. And including
126
127
  access_token Auto Refresh feature!