firepush 0.1.1 → 0.2.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
2
  SHA256:
3
- metadata.gz: 0c8078baed1b8162b2f74d8dc1484f192956eabd0974194b77a6f4820ce43109
4
- data.tar.gz: 7f1c6a033890b6fbf0ba01628cdc048b10931a1f8a2a11754ce2db558727fe20
3
+ metadata.gz: e7d7d6a2b469b0123ac7bafea8198f40b8a739548d45ab4940ebbc16f8316170
4
+ data.tar.gz: b9d2d7d2998108930f1f120747486e8ee057adf2213b91a7f4e8ea2557248c66
5
5
  SHA512:
6
- metadata.gz: cd7f586d4e875575024bdf5906906af7f291540ec1f3f813c1bba21630ed40cdb1975f1901b7db21d1ac0ebaea81943f3602b1f260d1684de73410c6cfc7731b
7
- data.tar.gz: 2493146182414eedaf77abee51133e2a417964ea67b3649736f7ce1874feaf4251c7b4269f47bcc1ece4ab01bb5d286b1b307afc5074d4ac144acd7537072f13
6
+ metadata.gz: 6549b9cb0afb7b808dcbda164c5f257cae152af884cc94501280b39b31e3bacdcfd7edaf92530aa0edcaf1f52e1a0627e5aa55a18014c46da90304c8824710b8
7
+ data.tar.gz: 37e9c3654f5dce4b50cb02f2de10589225eb7bc768d61985c23c5d2ae05ab9357c78062623832b70f3bbeca3bceebd27840a0568eb23f6c275e56302f07eaa98
@@ -0,0 +1,6 @@
1
+ .git
2
+ .rspec_status
3
+
4
+ CODE_OF_CONDUCT.md
5
+ Dockerfile
6
+ README.md
@@ -0,0 +1,8 @@
1
+ Layout:
2
+ Enabled: false
3
+ Metrics:
4
+ Enabled: false
5
+ Rails:
6
+ Enabled: false
7
+ Style:
8
+ Enabled: false
@@ -7,9 +7,9 @@ rvm:
7
7
  - 2.4.5
8
8
  - 2.5.3
9
9
  - 2.6.1
10
- - ruby-head
11
10
  before_install: gem install bundler -v 1.17.2
12
- matrix:
13
- allow_failures: ruby-head
11
+ script:
12
+ - bundle exec rspec
13
+ - bundle exec rubocop
14
14
  notifications:
15
15
  email: false
