fcmpush 0.9.1 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ca7fcf74ed6f65f87af9ac77aabdda313a8593d1954d6250be94e7f4d819307c
4
- data.tar.gz: fd3fa1913cef61ff15b36791a7fc9b35032aaaa1f0e3d5e98faa46c0b3903f24
3
+ metadata.gz: 1daa7e3e80decc9d67986e0ee52eee2863e267dada73aab4ded259f4499577f0
4
+ data.tar.gz: c3f1ae5e9a147f98e32166ccabdc0d8f8a9ad37cf5157827c1e9d3eec67f6e15
5
5
  SHA512:
6
- metadata.gz: 4bc377cb3eacfd52f28ef7e4d882c20314b0461d1cd12db8ecaf508cb0b2568514760cae969d296c099290704a5169aa08309dbf5eb38b21aaf8910a013ce3e2
7
- data.tar.gz: 0a7b0625078de8fa54e300d088fdc6bf4228cfda70033c7489514e4943738faa0c88259e955da3ba4b8002e389f59b5315748233576871decb9c1c75c3636f60
6
+ metadata.gz: 44d3ca77d9d4ce80b28f4cae8a33b8a6c7ce424b796d1c7509faf353053d64f44eed78b30bf683e92c8d7da0186b19d49ee026c1297ca986dd5e8bf344789282
7
+ data.tar.gz: 5e6cc5dbff0abaa26db3896e0662a06ea64cab2f2e5bd655710a3571eae9b407d17e0396dcce113199a8e245f105a2145a9e194dc95d84ff57e1ebedabab9e5b
@@ -8,7 +8,7 @@ Gem::Specification.new do |spec|
8
8
  spec.authors = ['miyataka']
9
9
  spec.email = ['voyager.3taka28@gmail.com']
10
10
 
11
- spec.summary = 'Firebase Cloud Messaging API wrapper for ruby, supports HTTP v1.'
11
+ spec.summary = 'Firebase Cloud Messaging API wrapper for ruby, supports HTTP v1. And including access_token Auto Refresh feature!'
12
12
  spec.homepage = 'https://github.com/miyataka/fcmpush'
13
13
  spec.license = 'MIT'
14
14
 
@@ -16,16 +16,18 @@ Gem::Specification.new do |spec|
16
16
  spec.metadata['source_code_uri'] = 'https://github.com/miyataka/fcmpush'
17
17
 
18
18
  spec.files = Dir.chdir(File.expand_path(__dir__)) do
