firebase-messaging 1.0.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b2457b8486e444c5c4b9d50099924150dcad1e9a
4
+ data.tar.gz: 96a73f5abb72f599609961b382b6ec59bd4a3c40
5
+ SHA512:
6
+ metadata.gz: 90596c989cefb35c40b4813df2120c620280c6009a06282b1d976648cf7f693d2501f6966ea2634a8f5f342904b5eee4204bd5124e94ceea190e0034c09de05d
7
+ data.tar.gz: 70fa965ce91e91ee5a870daed1834071b438f1eadfcb6b6895172cc3b10b2a920be92512fdb0e2dff8d1004fdf8025389f5e6bda806152dbeee0f72e66a181fa
data/.gitignore ADDED
@@ -0,0 +1,19 @@
1
+ ## PROJECT::GENERAL
2
+ coverage
3
+ rdoc
4
+ doc
5
+ log
6
+ pkg/*
7
+ tmp
8
+ .rvmrc
9
+ .ruby-version
10
+
11
+ ## BUNDLER
12
+ bin
13
+ *.gem
14
+ .bundle
15
+ Gemfile.lock
16
+ vendor/bundle
17
+
18
+ ## IDEs
19
+ .idea/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.12.5
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at nakagawa.takumi@dena.jp. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in firebase-messaging.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 tkm
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,46 @@
1
+ # Firebase::Messaging
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/firebase/messaging`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'firebase-messaging'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install firebase-messaging
22
+
23
+ ## Usage
24
+ ```ruby
25
+ Firebase::Messaging.configure do |config|
26
+ config.server_key = '***SERVER KEY***'
27
+ config.logger = Rails.logger
28
+ config.logger_level = :debug
29
+ end
30
+ ```
31
+
32
+ ## Development
33
+
34
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
35
+
36
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
37
+
38
+ ## Contributing
39
+
40
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/firebase-messaging. 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.
41
+
42
+
43
+ ## License
44
+
45
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
46
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
@@ -0,0 +1,32 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'firebase/messaging/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'firebase-messaging'
8
+ spec.version = Firebase::Messaging::VERSION
9
+ spec.authors = ['ntkm']
10
+ spec.email = ['aui.tkm@gmail.com']
11
+
12
+ spec.summary = 'Firebase Messaging Client'
13
+ spec.description = 'Remote Push Notification to iOS/Android via Firebase.'
14
+ spec.homepage = 'https://github.com/ntkm'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = 'exe'
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ['lib']
21
+
22
+ spec.add_dependency 'faraday', '~> 0.9', '>= 0.9.2'
23
+ spec.add_dependency 'faraday_middleware', '~> 0.9', '>= 0.9.2'
24
+ spec.add_dependency 'activesupport', '>= 4.0.0'
25
+
26
+ spec.add_development_dependency 'bundler'
27
+ spec.add_development_dependency 'rake'
28
+ spec.add_development_dependency 'rspec'
29
+ spec.add_development_dependency 'rspec_junit_formatter'
30
+ spec.add_development_dependency 'pry'
31
+ spec.add_development_dependency 'pry-byebug'
32
+ end
@@ -0,0 +1,35 @@
1
+ require 'faraday'
2
+ require 'faraday_middleware'
3
+ require 'firebase/messaging/version'
4
+ require 'firebase/messaging/configure'
5
+ require 'firebase/messaging/client'
6
+ require 'firebase/messaging/error'
7
+ require 'firebase/messaging/request'
8
+
9
+ module Firebase
10
+ module Messaging
11
+ # firebase settings
12
+ # Firebase::Messaging.configure do |config|
13
+ # config.server_key = "your-firebase-server-key"
14
+ # end
15
+ class << self
16
+ def configure
17
+ if block_given?
18
+ yield config
19
+ else
20
+ config
21
+ end
22
+ end
23
+
24
+ def logger
25
+ Firebase::Messaging::Logger.new(config.logger, level: config.logger_level)
26
+ end
27
+
28
+ private
29
+
30
+ def config
31
+ @config ||= Configure.new
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,23 @@
1
+ require_relative 'request'
2
+
3
+ module Firebase
4
+ module Messaging
5
+ # send firebase notification
6
+ # Firebase::Messaging::Client.new.send do |req|
7
+ # req.body.notification = {title: "title", body: "body"}
8
+ # req.body.data = {content: "abc"}
9
+ # req.priority = :high # default: "high"
10
+ # req.to = ['/topics/A', '/topics/B'] # or "/topics/A" or "fcm-token"
11
+ # end
12
+ class Client
13
+ def send
14
+ yield request
15
+ @request.post
16
+ end
17
+
18
+ def request
19
+ @request ||= Firebase::Messaging::Request.new
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,27 @@
1
+ module Firebase
2
+ module Messaging
3
+ class Configure
4
+ attr_accessor :server_key
5
+ attr_accessor :logger
6
+ attr_accessor :logger_level
7
+
8
+ def initialize
9
+ @server_key = ''
10
+ @logger = Logger.new(STDOUT)
11
+ @logger_level = :warn
12
+ end
13
+
14
+ # TODO: separete on request namespace when more headers
15
+ def headers
16
+ {
17
+ 'Authorization' => "key=#{@server_key}",
18
+ 'Content-Type' => 'application/json'
19
+ }
20
+ end
21
+
22
+ def base_url
23
+ 'https://fcm.googleapis.com/fcm/send'
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,6 @@
1
+ module Firebase
2
+ module Messaging
3
+ class Error < StandardError; end
4
+ class UnexpectedResponseError < Error; end
5
+ end
6
+ end
@@ -0,0 +1,22 @@
1
+ module Firebase
2
+ module Messaging
3
+ class Logger
4
+ attr_reader :logger, :level
5
+
6
+ LOGGER_LEVELS = [:fatal, :error, :warn, :info, :debug].freeze
7
+
8
+ def initialize(logger, level: :warn)
9
+ @logger = logger
10
+ @level = level
11
+ end
12
+
13
+ LOGGER_LEVELS.each do |method|
14
+ define_method method.to_s do |message|
15
+ if LOGGER_LEVELS.index(method) <= LOGGER_LEVELS.index(level)
16
+ logger.send(method, 'Firebase::Messaging: %s' % [message.gsub(/[\r\n]/, '')])
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,53 @@
1
+ require_relative 'request/body'
2
+ require_relative 'request/for'
3
+ require_relative 'response'
4
+ require_relative 'logger'
5
+
6
+ module Firebase
7
+ module Messaging
8
+ class Request
9
+ attr_reader :body
10
+ attr_accessor :priority
11
+
12
+ def initialize
13
+ @connector = Faraday.new(url: Firebase::Messaging.configure.base_url, headers: Firebase::Messaging.configure.headers) do |faraday|
14
+ faraday.request :url_encoded # form-encode POST params
15
+ faraday.adapter Faraday.default_adapter # make requests with Net::HTTP
16
+ faraday.response :logger # log requests to STDOUT
17
+ faraday.use :instrumentation
18
+ end
19
+ @body = Body.new
20
+ @priority = :high
21
+ end
22
+
23
+ def to=(push_for)
24
+ @for = Firebase::Messaging::Request::For.new(push_for)
25
+ end
26
+
27
+ def to
28
+ @for
29
+ end
30
+
31
+ def type
32
+ @for.type
33
+ end
34
+
35
+ def post
36
+ Firebase::Messaging.logger.debug("Request. body: #{build_body}")
37
+ res = @connector.post do |req|
38
+ req.body = build_body
39
+ end
40
+ Firebase::Messaging::Response.bind(type, status: res.status, body: res.body, headers: res.headers).tap do |response|
41
+ log_level = response.success? ? :info : :warn
42
+ Firebase::Messaging.logger.send(log_level, "Response. status: #{res.status}, body: #{res.body}")
43
+ end
44
+ end
45
+
46
+ private
47
+
48
+ def build_body
49
+ { priority: @priority }.merge(@for.payload).merge(@body.payload).to_json
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,39 @@
1
+ module Firebase
2
+ module Messaging
3
+ class Request
4
+ class Body
5
+ attr_reader :notification
6
+ attr_accessor :data
7
+
8
+ def initialize
9
+ @notification = Notification.new # NOTE: title, body, badge, icon
10
+ @data = {} # NOTE: custom data for app
11
+ end
12
+
13
+ def notification=(title: nil, body: nil, badge: nil, icon: nil)
14
+ @notification.title = title unless title.nil?
15
+ @notification.body = body unless body.nil?
16
+ @notification.badge = badge unless badge.nil?
17
+ @notification.icon = icon unless icon.nil?
18
+ end
19
+
20
+ def payload
21
+ message_payload = {}
22
+ message_payload[:notification] = @notification.payload unless @notification.payload.empty?
23
+ message_payload[:data] = @data unless @data.empty?
24
+ message_payload
25
+ end
26
+
27
+ class Notification
28
+ attr_accessor :title, :body, :badge, :icon
29
+
30
+ def payload
31
+ instance_variables.select { |key| !instance_variable_get(key).nil? }.each_with_object({}) do |key, payload|
32
+ payload[key.to_s.delete('@').to_sym] = instance_variable_get(key)
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,62 @@
1
+ module Firebase
2
+ module Messaging
3
+ class Request
4
+ class For
5
+ TOPIC_REGEX = /\A\/topics\/(.+)\Z/
6
+
7
+ attr_reader :to, :topics, :registration_ids
8
+
9
+ def initialize(directions)
10
+ if directions.is_a? String
11
+ @to = directions
12
+ elsif directions.is_a? Array
13
+ if directions.flatten.any? { |direction| TOPIC_REGEX === direction }
14
+ if directions.flatten.size == 1
15
+ @to = directions.first
16
+ else
17
+ @topics = directions
18
+ end
19
+ else
20
+ @registration_ids = directions
21
+ end
22
+ end
23
+ end
24
+
25
+ def payload
26
+ return { to: @to } if @to
27
+ return { registration_ids: @registration_ids } if @registration_ids
28
+
29
+ conditions = @topics.inject([]) do |arr, topic|
30
+ if topic.is_a? Array
31
+ arr.push "(#{or_condition(topic)})"
32
+ else
33
+ arr.push topic_condition(topic)
34
+ end
35
+ end
36
+
37
+ { condition: conditions.join(' && ') }
38
+ end
39
+
40
+ def type
41
+ if (@to && !(TOPIC_REGEX === @to)) || @registration_ids
42
+ :down_stream_http_message
43
+ else
44
+ :topic_message
45
+ end
46
+ end
47
+
48
+ private
49
+
50
+ def or_condition(or_topics)
51
+ or_topics.select { |topic| TOPIC_REGEX === topic }
52
+ .map { |topic| topic_condition(topic) }
53
+ .join(' || ')
54
+ end
55
+
56
+ def topic_condition(topic)
57
+ topic.gsub(TOPIC_REGEX, '\'\1\' in topics')
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,57 @@
1
+ require_relative 'response/down_stream_http_message'
2
+ require_relative 'response/topic_message'
3
+ require_relative 'error'
4
+ require 'json'
5
+ require 'active_support/all'
6
+
7
+ module Firebase
8
+ module Messaging
9
+ class Response
10
+ attr_reader :status, :body, :headers
11
+
12
+ def initialize(status: nil, body: nil, headers: nil)
13
+ @status = status.to_i
14
+ @body = body
15
+ @headers = headers
16
+ end
17
+
18
+ def success?
19
+ status >= 200 && status < 400
20
+ end
21
+
22
+ def failure?
23
+ !success?
24
+ end
25
+
26
+ def errors
27
+ if parsed_body.key? :error
28
+ [{ error: parsed_body[:error] }]
29
+ else
30
+ []
31
+ end
32
+ end
33
+
34
+ def parsed_body
35
+ @parsed_body ||= JSON.parse(body, symbolize_names: true)
36
+ end
37
+
38
+ class << self
39
+ def json?(json)
40
+ JSON.parse(json)
41
+ true
42
+ rescue JSON::ParserError
43
+ false
44
+ end
45
+
46
+ def bind(type, status: nil, body: nil, headers: nil)
47
+ if json?(body)
48
+ "Firebase::Messaging::Response::#{type.to_s.classify}".constantize.new(status: status, body: body, headers: headers)
49
+ else
50
+ Firebase::Messaging.logger.error("Unexpected response. status: #{status}, body: #{body}")
51
+ raise Firebase::Messaging::UnexpectedResponseError, { status: status, body: body, headers: headers }.to_s
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,23 @@
1
+ require_relative '../response'
2
+
3
+ module Firebase
4
+ module Messaging
5
+ class Response
6
+ class DownStreamHttpMessage < Firebase::Messaging::Response
7
+ def success?
8
+ super && parsed_body[:success] > 0
9
+ end
10
+
11
+ def errors
12
+ if parsed_body.key? :results
13
+ parsed_body[:results].select { |msg| msg[:error] }
14
+ elsif parsed_body.key? :failed_registration_ids
15
+ parsed_body[:failed_registration_ids].map { |id| { error: id } }
16
+ else
17
+ super
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,13 @@
1
+ require_relative '../response'
2
+
3
+ module Firebase
4
+ module Messaging
5
+ class Response
6
+ class TopicMessage < Firebase::Messaging::Response
7
+ def success?
8
+ super && !!parsed_body[:message_id]
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,5 @@
1
+ module Firebase
2
+ module Messaging
3
+ VERSION = '1.0.0'.freeze
4
+ end
5
+ end
@@ -0,0 +1,33 @@
1
+ require 'faraday'
2
+ require 'faraday_middleware'
3
+ require 'instance_id/configure'
4
+ require 'instance_id/client'
5
+ require 'instance_id/request'
6
+ require 'instance_id/request/get_info'
7
+ require 'instance_id/request/delete'
8
+
9
+ module InstanceId
10
+ # firebase settings
11
+ # Firebase::Messaging.configure do |config|
12
+ # config.server_key = "your-server-key"
13
+ # end
14
+ class << self
15
+ def configure
16
+ if block_given?
17
+ yield config
18
+ else
19
+ config
20
+ end
21
+ end
22
+
23
+ def logger
24
+ config.logger
25
+ end
26
+
27
+ private
28
+
29
+ def config
30
+ @config ||= Configure.new
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,28 @@
1
+ require_relative 'request/batch_operation'
2
+ require_relative 'request/delete'
3
+ require_relative 'request/get_info'
4
+
5
+ module InstanceId
6
+ class Client
7
+ def get_info(registration_token, including_details = false)
8
+ InstanceId::Request::GetInfo.new.tap do |request|
9
+ request.registration_token = registration_token
10
+ request.including_details = including_details
11
+ end.invoke
12
+ end
13
+
14
+ def delete(registration_token)
15
+ InstanceId::Request::Delete.new.tap do |request|
16
+ request.registration_token = registration_token
17
+ end.invoke
18
+ end
19
+
20
+ def batch_operation(operation_type, topic, registration_tokens)
21
+ InstanceId::Request::BatchOperation.new.tap do |request|
22
+ request.registration_tokens = registration_tokens
23
+ request.topic = topic
24
+ request.operation_type = operation_type
25
+ end.invoke
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,22 @@
1
+ module InstanceId
2
+ class Configure
3
+ attr_accessor :server_key
4
+ attr_accessor :logger
5
+
6
+ def initialize
7
+ @server_key = ''
8
+ @logger = Logger.new(STDOUT)
9
+ end
10
+
11
+ def headers
12
+ {
13
+ 'Authorization' => "key=#{@server_key}",
14
+ 'Content-Type' => 'application/json'
15
+ }
16
+ end
17
+
18
+ def base_url
19
+ 'https://iid.googleapis.com'
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,41 @@
1
+ module InstanceId
2
+ class Request
3
+ def initialize(*args, **options)
4
+ yield(connector) if block_given?
5
+ end
6
+
7
+ def invoke
8
+ response = connector.send(method, endpoint) do |request|
9
+ yield(request) if block_given?
10
+ end
11
+ parse response
12
+ end
13
+
14
+ def endpoint
15
+ raise NotImplementedError, "concrete classes should implement #{__method__}."
16
+ end
17
+
18
+ def method
19
+ raise NotImplementedError, "concrete classes should implement #{__method__}, returning one of #{%i(get post put patch delete)}."
20
+ end
21
+
22
+ def response_class
23
+ raise NotImplementedError, "concrete classes should implement #{__method__}, returning concrete class of InstanceId::Response"
24
+ end
25
+
26
+ private
27
+
28
+ def parse(response)
29
+ response_class.new(status: response.status, body: response.body, headers: response.headers)
30
+ end
31
+
32
+ def connector
33
+ @connector ||= Faraday.new(url: InstanceId.configure.base_url, headers: InstanceId.configure.headers) do |faraday|
34
+ faraday.request :url_encoded # form-encode POST params
35
+ faraday.adapter Faraday.default_adapter # make requests with Net::HTTP
36
+ faraday.response :logger # log requests to STDOUT
37
+ faraday.use :instrumentation
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,49 @@
1
+ require_relative '../request'
2
+ require_relative '../response/batch_operation'
3
+
4
+ module InstanceId
5
+ class Request
6
+ class BatchOperation < self
7
+ attr_accessor :operation_type
8
+ attr_accessor :topic
9
+ attr_accessor :registration_tokens
10
+
11
+ OPERATION_TYPE_ADD = 'add'.freeze
12
+ OPERATION_TYPE_REMOVE = 'remove'.freeze
13
+
14
+ def method
15
+ :post
16
+ end
17
+
18
+ def endpoint
19
+ "/iid/v1:#{operation_name}"
20
+ end
21
+
22
+ def response_class
23
+ InstanceId::Response::BatchOperation
24
+ end
25
+
26
+ def invoke
27
+ super do |request|
28
+ request.params['to'] = to
29
+ request.params["registration_tokens"] = [registration_tokens].flatten.compact
30
+ end
31
+ end
32
+
33
+ private
34
+
35
+ def to
36
+ raise "invalid topic format: #{topic}" unless /\A\/topics\/.+\z/ === topic
37
+ topic
38
+ end
39
+
40
+ def operation_name
41
+ case operation_type.to_s.downcase
42
+ when OPERATION_TYPE_ADD then 'batchAdd'
43
+ when OPERATION_TYPE_REMOVE then 'batchRemove'
44
+ else raise "operation not specified, operation_type = #{operation_type}"
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,26 @@
1
+ require_relative '../response/delete'
2
+
3
+ module InstanceId
4
+ class Request
5
+ class Delete < self
6
+ attr_accessor :registration_token
7
+
8
+ def endpoint
9
+ "/v1/web/iid/#{registration_token}"
10
+ end
11
+
12
+ def method
13
+ :delete
14
+ end
15
+
16
+ def response_class
17
+ InstanceId::Response::Delete
18
+ end
19
+
20
+ def invoke
21
+ raise "invalid token! #{registration_token}" unless /\A.+\z/ === registration_token
22
+ super
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,32 @@
1
+ require_relative '../request'
2
+ require_relative '../response/get_info'
3
+
4
+ module InstanceId
5
+ class Request
6
+ class GetInfo < self
7
+ attr_accessor :including_details
8
+ attr_accessor :registration_token
9
+
10
+ alias_method :including_details?, :including_details
11
+
12
+ def invoke
13
+ raise "invalid registration_token! #{registration_token}" unless /\A.+\z/ === registration_token
14
+ super do |request|
15
+ request.params['details'] = !!including_details ? 'true' : 'false'
16
+ end
17
+ end
18
+
19
+ def endpoint
20
+ "/iid/info/#{registration_token}"
21
+ end
22
+
23
+ def method
24
+ :get
25
+ end
26
+
27
+ def response_class
28
+ InstanceId::Response::GetInfo
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,44 @@
1
+ require 'json'
2
+ require 'active_support/all'
3
+
4
+ module InstanceId
5
+ class Response
6
+ attr_reader :status
7
+ attr_reader :body
8
+ attr_reader :headers
9
+
10
+ def initialize(status:, body: nil, headers: nil)
11
+ @status = status.to_i
12
+ @body = body
13
+ @headers = headers
14
+ end
15
+
16
+ def success?
17
+ @status.in? 200...400
18
+ end
19
+
20
+ def failure?
21
+ !success?
22
+ end
23
+
24
+ def body_json
25
+ parse_body.tap do |json|
26
+ break json.key?(:error) ? nil : json
27
+ end
28
+ end
29
+
30
+ def errors
31
+ parse_body.tap do |json|
32
+ break json.key?(:error) ? [json.slice(:error)] : []
33
+ end
34
+ end
35
+
36
+ private
37
+
38
+ def parse_body
39
+ JSON.parse(@body, symbolize_names: true)
40
+ rescue JSON::ParserError => e
41
+ { error: e.class.to_s }
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,7 @@
1
+ require_relative '../response'
2
+
3
+ module InstanceId
4
+ class Response
5
+ class BatchOperation < self; end
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ require_relative '../response'
2
+
3
+ module InstanceId
4
+ class Response
5
+ class Delete < self; end
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ require_relative '../response'
2
+
3
+ module InstanceId
4
+ class Response
5
+ class GetInfo < self; end
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,214 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: firebase-messaging
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - ntkm
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-10-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.9'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 0.9.2
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '0.9'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 0.9.2
33
+ - !ruby/object:Gem::Dependency
34
+ name: faraday_middleware
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '0.9'
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: 0.9.2
43
+ type: :runtime
44
+ prerelease: false
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - "~>"
48
+ - !ruby/object:Gem::Version
49
+ version: '0.9'
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: 0.9.2
53
+ - !ruby/object:Gem::Dependency
54
+ name: activesupport
55
+ requirement: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: 4.0.0
60
+ type: :runtime
61
+ prerelease: false
62
+ version_requirements: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: 4.0.0
67
+ - !ruby/object:Gem::Dependency
68
+ name: bundler
69
+ requirement: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ type: :development
75
+ prerelease: false
76
+ version_requirements: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ - !ruby/object:Gem::Dependency
82
+ name: rake
83
+ requirement: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ type: :development
89
+ prerelease: false
90
+ version_requirements: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ - !ruby/object:Gem::Dependency
96
+ name: rspec
97
+ requirement: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ - !ruby/object:Gem::Dependency
110
+ name: rspec_junit_formatter
111
+ requirement: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ type: :development
117
+ prerelease: false
118
+ version_requirements: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - ">="
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ - !ruby/object:Gem::Dependency
124
+ name: pry
125
+ requirement: !ruby/object:Gem::Requirement
126
+ requirements:
127
+ - - ">="
128
+ - !ruby/object:Gem::Version
129
+ version: '0'
130
+ type: :development
131
+ prerelease: false
132
+ version_requirements: !ruby/object:Gem::Requirement
133
+ requirements:
134
+ - - ">="
135
+ - !ruby/object:Gem::Version
136
+ version: '0'
137
+ - !ruby/object:Gem::Dependency
138
+ name: pry-byebug
139
+ requirement: !ruby/object:Gem::Requirement
140
+ requirements:
141
+ - - ">="
142
+ - !ruby/object:Gem::Version
143
+ version: '0'
144
+ type: :development
145
+ prerelease: false
146
+ version_requirements: !ruby/object:Gem::Requirement
147
+ requirements:
148
+ - - ">="
149
+ - !ruby/object:Gem::Version
150
+ version: '0'
151
+ description: Remote Push Notification to iOS/Android via Firebase.
152
+ email:
153
+ - aui.tkm@gmail.com
154
+ executables: []
155
+ extensions: []
156
+ extra_rdoc_files: []
157
+ files:
158
+ - ".gitignore"
159
+ - ".rspec"
160
+ - ".travis.yml"
161
+ - CODE_OF_CONDUCT.md
162
+ - Gemfile
163
+ - LICENSE.txt
164
+ - README.md
165
+ - Rakefile
166
+ - firebase-messaging.gemspec
167
+ - lib/firebase/messaging.rb
168
+ - lib/firebase/messaging/client.rb
169
+ - lib/firebase/messaging/configure.rb
170
+ - lib/firebase/messaging/error.rb
171
+ - lib/firebase/messaging/logger.rb
172
+ - lib/firebase/messaging/request.rb
173
+ - lib/firebase/messaging/request/body.rb
174
+ - lib/firebase/messaging/request/for.rb
175
+ - lib/firebase/messaging/response.rb
176
+ - lib/firebase/messaging/response/down_stream_http_message.rb
177
+ - lib/firebase/messaging/response/topic_message.rb
178
+ - lib/firebase/messaging/version.rb
179
+ - lib/instance_id.rb
180
+ - lib/instance_id/client.rb
181
+ - lib/instance_id/configure.rb
182
+ - lib/instance_id/request.rb
183
+ - lib/instance_id/request/batch_operation.rb
184
+ - lib/instance_id/request/delete.rb
185
+ - lib/instance_id/request/get_info.rb
186
+ - lib/instance_id/response.rb
187
+ - lib/instance_id/response/batch_operation.rb
188
+ - lib/instance_id/response/delete.rb
189
+ - lib/instance_id/response/get_info.rb
190
+ homepage: https://github.com/ntkm
191
+ licenses:
192
+ - MIT
193
+ metadata: {}
194
+ post_install_message:
195
+ rdoc_options: []
196
+ require_paths:
197
+ - lib
198
+ required_ruby_version: !ruby/object:Gem::Requirement
199
+ requirements:
200
+ - - ">="
201
+ - !ruby/object:Gem::Version
202
+ version: '0'
203
+ required_rubygems_version: !ruby/object:Gem::Requirement
204
+ requirements:
205
+ - - ">="
206
+ - !ruby/object:Gem::Version
207
+ version: '0'
208
+ requirements: []
209
+ rubyforge_project:
210
+ rubygems_version: 2.6.11
211
+ signing_key:
212
+ specification_version: 4
213
+ summary: Firebase Messaging Client
214
+ test_files: []