@@ -0,0 +1,17 @@
1
+ FROM ruby:2.6.1-alpine
2
+
3
+ RUN apk add \
4
+ gcc \
5
+ git \
6
+ libc-dev \
7
+ make
8
+
9
+ WORKDIR /app
10
+
11
+ COPY firepush.gemspec /app/firepush.gemspec
12
+ COPY Gemfile /app/Gemfile
13
+ COPY lib/firepush/version.rb /app/lib/firepush/version.rb
14
+
15
+ RUN bundle install
16
+
17
+ COPY . /app
data/README.md CHANGED
@@ -9,14 +9,14 @@ Firepush is [Firebase Cloud Messaging](https://firebase.google.com/docs/cloud-me
9
9
  Add this line to your application's Gemfile:
10
10
 
11
11
  ```ruby
12
- gem 'firepush', git: "git://github.com/mmyoji/firepush.git"
12
+ gem 'firepush'
13
13
  ```
14
14
 
15
15
  And then execute:
16
16
 
17
17
  $ bundle install
18
18
 
19
- Or install it yourself as: (This doesn't work now)
19
+ Or install it yourself as:
20
20
 
21
21
  $ gem install firepush
22
22
 
@@ -35,24 +35,45 @@ client = Firepush::Client.new(
35
35
  )
36
36
 
37
37
  # 1. Notification message to a single device
38
-
39
38
  client.push(
40
39
  notification: {
41
- title: "Some title is required",
42
- body: "Some body is required",
40
+ title: "Hello",
41
+ body: "from firepush!",
43
42
  },
44
- token: "<Android Registration ID>",
43
+ token: "<client token>",
45
44
  )
46
45
 
47
46
  # 2. Data message to a topic
47
+ client.push(
48
+ data: {
49
+ key: "foo",
50
+ key2: "bar",
51
+ },
52
+ topic: "news",
53
+ )
48
54
 
55
+ # 3. You can set both Notification/Data message types
49
56
  client.push(
57
+ notification: {
58
+ title: "Hello",
59
+ body: "from firepush!",
60
+ },
50
61
  data: {
51
- key: "some String value is required",
52
- key2: "you can set multiple key-value in this section",
62
+ key: "foo",
63
+ key2: "bar",
53
64
  },
54
65
  topic: "news",
55
66
  )
67
+
68
+ # or you can set message beforehand.
69
+ client.message = {
70
+ notification: {
71
+ title: "hey"
72
+ body: "siri",
73
+ },
74
+ topic: "apple",
75
+ }
76
+ client.push
56
77
  ```
57
78
 
58
79
  ## Development
@@ -26,4 +26,5 @@ Gem::Specification.new do |spec|
26
26
  spec.add_development_dependency "bundler", "~> 1.17"
27
27
  spec.add_development_dependency "rake", "~> 10.0"
28
28
  spec.add_development_dependency "rspec", "~> 3.0"
29
+ spec.add_development_dependency "rubocop", "~> 0.64"
29
30
  end
@@ -1,8 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Firepush
4
- autoload :Client, "firepush/client"
5
- autoload :Message, "firepush/message"
4
+ autoload :Client, "firepush/client"
5
+ autoload :Message, "firepush/message"
6
+ autoload :MessageTypes, "firepush/message_types"
6
7
 
7
8
  module MessageType
8
9
  autoload :Base, "firepush/message_type/base"
@@ -12,10 +13,11 @@ module Firepush
12
13
  end
13
14
 
14
15
  module Recipient
15
- autoload :Base, "firepush/recipient/base"
16
- autoload :Builder, "firepush/recipient/builder"
17
- autoload :Token, "firepush/recipient/token"
18
- autoload :Topic, "firepush/recipient/topic"
16
+ autoload :Base, "firepush/recipient/base"
17
+ autoload :Builder, "firepush/recipient/builder"
18
+ autoload :Condition, "firepush/recipient/condition"
19
+ autoload :Token, "firepush/recipient/token"
20
+ autoload :Topic, "firepush/recipient/topic"
19
21
  end
20
22
 
21
23
  autoload :HelperMethods, "firepush/helper_methods"
@@ -31,7 +31,7 @@ module Firepush
31
31
  # @param message [Hash] optional
32
32
  #
33
33
  # TODO: Return useful response
34
- # @return [Http::Response]
34
+ # @return [Net::HTTPResponse]
35
35
  #
36
36
  # @raise [Firepush::Client::InvalidAttributes]
37
37
  def push(message = nil)
@@ -4,27 +4,29 @@ require "json"
4
4
 
5
5
  module Firepush
6
6
  class Message
7
- attr_reader :recipient # @return [Firepush::Recipient::Base]
8
- attr_reader :message_type # @return [Firepush::MessageType::Base]
7
+ attr_reader :recipient # @return [Firepush::Recipient::Base]
8
+ attr_reader :message_types # @return [Firepush::MessageTypes]
9
9
 
10
10
  # TODO: handle extra data in better way.
11
11
  attr_reader :extra # @return [Hash]
12
12
 
13
13
  # @param msg [Hash]
14
- # @see lib/firepush/{message_type,recipient}/*.rb
14
+ # @see lib/firepush/{recipient/*,message_types}.rb
15
15
  # @raise [ArgumentError]
16
16
  def initialize(msg)
17
17
  msg = msg.dup
18
18
 
19
19
  args = {}
20
- args[:topic] = msg.delete(:topic) if msg.key?(:topic)
21
- args[:token] = msg.delete(:token) if msg.key?(:token)
20
+ Recipient::TYPES.each do |type|
21
+ args[type] = msg.delete(type) if msg.key?(type)
22
+ end
22
23
  @recipient = Recipient::Builder.build(args)
23
24
 
24
25
  args.clear
25
- args[:notification] = msg.delete(:notification) if msg.key?(:notification)
26
- args[:data] = msg.delete(:data) if msg.key?(:data)
27
- @message_type = MessageType::Builder.build(args)
26
+ MessageType::TYPES.each do |type|
27
+ args[type] = msg.delete(type) if msg.key?(type)
28
+ end
29
+ @message_types = MessageTypes.new(args)
28
30
 
29
31
  @extra = msg
30
32
  end
@@ -36,7 +38,7 @@ module Firepush
36
38
 
37
39
  # @return [Boolean]
38
40
  def valid?
39
- recipient.valid? && message_type.valid?
41
+ recipient.valid? && message_types.valid?
40
42
  end
41
43
 
42
44
  private
@@ -44,10 +46,9 @@ module Firepush
44
46
  # @private
45
47
  # @return [Hash]
46
48
  def message
47
- {
48
- recipient.key => recipient.value,
49
- message_type.key => message_type.value,
50
- }.merge(extra)
49
+ message_types.message.merge({
50
+ recipient.key => recipient.value
51
+ }.merge(extra))
51
52
  end
52
53
  end
53
54
  end
@@ -2,6 +2,9 @@
2
2
 
3
3
  module Firepush
4
4
  module MessageType
5
+ # TODO: support :android, :webpush and :apns
6
+ TYPES = %i(notification data)
7
+
5
8
  class Builder
6
9
  # @param args [Hash]
7
10
  # @option args [Hash] :notification
@@ -14,8 +17,6 @@ module Firepush
14
17
  # @see .build
15
18
  def initialize(args)
16
19
  @_args = args
17
-
18
- check_args!
19
20
  end
20
21
  private_class_method :new
21
22
 
@@ -33,18 +34,6 @@ module Firepush
33
34
 
34
35
  attr_reader :_args
35
36
 
36
- # @private
37
- # @raise [ArgumentError]
38
- def check_args!
39
- if notification? && data?
40
- raise ::ArgumentError.new("Cannot set both :notification and :data")
41
- end
42
-
43
- if !notification? && !data?
44
- raise ::ArgumentError.new("Must set either :notification or :data")
45
- end
46
- end
47
-
48
37
  # @private
49
38
  # @return [Boolean]
50
39
  def data?
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Firepush
4
+ class MessageTypes
5
+ attr_reader :types # @return [Array<Firepush::MessageType::Base>]
6
+
7
+ # @param args [Hash]
8
+ # @option args [Hash] :notification
9
+ # @option args [Hash] :data
10
+ def initialize(args)
11
+ valid_types = MessageType::TYPES.reduce([]) do |acc, type|
12
+ acc.push(type => args[type]) if args.key?(type)
13
+ acc
14
+ end
15
+
16
+ @types = valid_types.map { |type| MessageType::Builder.build(type) }
17
+ end
18
+
19
+ # @return [Hash]
20
+ def message
21
+ types.reduce({}) do |acc, type|
22
+ acc[type.key] = type.value
23
+ acc
24
+ end
25
+ end
26
+
27
+ # @return [Boolean]
28
+ def valid?
29
+ types.count > 0 && types.all?(&:valid?)
30
+ end
31
+ end
32
+ end
@@ -2,10 +2,13 @@
2
2
 
3
3
  module Firepush
4
4
  module Recipient
5
+ TYPES = %i(topic token condition)
6
+
5
7
  class Builder
6
8
  # @param args [Hash]
7
9
  # @option args [Hash] :topic
8
10
  # @option args [Hash] :token
11
+ # @option args [Hash] :condition
9
12
  def self.build(args)
10
13
  new(args).build
11
14
  end
@@ -26,6 +29,8 @@ module Firepush
26
29
  Topic.new(_args.fetch(:topic))
27
30
  when token?
28
31
  Token.new(_args.fetch(:token))
32
+ when condition?
33
+ Condition.new(_args.fetch(:condition))
29
34
  end
30
35
  end
31
36
 
@@ -36,13 +41,19 @@ module Firepush
36
41
  # @private
37
42
  # @raise [ArgumentError]
38
43
  def check_args!
39
- if topic? && token?
40
- raise ::ArgumentError.new("Cannot set both :topic and :token")
44
+ count = TYPES.reduce(0) do |sum, type|
45
+ sum += 1 if _args.key?(type)
46
+ sum
41
47
  end
48
+ return if count == 1
42
49
 
43
- if !topic? && !token?
44
- raise ::ArgumentError.new("Must set either :topic or :token")
45
- end
50
+ raise ::ArgumentError.new("Have to set one of :topic, :token, or :condition")
51
+ end
52
+
53
+ # @private
54
+ # @return [Boolean]
55
+ def condition?
56
+ _args.key?(:condition)
46
57
  end
47
58
 
48
59
  # @private
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Firepush
4
+ module Recipient
5
+ class Condition < Base
6
+ attr_reader :condition # @return [String]
7
+
8
+ # @param condition [String]
9
+ def initialize(condition)
10
+ @condition = condition
11
+ end
12
+
13
+ # @override
14
+ # @return [Boolean]
15
+ # @see https://firebase.google.com/docs/cloud-messaging/send-message?hl=en
16
+ # @note Doesn't add parser for the condition string because it can be mess
17
+ def valid?
18
+ valid_str?(condition)
19
+ end
20
+
21
+ # @override
22
+ # @return [String]
23
+ def value
24
+ condition
25
+ end
26
+ end
27
+ end
28
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Firepush
4
- VERSION = "0.1.1"
4
+ VERSION = "0.2.0"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: firepush
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - mmyoji
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-02-16 00:00:00.000000000 Z
11
+ date: 2019-02-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rubocop
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.64'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.64'
55
69
  description: Firebase Cloud Messaging Client library which uses HTTP v1 API.
56
70
  email:
57
71
  - mmyoji@gmail.com
@@ -59,10 +73,13 @@ executables: []
59
73
  extensions: []
60
74
  extra_rdoc_files: []
61
75
  files:
76
+ - ".dockerignore"
62
77
  - ".gitignore"
63
78
  - ".rspec"
79
+ - ".rubocop.yml"
64
80
  - ".travis.yml"
65
81
  - CODE_OF_CONDUCT.md
82
+ - Dockerfile
66
83
  - Gemfile
67
84
  - LICENSE.txt
68
85
  - README.md
@@ -78,8 +95,10 @@ files:
78
95
  - lib/firepush/message_type/builder.rb
79
96
  - lib/firepush/message_type/data.rb
80
97
  - lib/firepush/message_type/notification.rb
98
+ - lib/firepush/message_types.rb
81
99
  - lib/firepush/recipient/base.rb
82
100
  - lib/firepush/recipient/builder.rb
101
+ - lib/firepush/recipient/condition.rb
83
102
  - lib/firepush/recipient/token.rb
84
103
  - lib/firepush/recipient/topic.rb
85
104
  - lib/firepush/version.rb