19
- `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
19
+ `git ls-files -z`.split("\x0").select { |f| f.match(%r{^(lib/|fcmpush.gemspec)}) }
20
20
  end
21
21
  spec.bindir = 'exe'
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'
26
+
25
27
  spec.add_dependency 'googleauth', '>= 0.9.0'
26
28
  spec.add_dependency 'net-http-persistent', '>= 3.1.0'
27
29
 
28
30
  spec.add_development_dependency 'bundler', '~> 2.0'
29
- spec.add_development_dependency 'rake', '~> 10.0'
31
+ spec.add_development_dependency 'rake', '~> 13'
30
32
  spec.add_development_dependency 'rspec', '~> 3.0'
31
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)
@@ -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,4 @@
1
+ require 'delegate' if RUBY_VERSION >= '2.7'
1
2
  require 'json'
2
3
 
3
4
  module Fcmpush
@@ -1,3 +1,3 @@
1
1
  module Fcmpush
2
- VERSION = '0.9.1'.freeze
2
+ VERSION = '1.2.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: 0.9.1
4
+ version: 1.2.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-10-21 00:00:00.000000000 Z
11
+ date: 2020-07-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: googleauth
@@ -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,25 +80,17 @@ 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: []
87
87
  extensions: []
88
88
  extra_rdoc_files: []
89
89
  files:
90
- - ".gitignore"
91
- - ".rspec"
92
- - ".rubocop.yml"
93
- - ".travis.yml"
94
- - Gemfile
95
- - LICENSE.txt
96
- - README.md
97
- - Rakefile
98
- - bin/console
99
- - bin/setup
100
90
  - fcmpush.gemspec
101
91
  - lib/fcmpush.rb
92
+ - lib/fcmpush/batch.rb
93
+ - lib/fcmpush/batch_response.rb
102
94
  - lib/fcmpush/client.rb
103
95
  - lib/fcmpush/configuration.rb
104
96
  - lib/fcmpush/exceptions.rb
@@ -110,7 +102,7 @@ licenses:
110
102
  metadata:
111
103
  homepage_uri: https://github.com/miyataka/fcmpush
112
104
  source_code_uri: https://github.com/miyataka/fcmpush
113
- post_install_message:
105
+ post_install_message:
114
106
  rdoc_options: []
115
107
  require_paths:
116
108
  - lib
@@ -118,7 +110,10 @@ required_ruby_version: !ruby/object:Gem::Requirement
118
110
  requirements:
119
111
  - - ">="
120
112
  - !ruby/object:Gem::Version
121
- version: '0'
113
+ version: '2.3'
114
+ - - "<"
115
+ - !ruby/object:Gem::Version
116
+ version: '2.8'
122
117
  required_rubygems_version: !ruby/object:Gem::Requirement
123
118
  requirements:
124
119
  - - ">="
@@ -126,7 +121,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
126
121
  version: '0'
127
122
  requirements: []
128
123
  rubygems_version: 3.0.3
129
- signing_key:
124
+ signing_key:
130
125
  specification_version: 4
131
- summary: Firebase Cloud Messaging API wrapper for ruby, supports HTTP v1.
126
+ summary: Firebase Cloud Messaging API wrapper for ruby, supports HTTP v1. And including
127
+ access_token Auto Refresh feature!
132
128
  test_files: []
data/.gitignore DELETED
@@ -1,13 +0,0 @@
1
- /.bundle/
2
- /.yardoc
3
- /_yardoc/
4
- /coverage/
5
- /doc/
6
- /pkg/
7
- /spec/reports/
8
- /tmp/
9
-
10
- # rspec failure tracking
11
- .rspec_status
12
-
13
- /vendor/bundle
data/.rspec DELETED
@@ -1,3 +0,0 @@
1
- --format documentation
2
- --color
3
- --require spec_helper
@@ -1,11 +0,0 @@
1
- Style/FrozenStringLiteralComment:
2
- Enabled: false
3
-
4
- Metrics/LineLength:
5
- Enabled: false
6
-
7
- Style/Documentation:
8
- Enabled: false
9
-
10
- Layout/IndentationConsistency:
11
- EnforcedStyle: indented_internal_methods
@@ -1,13 +0,0 @@
1
- sudo: false
2
- language: ruby
3
- cache: bundler
4
- script: bundle exec rspec
5
- rvm:
6
- - 2.3.8
7
- - 2.4.9
8
- - 2.5.7
9
- - 2.6.5
10
- before_install: gem install bundler -v 2.0.2
11
- notifications:
12
- slack:
13
- secure: Uckm/iIr1N6dvkzJeGes8yARAQSHLFGWjAw7CsNGiU5KERf0a/WWo+8OoWAqBtgcnqVDdBcNm0aUax+5JbfyLg0q8YW1wtYuAE2Gqocvs5TPt8XwyEQv6sYyN1mqrcJWl4FO22DCxF+Oetju1wEjpjSVYs6uzDrj7U6HzuJON/yd+lUjJWOD1OOaRJUYjI9uU7x7RRscECGU8p0ffjeiGOkM2EuJ/osA/CeDlDd93hlUpseZqxceQGdsSZHscbqV0eWVDexoPtUOIK08VFgCk3FIA4vfO8XDaBibSiqiTYUPGPQILjMhGKmB/dmz2LyxdWGdCBOjH1CnD1LXNq9yPhquDXm/2u4cVWIV7bgOHDZ6ylRz0u+AUWVMI/VJOd27YOkZCGgqbb12Y7sBh0m9KalU/fqfIafOvt24j5o2abjJCKHoTUgdESpbLYqnZvK00uRjjOp0H0yQnXicaFE4lByAGbxmpxWZ3fs1OZkppYOl8dF9AQWB6Uty4KraTpUB6fGHoE7/IPeUNsENXWPUqITsRDPFjCTWED/YvKfUi+xTA+K1vJZympsPevT6xcL82TXGDtdHA4U52MLiAuizXP/+R3XhO2PzBSyLbEMus2iJYwXsIac157uhcDrECQfIkm9DMtSHKP83ABf/Flj73NKayWYP0OsigBdUYVgs5aQ=
data/Gemfile DELETED
@@ -1,7 +0,0 @@
1
- source 'https://rubygems.org'
2
-
3
- # Specify your gem's dependencies in fcmpush.gemspec
4
- gemspec
5
-
6
- gem 'activesupport', '< 6.0'
7
- gem 'pry-byebug'
@@ -1,21 +0,0 @@
1
- The MIT License (MIT)
2
-
3
- Copyright (c) 2019 Takayuki Miyahara
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in
13
- all copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
- THE SOFTWARE.
data/README.md DELETED
@@ -1,99 +0,0 @@
1
- # Fcmpush [![Build Status](https://travis-ci.org/miyataka/fcmpush.svg?branch=master)](https://travis-ci.org/miyataka/fcmpush)
2
-
3
- Fcmpush is an Firebase Cloud Messaging(FCM) Client. It implements [FCM HTTP v1 API](https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages), including **Auto Refresh** access_token feature!!
4
- This gem supports HTTP v1 API only, **NOT supported [legacy HTTP protocol](https://firebase.google.com/docs/cloud-messaging/http-server-ref)**.
5
-
6
- fcmpush is highly inspired by [andpush gem](https://github.com/yuki24/andpush).
7
-
8
- ## Installation
9
-
10
- Add this line to your application's Gemfile:
11
-
12
- ```ruby
13
- gem 'fcmpush'
14
- ```
15
-
16
- And then execute:
17
-
18
- $ bundle
19
-
20
- Or install it yourself as:
21
-
22
- $ gem install fcmpush
23
-
24
- ## Usage
25
-
26
- on Rails, config/initializers/fcmpush.rb
27
- ```ruby
28
- Fcmpush.configure do |config|
29
- ## for message push
30
- # firebase web console => project settings => service account => firebase admin sdk => generate new private key
31
- config.json_key_io = "#{Rails.root}/path/to/service_account_credentials.json"
32
-
33
- # Or set environment variables
34
- # ENV['GOOGLE_ACCOUNT_TYPE'] = 'service_account'
35
- # ENV['GOOGLE_CLIENT_ID'] = '000000000000000000000'
36
- # ENV['GOOGLE_CLIENT_EMAIL'] = 'xxxx@xxxx.iam.gserviceaccount.com'
37
- # ENV['GOOGLE_PRIVATE_KEY'] = '-----BEGIN PRIVATE KEY-----\n...\n-----END PRIVATE KEY-----\n\'
38
-
39
- ## for topic subscribe/unsubscribe because they use regacy auth
40
- # firebase web console => project settings => cloud messaging => Project credentials => Server key
41
- config.server_key = 'your firebase server key'
42
- # Or set environment variables
43
- # ENV['FCM_SERVER_KEY'] = 'your firebase server key'
44
- end
45
- ```
46
-
47
- for more detail. see [here](https://github.com/googleapis/google-auth-library-ruby#example-service-account).
48
-
49
- ### push message
50
- ```ruby
51
- require 'fcmpush'
52
-
53
- project_id = "..." # Your project_id
54
- device_token = "..." # The device token of the device you'd like to push a message to
55
-
56
- client = Fcmpush.new(project_id)
57
- payload = { # ref. https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages
58
- message: {
59
- token: device_token,
60
- notification: {
61
- title: "this is title",
62
- body: "this is message body"
63
- }
64
- }
65
- }
66
-
67
- response = client.push(payload)
68
-
69
- json = response.json
70
- json[:name] # => "projects/[your_project_id]/messages/0:1571037134532751%31bd1c9631bd1c96"
71
- ```
72
-
73
- ### topic subscribe/unsubscribe
74
- ```ruby
75
- require 'fcmpush'
76
-
77
- project_id = "..." # Your project_id
78
- topic = "your_topic_name"
79
- device_tokens = ["device_tokenA", "device_tokenB", ...] # The device tokens of the device you'd like to subscribe
80
-
81
- client = Fcmpush.new(project_id)
82
-
83
- response = client.subscribe(topic, device_tokens)
84
- # response = client.unsubscribe(topic, device_tokens)
85
-
86
- json = response.json
87
- json[:results] # => [{}, {"error":"NOT_FOUND"}, ...] ref. https://developers.google.com/instance-id/reference/server#example_result_3
88
- ```
89
-
90
- ## Future Work
91
- - [DEV] compare other gems
92
-
93
- ## Contributing
94
-
95
- Bug reports and pull requests are welcome on GitHub at https://github.com/miyataka/fcmpush.
96
-
97
- ## License
98
-
99
- The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile DELETED
@@ -1,6 +0,0 @@
1
- require "bundler/gem_tasks"
2
- require "rspec/core/rake_task"
3
-
4
- RSpec::Core::RakeTask.new(:spec)
5
-
6
- task :default => :spec
@@ -1,14 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- require "bundler/setup"
4
- require "fcmpush"
5
-
6
- # You can add fixtures and/or initialization code here to make experimenting
7
- # with your gem easier. You can also use a different console, if you like.
8
-
9
- # (If you use this, don't forget to add pry to your Gemfile!)
10
- # require "pry"
11
- # Pry.start
12
-
13
- require "irb"
14
- IRB.start(__FILE__)
data/bin/setup DELETED
@@ -1,8 +0,0 @@
1
- #!/usr/bin/env bash
2
- set -euo pipefail
3
- IFS=$'\n\t'
4
- set -vx
5
-
6
- bundle install
7
-
8
- # Do any other automated setup that you need to do here