fcmpush 1.1.0 → 1.3.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/fcmpush.gemspec +3 -3
- data/lib/fcmpush/batch.rb +37 -0
- data/lib/fcmpush/batch_response.rb +39 -0
- data/lib/fcmpush/client.rb +26 -1
- data/lib/fcmpush/version.rb +1 -1
- metadata +18 -16
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d01eb048620dbc5a8b38f25e302431f2a4d990017fa23c3346c1317418e390d0
|
4
|
+
data.tar.gz: ff37ce62519b04d32637d7842962ce6037c170d8cd1ee49c53319982b0e7d679
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 943028b0bf4ddc69505ed01c43af6b83693d01ec1a5c83a26b859849ee3796dc63f0f0d2a5fdc382b02f58764a05aa04e00f80dbf3e9194a49d93ad1e9c0998a
|
7
|
+
data.tar.gz: 88c83ae5fc8decf2e2a41e5b56e35d25c70643f156c6c558438812cff933574c4b4179759804909bb50c8d697ee495fe3ff9267c1ed2d2eebac186357a13b6f9
|
data/fcmpush.gemspec
CHANGED
@@ -22,10 +22,10 @@ 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.
|
25
|
+
spec.required_ruby_version = '>= 2.4', '< 3.1'
|
26
26
|
|
27
|
-
spec.add_dependency '
|
28
|
-
spec.add_dependency 'net-http-persistent', '
|
27
|
+
spec.add_dependency 'google-apis-identitytoolkit_v3'
|
28
|
+
spec.add_dependency 'net-http-persistent', '~> 4.0.1'
|
29
29
|
|
30
30
|
spec.add_development_dependency 'bundler', '~> 2.0'
|
31
31
|
spec.add_development_dependency 'rake', '~> 13'
|
@@ -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
|
data/lib/fcmpush/client.rb
CHANGED
@@ -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']
|
@@ -65,6 +69,14 @@ module Fcmpush
|
|
65
69
|
raise NetworkError, "A network error occurred: #{e.class} (#{e.message})"
|
66
70
|
end
|
67
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
|
+
|
68
80
|
private
|
69
81
|
|
70
82
|
def make_push_request(body, query, headers)
|
@@ -126,5 +138,18 @@ module Fcmpush
|
|
126
138
|
registration_tokens: instance_ids
|
127
139
|
}.to_json
|
128
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
|
129
154
|
end
|
130
155
|
end
|
data/lib/fcmpush/version.rb
CHANGED
metadata
CHANGED
@@ -1,43 +1,43 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fcmpush
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.3.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- miyataka
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-04-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
|
-
name:
|
14
|
+
name: google-apis-identitytoolkit_v3
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: 0
|
19
|
+
version: '0'
|
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: 0
|
26
|
+
version: '0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
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:
|
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:
|
40
|
+
version: 4.0.1
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: bundler
|
43
43
|
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,18 +110,18 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
108
110
|
requirements:
|
109
111
|
- - ">="
|
110
112
|
- !ruby/object:Gem::Version
|
111
|
-
version: '2.
|
113
|
+
version: '2.4'
|
112
114
|
- - "<"
|
113
115
|
- !ruby/object:Gem::Version
|
114
|
-
version: '
|
116
|
+
version: '3.1'
|
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
|
-
rubygems_version: 3.
|
122
|
-
signing_key:
|
123
|
+
rubygems_version: 3.2.15
|
124
|
+
signing_key:
|
123
125
|
specification_version: 4
|
124
126
|
summary: Firebase Cloud Messaging API wrapper for ruby, supports HTTP v1. And including
|
125
127
|
access_token Auto Refresh feature!
|