courier-notifications 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5bb95c354f6cd88b09abb11e744567f9a9f5b895
4
+ data.tar.gz: e496939b785f81bcb1d09e4eec189c8d104c2d1d
5
+ SHA512:
6
+ metadata.gz: 68777203feaf4e4274068206baf9f95be56ed461d491a1be40215ab10b742a8d08c7514fc070abe67dd6f03420be7294cd055d07e80ee1504a10a0acda875e68
7
+ data.tar.gz: 93ef5b42dbd039e7f6062b6d550233d286b175727b9cc461362a9ab654ba76e847c510cc91b1b18fb8c5608a134906f249fce952c71e16107ef35f10a18ffeac
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1 @@
1
+ 2.3.0
@@ -0,0 +1,3 @@
1
+ --markup markdown
2
+ --private
3
+ --protected
@@ -0,0 +1,20 @@
1
+ We love pull requests from everyone. Follow the thoughtbot [code of conduct] while contributing.
2
+
3
+ [code of conduct]: https://thoughtbot.com/open-source-code-of-conduct
4
+
5
+ ## Contributing
6
+
7
+ 1. Fork the repo.
8
+ 2. Run the tests. We only take pull requests with passing tests, and it's great to know that you have a clean slate.
9
+ 3. Add a test for your change. Only refactoring and documentation changes require no new tests. If you are adding functionality or fixing a bug, we need a test.
10
+ 4. Make the test pass.
11
+ 5. Push to your fork and submit a pull request.
12
+
13
+ At this point you're waiting on us. We like to at least comment on, if not accept, pull requests within three business days (and, typically, one business day). We may suggest some changes or improvements or alternatives.
14
+
15
+ Some things that will increase the chance that your pull request is accepted,
16
+
17
+ * Include tests that fail without your code, and pass with it
18
+ * Update the documentation, the surrounding one, examples elsewhere, guides, whatever is affected by your contribution
19
+ * Follow the existing style of the project
20
+
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 thoughtbot, inc.
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.
@@ -0,0 +1,77 @@
1
+ # Courier
2
+
3
+ Ruby gem for interacting with the [Courier] API.
4
+
5
+ [Courier]: https://courier.thoughtbot.com
6
+
7
+ [![Build Status](https://circleci.com/gh/thoughtbot/courier-gem.svg?style=shield&circle-token=b09feb2f03dbc8fa7aa16d1977da3771b47c675c)](https://circleci.com/gh/thoughtbot/courier-gem)
8
+ [![Code Climate](https://codeclimate.com/repos/572a0503b781dc24b0000564/badges/db6744d185fd35deb8da/gpa.svg)](https://codeclimate.com/repos/572a0503b781dc24b0000564/feed)
9
+ [![Inline docs](http://inch-ci.org/github/thoughtbot/courier-gem.svg?branch=master)](http://inch-ci.org/github/thoughtbot/courier-gem)
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem 'courier-notifications'
17
+ ```
18
+
19
+ And then execute:
20
+
21
+ $ bundle
22
+
23
+ Or install it yourself as:
24
+
25
+ $ gem install courier-notifications
26
+
27
+ ## Usage
28
+
29
+ Instantiate a Courier instance with your app's API token and an environment:
30
+
31
+ ```ruby
32
+ courier = Courier::Client.new(api_token: "[YOUR_API_TOKEN]", environment: :development)
33
+ ```
34
+
35
+ For the environment choose `:development` if you're sending notifications to a development build of an app. If you're sending notifications to an app signed with a distribution certificate (TestFlight, HockeyApp, AppStore, etc) use `:production`.
36
+
37
+ Broadcast a notification to a channel:
38
+
39
+ ```ruby
40
+ broadcast = courier.broadcast("[CHANNEL_NAME]", alert: "Hello from Courier")
41
+
42
+ if broadcast.sent?
43
+ # How do you handle success?
44
+ else
45
+ # Interpret broadcast.status_code
46
+ end
47
+ ```
48
+
49
+ Courier will send anything after the channel name argument as the push notification payload. See Apple's [remote notification payload documentation] for more information.
50
+
51
+ [remote notification payload documentation]: https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/TheNotificationPayload.html
52
+
53
+ Note that delivery of notifications is a “best effort”. It is possible for `broadcast.sent?` to return true, but no notification to be delivered.
54
+
55
+ ## Development
56
+
57
+ 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.
58
+
59
+ ## Contributing
60
+
61
+ Please see [CONTRIBUTING.md](/CONTRIBUTING.md).
62
+
63
+ ## License
64
+
65
+ Courier is Copyright © 2016 thoughtbot. It is free software, and may be redistributed under the terms specified in the [LICENSE](/LICENSE) file.
66
+
67
+ ## About thoughtbot
68
+
69
+ ![thoughtbot](https://thoughtbot.com/logo.png)
70
+
71
+ Courier is maintained and funded by thoughtbot, inc.
72
+ The names and logos for thoughtbot are trademarks of thoughtbot, inc.
73
+
74
+ We love open source software! See [our other projects][community] or [hire us][hire] to design, develop, and grow your product.
75
+
76
+ [community]: https://thoughtbot.com/community?utm_source=github
77
+ [hire]: https://thoughtbot.com?utm_source=github
@@ -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,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "courier"
5
+
6
+ require "pry"
7
+ Pry.start
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'courier/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "courier-notifications"
8
+ spec.version = Courier::VERSION
9
+ spec.authors = ["Klaas Pieter Annema"]
10
+ spec.email = ["support@thoughtbot.com"]
11
+
12
+ spec.summary = %q{Interact with the Courier API}
13
+ spec.description = %q{Easy push notifications}
14
+ spec.homepage = "https://courier.thoughtbot.com"
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
+ spec.required_ruby_version = "~> 2.2"
22
+
23
+ spec.add_dependency "faraday_middleware", "~> 0.8"
24
+ spec.add_dependency "activesupport", "~> 4.2"
25
+
26
+ spec.add_development_dependency "bundler", "~> 1.9"
27
+ spec.add_development_dependency "rake", "~> 10.0"
28
+ spec.add_development_dependency "rspec", "~> 3.0"
29
+ spec.add_development_dependency "pry", "~> 0.10"
30
+ spec.add_development_dependency "webmock", "~> 1.24"
31
+ end
@@ -0,0 +1,10 @@
1
+ require "courier/version"
2
+ require "courier/client"
3
+ require "courier/broadcast"
4
+
5
+ # Courier is a Ruby client for interacting with the [Courier web
6
+ # service](https://courier.thoughtbot.com).
7
+ #
8
+ # See the {file:README.md} for usage instructions.
9
+ module Courier
10
+ end
@@ -0,0 +1,63 @@
1
+ module Courier
2
+
3
+ # A Broadcast instance reflects the state of a broadcast to a channel.
4
+ #
5
+ # Use `status_code` and `sent?` to determine the state of a broadcast.
6
+ #
7
+ # You do not create Broadcast instances directly. Instead, a Client creates
8
+ # them for you to reflect the state of a broadcast it sent.
9
+ class Broadcast
10
+
11
+ # @return [Fixnum] HTTP status code of the request.
12
+ attr_reader :status_code
13
+
14
+ # Create a Broadcast for a channel, payload and status code.
15
+ #
16
+ # @param [String] channel (see #channel)
17
+ #
18
+ # @param {String: String, Hash, Array} payload (see #payload)
19
+ #
20
+ # @param (Fixnum) status_code (see #status_code)
21
+ #
22
+ # @!visibility private
23
+ def initialize(channel:, payload:, status_code:)
24
+ @channel = channel
25
+ @payload = payload
26
+ @status_code = status_code
27
+ end
28
+
29
+ # Wether the broadcast was sent successfuly or not.
30
+ # Note that delivery of notifications is a “best effort”. It is possible for
31
+ # broadcast.sent? to return true, but no notification to be delivered.
32
+ #
33
+ # @return [Bool]
34
+ # true if the broadcast was sent to Courier, false otherwise.
35
+ def sent?
36
+ (200..299).cover?(status_code)
37
+ end
38
+
39
+ # Compare two Broadcast instance. Broadcasts are considered equal if their
40
+ # `class`, `channel`, `payload` and `status_code` are equal.
41
+ #
42
+ # @param [Object] other
43
+ # the object to be compare to the receiver.
44
+ #
45
+ # @return [Bool]
46
+ # true if the receiver and `other` are equal, otherwise false.
47
+ def ==(other)
48
+ self.class == other.class &&
49
+ channel == other.channel &&
50
+ payload == other.payload &&
51
+ status_code == other.status_code
52
+ end
53
+
54
+ protected
55
+
56
+ # @return [String] channel the broadcast was sent too.
57
+ attr_reader :channel
58
+
59
+ # @return {String: String, Hash, Array}
60
+ # payload that was sent to the channel.
61
+ attr_reader :payload
62
+ end
63
+ end
@@ -0,0 +1,88 @@
1
+ require "faraday_middleware"
2
+ require "active_support/inflector"
3
+
4
+ module Courier
5
+
6
+ # Client object manages authenticating and communicating with the Courier API.
7
+ class Client
8
+
9
+ # The Courier API base URL.
10
+ # @private_constant
11
+ DEFAULT_BASE_URL = "https://courier.thoughtbot.com".freeze
12
+
13
+ # Create a Client configured with an app's `api_token` and an `environment`.
14
+ #
15
+ # @param [String] api_token
16
+ # The app's API token for authenticating with the Courier API.
17
+ #
18
+ # @param [:development, :production] environment
19
+ # environment to use when communicating with the Courier API. Use
20
+ # `:development` when interacting with an app signed with a
21
+ # development certificate. Use `production` when an app is signed
22
+ # with a distribution certificate.
23
+ #
24
+ # @param [String] base_url
25
+ # The base_url to connect to. Primarily used for testing purposes.
26
+ # You should not have to change this.
27
+ #
28
+ # Example
29
+ #
30
+ # Courier::Client.new(api_token: "[YOUR API_TOKEN]", environment:
31
+ # :development)
32
+ #
33
+ # @return [Client] A new Client instance
34
+ def initialize(api_token:, environment:, base_url: DEFAULT_BASE_URL)
35
+ @api_token = api_token
36
+ @base_url = base_url
37
+ @environment = environment
38
+ end
39
+
40
+ # Send a notification to all devices registered with a channel
41
+ #
42
+ # @param [String] channel
43
+ # name of the channel. Names are automatically parameterized
44
+ # using
45
+ # [ActiveSupport::Inflector](http://apidock.com/rails/ActiveSupport/Inflector).
46
+ #
47
+ # @param {String: String, Hash, Array} payload
48
+ # The
49
+ # [APNs](https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/TheNotificationPayload.html#//apple_ref/doc/uid/TP40008194-CH107-SW1)
50
+ # payload to broadcast.
51
+ # See [lowdown](http://www.rubydoc.info/gems/lowdown/Lowdown%2FNotification%3Aformatted_payload)
52
+ # documentation for more information on how the payload is processed.
53
+ #
54
+ # @return [Broadcast] a broadcast instance.
55
+ #
56
+ # @see http://apidock.com/rails/ActiveSupport/Inflector/parameterize
57
+ # @see http://www.rubydoc.info/gems/lowdown/Lowdown%2FNotification%3Aformatted_payload
58
+ # @see https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/TheNotificationPayload.html#//apple_ref/doc/uid/TP40008194-CH107-SW1
59
+ #
60
+ def broadcast(channel, payload)
61
+ response = http.post("/broadcast/#{channel.parameterize}",
62
+ broadcast: { payload: payload })
63
+ Broadcast.new(channel: channel,
64
+ payload: payload,
65
+ status_code: response.status)
66
+ end
67
+
68
+ private
69
+
70
+ attr_reader :api_token, :base_url, :environment
71
+
72
+ # Make authenticated requests to the Courier API.
73
+ #
74
+ # @return [Faraday::Connection]
75
+ # a Faraday::Connection configured to authenticate and communicate
76
+ # with the Courier API.
77
+ def http
78
+ Faraday.new(url: base_url) do |conn|
79
+ conn.headers["Accept"] = "application/json version=1"
80
+ conn.headers["Content-Type"] = "application/json"
81
+ conn.request :json
82
+ conn.params["environment"] = environment
83
+ conn.token_auth api_token
84
+ conn.adapter Faraday.default_adapter
85
+ end
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,3 @@
1
+ module Courier
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,159 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: courier-notifications
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Klaas Pieter Annema
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-06-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday_middleware
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.8'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.8'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '4.2'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '4.2'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.9'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.9'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '0.10'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '0.10'
97
+ - !ruby/object:Gem::Dependency
98
+ name: webmock
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '1.24'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '1.24'
111
+ description: Easy push notifications
112
+ email:
113
+ - support@thoughtbot.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - ".rspec"
120
+ - ".ruby-version"
121
+ - ".yardopts"
122
+ - CONTRIBUTING.md
123
+ - Gemfile
124
+ - LICENSE
125
+ - README.md
126
+ - Rakefile
127
+ - bin/console
128
+ - bin/setup
129
+ - courier-notifications.gemspec
130
+ - lib/courier.rb
131
+ - lib/courier/broadcast.rb
132
+ - lib/courier/client.rb
133
+ - lib/courier/version.rb
134
+ homepage: https://courier.thoughtbot.com
135
+ licenses:
136
+ - MIT
137
+ metadata: {}
138
+ post_install_message:
139
+ rdoc_options: []
140
+ require_paths:
141
+ - lib
142
+ required_ruby_version: !ruby/object:Gem::Requirement
143
+ requirements:
144
+ - - "~>"
145
+ - !ruby/object:Gem::Version
146
+ version: '2.2'
147
+ required_rubygems_version: !ruby/object:Gem::Requirement
148
+ requirements:
149
+ - - ">="
150
+ - !ruby/object:Gem::Version
151
+ version: '0'
152
+ requirements: []
153
+ rubyforge_project:
154
+ rubygems_version: 2.6.4
155
+ signing_key:
156
+ specification_version: 4
157
+ summary: Interact with the Courier API
158
+ test_files: []
159
+ has_rdoc: