andpush 0.1.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 +7 -0
- data/.gitignore +10 -0
- data/.travis.yml +18 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +9 -0
- data/LICENSE.txt +21 -0
- data/README.md +51 -0
- data/Rakefile +15 -0
- data/andpush.gemspec +23 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/fcm.apispec +7 -0
- data/lib/andpush.rb +43 -0
- data/lib/andpush/client.rb +90 -0
- data/lib/andpush/exceptions.rb +110 -0
- data/lib/andpush/json_handler.rb +46 -0
- data/lib/andpush/version.rb +3 -0
- data/response_types.yml +17 -0
- metadata +117 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 5906e62af1538446835164d870e6ad5208c273b8
|
4
|
+
data.tar.gz: 152292ea0800e0ea8b8ea5646fde079488f9a29e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 1388a7d47b11a593d3bf831be5f95a302aae5abae28c4edde8823575e263f89d7945089cf288d8ada21f88f3c39bfbcbf5089849b2edefa4940a05856b48ddc8
|
7
|
+
data.tar.gz: 8466478c030432ceeb73bdc59a2314bd5a09a964ccd355d92badc2792bdc76beb77a07ad9f9dbec3664e31956d11a88220878bdb3816dcf837caecab620001c3
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
language: ruby
|
2
|
+
cache: bundler
|
3
|
+
script: bundle exec rake test
|
4
|
+
sudo: false
|
5
|
+
|
6
|
+
rvm:
|
7
|
+
- 2.2.7
|
8
|
+
- 2.3.4
|
9
|
+
- 2.4.1
|
10
|
+
- ruby-head
|
11
|
+
- jruby-9.1.10.0
|
12
|
+
- jruby-head
|
13
|
+
|
14
|
+
matrix:
|
15
|
+
allow_failures:
|
16
|
+
- rvm: ruby-head
|
17
|
+
- rvm: jruby-9.1.10.0
|
18
|
+
- rvm: jruby-head
|
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at mail@yukinishijima.net. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 Yuki Nishijima
|
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
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
# Andpush
|
2
|
+
|
3
|
+
Andpush is an HTTP client for FCM (Firebase Cloud Messaging). It implements [Firebase Cloud Messaging HTTP Protocol](https://firebase.google.com/docs/cloud-messaging/http-server-ref).
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'andpush'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install andpush
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
You'll need your application's server key, whose value is available in the [Cloud Messaging](https://console.firebase.google.com/project/_/settings/cloudmessaging) tab of the Firebase console Settings pane.
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
require 'andpush'
|
27
|
+
|
28
|
+
server_key = "..." # Your server key
|
29
|
+
device_token = "..." # The device token of the device you'd like to push a message to
|
30
|
+
|
31
|
+
client = Andpush.build(server_key)
|
32
|
+
response = client.push(to: device_token, notification: { title: "Update", body: "Your weekly summary is ready" }, data: { extra: "data" })
|
33
|
+
|
34
|
+
json = response.json
|
35
|
+
json[:canonical_ids] # => 0
|
36
|
+
json[:failure] # => 0
|
37
|
+
json[:multicast_id] # => 8478364278516813477
|
38
|
+
|
39
|
+
result = json[:results].first
|
40
|
+
result[:message_id] # => "0:1489498959348701%3b8aef473b8aef47"
|
41
|
+
result[:error] # => nil, "InvalidRegistration" or something else
|
42
|
+
result[:registration_id] # => nil
|
43
|
+
```
|
44
|
+
|
45
|
+
## Contributing
|
46
|
+
|
47
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/yuki24/andpush. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
48
|
+
|
49
|
+
## License
|
50
|
+
|
51
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
2
|
+
require "rake/testtask"
|
3
|
+
|
4
|
+
Rake::TestTask.new(:test) do |t|
|
5
|
+
t.libs << "test"
|
6
|
+
t.libs << "lib"
|
7
|
+
t.test_files = FileList['test/**/*_test.rb']
|
8
|
+
end
|
9
|
+
|
10
|
+
desc 'Generate an API client with the oven gem (the cmd overrides the client if it already exists)'
|
11
|
+
task :generate do
|
12
|
+
sh "ruby -roven fcm.apispec && rubocop -a lib/"
|
13
|
+
end
|
14
|
+
|
15
|
+
task :default => :test
|
data/andpush.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'andpush/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "andpush"
|
8
|
+
spec.version = Andpush::VERSION
|
9
|
+
spec.authors = ["Yuki Nishijima"]
|
10
|
+
spec.email = ["mail@yukinishijima.net"]
|
11
|
+
spec.summary = %q{FCM HTTP client}
|
12
|
+
spec.description = %q{Andpush is an HTTP client for Firebase Cloud Messaging.}
|
13
|
+
spec.homepage = "https://github.com/yuki24/andpush"
|
14
|
+
spec.license = "MIT"
|
15
|
+
spec.files = `git ls-files -z`.split("\x0").reject {|f| f.match(%r{^(test)/}) }
|
16
|
+
spec.require_paths = ["lib"]
|
17
|
+
|
18
|
+
spec.add_dependency 'net-http-persistent'
|
19
|
+
|
20
|
+
spec.add_development_dependency "bundler"
|
21
|
+
spec.add_development_dependency "rake"
|
22
|
+
spec.add_development_dependency "minitest"
|
23
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "andpush"
|
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
ADDED
data/fcm.apispec
ADDED
data/lib/andpush.rb
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'net/http/persistent'
|
2
|
+
|
3
|
+
require 'andpush/version'
|
4
|
+
require 'andpush/client'
|
5
|
+
|
6
|
+
module Andpush
|
7
|
+
DOMAIN = 'https://fcm.googleapis.com'.freeze
|
8
|
+
|
9
|
+
def self.build(server_key, domain: nil, request_handler: ConnectionPool.new)
|
10
|
+
::Andpush::Client
|
11
|
+
.new(domain || DOMAIN, request_handler: request_handler)
|
12
|
+
.register_interceptor(Authenticator.new(server_key))
|
13
|
+
end
|
14
|
+
|
15
|
+
class Authenticator
|
16
|
+
def initialize(server_key)
|
17
|
+
@server_key = server_key
|
18
|
+
end
|
19
|
+
|
20
|
+
def before_request(uri, body, headers, options)
|
21
|
+
headers['Authorization'] = "key=#{@server_key}"
|
22
|
+
|
23
|
+
[uri, body, headers, options]
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
class ConnectionPool
|
28
|
+
attr_reader :connection
|
29
|
+
|
30
|
+
def initialize(name: nil, proxy: nil, pool_size: Net::HTTP::Persistent::DEFAULT_POOL_SIZE)
|
31
|
+
@connection = Net::HTTP::Persistent.new(name: name, proxy: proxy, pool_size: pool_size)
|
32
|
+
end
|
33
|
+
|
34
|
+
def call(request_class, uri, headers, body, *_)
|
35
|
+
req = request_class.new(uri, headers)
|
36
|
+
req.set_body_internal(body)
|
37
|
+
|
38
|
+
connection.request(uri, req)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
private_constant :Authenticator, :ConnectionPool
|
43
|
+
end
|
@@ -0,0 +1,90 @@
|
|
1
|
+
# frozen-string-literal: true
|
2
|
+
require 'net/http'
|
3
|
+
require 'andpush/exceptions'
|
4
|
+
require 'andpush/json_handler'
|
5
|
+
|
6
|
+
module Andpush
|
7
|
+
class Client
|
8
|
+
attr_reader :domain, :proxy_addr, :proxy_port, :proxy_user, :proxy_password, :request_handler
|
9
|
+
|
10
|
+
def initialize(domain, proxy_addr: nil, proxy_port: nil, proxy_user: nil, proxy_password: nil, request_handler: RequestHandler.new, **options)
|
11
|
+
@domain = domain
|
12
|
+
@proxy_addr = proxy_addr
|
13
|
+
@proxy_port = proxy_port
|
14
|
+
@proxy_user = proxy_user
|
15
|
+
@proxy_password = proxy_password
|
16
|
+
@interceptors = []
|
17
|
+
@observers = []
|
18
|
+
@request_handler = request_handler
|
19
|
+
@options = DEFAULT_OPTIONS.merge(options)
|
20
|
+
|
21
|
+
register_interceptor(JsonSerializer.new)
|
22
|
+
register_observer(ResponseHandler.new)
|
23
|
+
register_observer(JsonDeserializer.new)
|
24
|
+
end
|
25
|
+
|
26
|
+
def register_interceptor(interceptor)
|
27
|
+
@interceptors << interceptor
|
28
|
+
self
|
29
|
+
end
|
30
|
+
|
31
|
+
def register_observer(observer)
|
32
|
+
@observers << observer
|
33
|
+
self
|
34
|
+
end
|
35
|
+
|
36
|
+
def push(body, query: {}, headers: {}, **options)
|
37
|
+
request(Net::HTTP::Post, uri('/fcm/send', query), body, headers, method: :push, **options)
|
38
|
+
end
|
39
|
+
|
40
|
+
private
|
41
|
+
|
42
|
+
DEFAULT_OPTIONS = {
|
43
|
+
ca_file: nil,
|
44
|
+
ca_path: nil,
|
45
|
+
cert: nil,
|
46
|
+
cert_store: nil,
|
47
|
+
ciphers: nil,
|
48
|
+
close_on_empty_response: nil,
|
49
|
+
key: nil,
|
50
|
+
open_timeout: nil,
|
51
|
+
read_timeout: nil,
|
52
|
+
ssl_timeout: nil,
|
53
|
+
ssl_version: nil,
|
54
|
+
use_ssl: nil,
|
55
|
+
verify_callback: nil,
|
56
|
+
verify_depth: nil,
|
57
|
+
verify_mode: nil
|
58
|
+
}.freeze
|
59
|
+
|
60
|
+
HTTPS = 'https'.freeze
|
61
|
+
|
62
|
+
def request(request_class, uri, body, headers, options = {})
|
63
|
+
uri, body, headers, options = @interceptors.reduce([uri, body, headers, @options.merge(options)]) { |r, i| i.before_request(*r) }
|
64
|
+
|
65
|
+
response = begin
|
66
|
+
request_handler.call(request_class, uri, headers, body, proxy_addr, proxy_port, proxy_user, proxy_password, options)
|
67
|
+
rescue Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, EOFError, Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError => e
|
68
|
+
raise NetworkError, "A network error occurred: #{e.class} (#{e.message})"
|
69
|
+
end
|
70
|
+
|
71
|
+
@observers.reduce(response) { |r, o| o.received_response(r, options) }
|
72
|
+
end
|
73
|
+
|
74
|
+
def uri(path, query = {})
|
75
|
+
uri = URI.join(domain, path)
|
76
|
+
uri.query = URI.encode_www_form(query) unless query.empty?
|
77
|
+
uri
|
78
|
+
end
|
79
|
+
|
80
|
+
class RequestHandler
|
81
|
+
def call(request_class, uri, headers, body, proxy_addr, proxy_port, proxy_user, proxy_password, options = {})
|
82
|
+
Net::HTTP.start(uri.host, uri.port, proxy_addr, proxy_port, proxy_user, proxy_password, options, use_ssl: (uri.scheme == HTTPS)) do |http|
|
83
|
+
http.request request_class.new(uri, headers), body
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
private_constant :RequestHandler
|
89
|
+
end
|
90
|
+
end
|
@@ -0,0 +1,110 @@
|
|
1
|
+
module Andpush
|
2
|
+
class APIError < StandardError; end
|
3
|
+
class NetworkError < APIError; end
|
4
|
+
|
5
|
+
class HttpError < APIError
|
6
|
+
attr_reader :response
|
7
|
+
|
8
|
+
def initialize(message, response)
|
9
|
+
super(message)
|
10
|
+
@response = response
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
class ClientError < HttpError; end
|
15
|
+
|
16
|
+
class BadRequest < ClientError; end # status: 400
|
17
|
+
class Unauthorized < ClientError; end # status: 401
|
18
|
+
class PaymentRequired < ClientError; end # status: 402
|
19
|
+
class Forbidden < ClientError; end # status: 403
|
20
|
+
class NotFound < ClientError; end # status: 404
|
21
|
+
class MethodNotAllowed < ClientError; end # status: 405
|
22
|
+
class NotAcceptable < ClientError; end # status: 406
|
23
|
+
class ProxyAuthenticationRequired < ClientError; end # status: 407
|
24
|
+
class RequestTimeout < ClientError; end # status: 408
|
25
|
+
class Conflict < ClientError; end # status: 409
|
26
|
+
class Gone < ClientError; end # status: 410
|
27
|
+
class LengthRequired < ClientError; end # status: 411
|
28
|
+
class PreconditionFailed < ClientError; end # status: 412
|
29
|
+
class PayloadTooLarge < ClientError; end # status: 413
|
30
|
+
class URITooLong < ClientError; end # status: 414
|
31
|
+
class UnsupportedMediaType < ClientError; end # status: 415
|
32
|
+
class RangeNotSatisfiable < ClientError; end # status: 416
|
33
|
+
class ExpectationFailed < ClientError; end # status: 417
|
34
|
+
class ImaTeapot < ClientError; end # status: 418
|
35
|
+
class MisdirectedRequest < ClientError; end # status: 421
|
36
|
+
class UnprocessableEntity < ClientError; end # status: 422
|
37
|
+
class Locked < ClientError; end # status: 423
|
38
|
+
class FailedDependency < ClientError; end # status: 424
|
39
|
+
class UpgradeRequired < ClientError; end # status: 426
|
40
|
+
class PreconditionRequired < ClientError; end # status: 428
|
41
|
+
class TooManyRequests < ClientError; end # status: 429
|
42
|
+
class RequestHeaderFieldsTooLarge < ClientError; end # status: 431
|
43
|
+
class UnavailableForLegalReasons < ClientError; end # status: 451
|
44
|
+
|
45
|
+
class ServerError < HttpError; end
|
46
|
+
|
47
|
+
class InternalServerError < ServerError; end # status: 500
|
48
|
+
class NotImplemented < ServerError; end # status: 501
|
49
|
+
class BadGateway < ServerError; end # status: 502
|
50
|
+
class ServiceUnavailable < ServerError; end # status: 503
|
51
|
+
class GatewayTimeout < ServerError; end # status: 504
|
52
|
+
class HTTPVersionNotSupported < ServerError; end # status: 505
|
53
|
+
class VariantAlsoNegotiates < ServerError; end # status: 506
|
54
|
+
class InsufficientStorage < ServerError; end # status: 507
|
55
|
+
class LoopDetected < ServerError; end # status: 508
|
56
|
+
class NotExtended < ServerError; end # status: 510
|
57
|
+
class NetworkAuthenticationRequired < ServerError; end # status: 511
|
58
|
+
|
59
|
+
STATUS_TO_EXCEPTION_MAPPING = {
|
60
|
+
'400' => BadRequest,
|
61
|
+
'401' => Unauthorized,
|
62
|
+
'402' => PaymentRequired,
|
63
|
+
'403' => Forbidden,
|
64
|
+
'404' => NotFound,
|
65
|
+
'405' => MethodNotAllowed,
|
66
|
+
'406' => NotAcceptable,
|
67
|
+
'407' => ProxyAuthenticationRequired,
|
68
|
+
'408' => RequestTimeout,
|
69
|
+
'409' => Conflict,
|
70
|
+
'410' => Gone,
|
71
|
+
'411' => LengthRequired,
|
72
|
+
'412' => PreconditionFailed,
|
73
|
+
'413' => PayloadTooLarge,
|
74
|
+
'414' => URITooLong,
|
75
|
+
'415' => UnsupportedMediaType,
|
76
|
+
'416' => RangeNotSatisfiable,
|
77
|
+
'417' => ExpectationFailed,
|
78
|
+
'418' => ImaTeapot,
|
79
|
+
'421' => MisdirectedRequest,
|
80
|
+
'422' => UnprocessableEntity,
|
81
|
+
'423' => Locked,
|
82
|
+
'424' => FailedDependency,
|
83
|
+
'426' => UpgradeRequired,
|
84
|
+
'428' => PreconditionRequired,
|
85
|
+
'429' => TooManyRequests,
|
86
|
+
'431' => RequestHeaderFieldsTooLarge,
|
87
|
+
'451' => UnavailableForLegalReasons,
|
88
|
+
'500' => InternalServerError,
|
89
|
+
'501' => NotImplemented,
|
90
|
+
'502' => BadGateway,
|
91
|
+
'503' => ServiceUnavailable,
|
92
|
+
'504' => GatewayTimeout,
|
93
|
+
'505' => HTTPVersionNotSupported,
|
94
|
+
'506' => VariantAlsoNegotiates,
|
95
|
+
'507' => InsufficientStorage,
|
96
|
+
'508' => LoopDetected,
|
97
|
+
'510' => NotExtended,
|
98
|
+
'511' => NetworkAuthenticationRequired
|
99
|
+
}.freeze
|
100
|
+
|
101
|
+
class ResponseHandler
|
102
|
+
def received_response(response, _options)
|
103
|
+
error = STATUS_TO_EXCEPTION_MAPPING[response.code]
|
104
|
+
raise error.new("Receieved an error response #{response.code} #{error.to_s.split('::').last}: #{response.body}", response) if error
|
105
|
+
response
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
private_constant :ResponseHandler
|
110
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'json'
|
2
|
+
require 'delegate'
|
3
|
+
|
4
|
+
module Andpush
|
5
|
+
class JsonResponse < DelegateClass(Net::HTTPResponse)
|
6
|
+
alias response __getobj__
|
7
|
+
alias headers to_hash
|
8
|
+
HAS_SYMBOL_GC = RUBY_VERSION > '2.2.0'
|
9
|
+
|
10
|
+
def json
|
11
|
+
parsable? ? JSON.parse(body, symbolize_names: HAS_SYMBOL_GC) : nil
|
12
|
+
end
|
13
|
+
|
14
|
+
def inspect
|
15
|
+
"#<JsonResponse response: #{response.inspect}, json: #{json}>"
|
16
|
+
end
|
17
|
+
alias to_s inspect
|
18
|
+
|
19
|
+
def parsable?
|
20
|
+
!!body && !body.empty?
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
class JsonSerializer
|
25
|
+
APPLICATION_JSON = 'application/json'.freeze
|
26
|
+
JSON_REQUEST_HEADERS = {
|
27
|
+
'Content-Type' => APPLICATION_JSON,
|
28
|
+
'Accept' => APPLICATION_JSON
|
29
|
+
}.freeze
|
30
|
+
|
31
|
+
def before_request(uri, body, headers, options)
|
32
|
+
headers = headers.merge(JSON_REQUEST_HEADERS)
|
33
|
+
body = body.nil? || body.is_a?(String) ? body : body.to_json
|
34
|
+
|
35
|
+
[uri, body, headers, options]
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
class JsonDeserializer
|
40
|
+
def received_response(response, _options)
|
41
|
+
JsonResponse.new(response)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
private_constant :JsonSerializer, :JsonDeserializer
|
46
|
+
end
|
data/response_types.yml
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
Response:
|
2
|
+
multicast_id: Integer
|
3
|
+
success: Integer
|
4
|
+
failure: Integer
|
5
|
+
canonical_ids: Integer
|
6
|
+
results: Array(Result)
|
7
|
+
|
8
|
+
Result:
|
9
|
+
message_id: String
|
10
|
+
registration_id: String
|
11
|
+
error: String
|
12
|
+
|
13
|
+
ErrorResponse:
|
14
|
+
operation: String
|
15
|
+
notification_key_name: String
|
16
|
+
notification_key: String
|
17
|
+
registration_ids: Array(String)
|
metadata
ADDED
@@ -0,0 +1,117 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: andpush
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Yuki Nishijima
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-05-29 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: net-http-persistent
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: minitest
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description: Andpush is an HTTP client for Firebase Cloud Messaging.
|
70
|
+
email:
|
71
|
+
- mail@yukinishijima.net
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".travis.yml"
|
78
|
+
- CODE_OF_CONDUCT.md
|
79
|
+
- Gemfile
|
80
|
+
- LICENSE.txt
|
81
|
+
- README.md
|
82
|
+
- Rakefile
|
83
|
+
- andpush.gemspec
|
84
|
+
- bin/console
|
85
|
+
- bin/setup
|
86
|
+
- fcm.apispec
|
87
|
+
- lib/andpush.rb
|
88
|
+
- lib/andpush/client.rb
|
89
|
+
- lib/andpush/exceptions.rb
|
90
|
+
- lib/andpush/json_handler.rb
|
91
|
+
- lib/andpush/version.rb
|
92
|
+
- response_types.yml
|
93
|
+
homepage: https://github.com/yuki24/andpush
|
94
|
+
licenses:
|
95
|
+
- MIT
|
96
|
+
metadata: {}
|
97
|
+
post_install_message:
|
98
|
+
rdoc_options: []
|
99
|
+
require_paths:
|
100
|
+
- lib
|
101
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
102
|
+
requirements:
|
103
|
+
- - ">="
|
104
|
+
- !ruby/object:Gem::Version
|
105
|
+
version: '0'
|
106
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
requirements: []
|
112
|
+
rubyforge_project:
|
113
|
+
rubygems_version: 2.6.12
|
114
|
+
signing_key:
|
115
|
+
specification_version: 4
|
116
|
+
summary: FCM HTTP client
|
117
|
+
test_files: []
|