eventflit 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/.document +5 -0
- data/.gemtest +0 -0
- data/.gitignore +24 -0
- data/.travis.yml +16 -0
- data/CHANGELOG.md +0 -0
- data/Gemfile +2 -0
- data/LICENSE +20 -0
- data/README.md +301 -0
- data/Rakefile +11 -0
- data/eventflit.gemspec +33 -0
- data/examples/async_message.rb +28 -0
- data/lib/eventflit.rb +69 -0
- data/lib/eventflit/channel.rb +185 -0
- data/lib/eventflit/client.rb +437 -0
- data/lib/eventflit/native_notification/client.rb +68 -0
- data/lib/eventflit/request.rb +109 -0
- data/lib/eventflit/resource.rb +36 -0
- data/lib/eventflit/version.rb +3 -0
- data/lib/eventflit/webhook.rb +110 -0
- data/spec/channel_spec.rb +170 -0
- data/spec/client_spec.rb +629 -0
- data/spec/spec_helper.rb +26 -0
- data/spec/web_hook_spec.rb +117 -0
- metadata +207 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 42f56605698bfed7634edb039c6c7ab0b796fc51
|
4
|
+
data.tar.gz: 1ad29c30ce81381f82aa204a02a76da5199f5721
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ef09453355ae7d3a4fc6b19b3c1ccbcf3c30245b054a7df6059b3a8212064fb36c14452b7489f5a82f29caecd71089ce63a17c477f7235a2c577de315f6e09bf
|
7
|
+
data.tar.gz: a93e23180345583cf66a412cfd4558b4ff4f7f259e7740d0d303d8bb5d596ce8beb788004d98be3fbc51e2f761c4a492948c5b191538da8f0c1c3baa2776cc05
|
data/.document
ADDED
data/.gemtest
ADDED
File without changes
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/CHANGELOG.md
ADDED
File without changes
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2010-2013 Pusher
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,301 @@
|
|
1
|
+
Eventflit gem
|
2
|
+
==========
|
3
|
+
|
4
|
+
## Installation & Configuration
|
5
|
+
|
6
|
+
Add eventflit to your Gemfile, and then run `bundle install`
|
7
|
+
|
8
|
+
``` ruby
|
9
|
+
gem 'eventflit'
|
10
|
+
```
|
11
|
+
|
12
|
+
or install via gem
|
13
|
+
|
14
|
+
``` bash
|
15
|
+
gem install eventflit
|
16
|
+
```
|
17
|
+
|
18
|
+
After registering at <http://eventflit.com> configure your app with the security credentials.
|
19
|
+
|
20
|
+
### Instantiating a Eventflit client
|
21
|
+
|
22
|
+
Creating a new Eventflit `client` can be done as follows.
|
23
|
+
|
24
|
+
``` ruby
|
25
|
+
require 'eventflit'
|
26
|
+
|
27
|
+
eventflit_client = Eventflit::Client.new(
|
28
|
+
app_id: 'your-app-id',
|
29
|
+
key: 'your-app-key',
|
30
|
+
secret: 'your-app-secret',
|
31
|
+
cluster: 'your-app-cluster',
|
32
|
+
)
|
33
|
+
```
|
34
|
+
The cluster value will set the `host` to `api-<cluster>.eventflit.com`.
|
35
|
+
|
36
|
+
If you want to set a custom `host` value for your client then you can do so when instantiating a Eventflit client like so:
|
37
|
+
|
38
|
+
``` ruby
|
39
|
+
require 'eventflit'
|
40
|
+
|
41
|
+
eventflit_client = Eventflit::Client.new(
|
42
|
+
app_id: 'your-app-id',
|
43
|
+
key: 'your-app-key',
|
44
|
+
secret: 'your-app-secret',
|
45
|
+
host: 'your-app-host'
|
46
|
+
)
|
47
|
+
```
|
48
|
+
|
49
|
+
If you pass both `host` and `cluster` options, the `host` will take precendence and `cluster` will be ignored.
|
50
|
+
|
51
|
+
|
52
|
+
Finally, if you have the configuration set in an `EVENTFLIT_URL` environment
|
53
|
+
variable, you can use:
|
54
|
+
|
55
|
+
``` ruby
|
56
|
+
eventflit_client = Eventflit::Client.from_env
|
57
|
+
```
|
58
|
+
|
59
|
+
### Global
|
60
|
+
|
61
|
+
Configuring Eventflit can also be done globally on the Eventflit class.
|
62
|
+
|
63
|
+
``` ruby
|
64
|
+
Eventflit.app_id = 'your-app-id'
|
65
|
+
Eventflit.key = 'your-app-key'
|
66
|
+
Eventflit.secret = 'your-app-secret'
|
67
|
+
Eventflit.cluster = 'your-app-cluster'
|
68
|
+
```
|
69
|
+
|
70
|
+
Global configuration will automatically be set from the `EVENTFLIT_URL` environment variable if it exists. This should be in the form `http://KEY:SECRET@HOST/apps/APP_ID`. On Heroku this environment variable will already be set.
|
71
|
+
|
72
|
+
If you need to make requests via a HTTP proxy then it can be configured
|
73
|
+
|
74
|
+
``` ruby
|
75
|
+
Eventflit.http_proxy = 'http://(user):(password)@(host):(port)'
|
76
|
+
```
|
77
|
+
|
78
|
+
By default API requests are made over HTTP. HTTPS can be used by setting `encrypted` to `true`.
|
79
|
+
Issuing this command is going to reset `port` value if it was previously specified.
|
80
|
+
|
81
|
+
``` ruby
|
82
|
+
Eventflit.encrypted = true
|
83
|
+
```
|
84
|
+
|
85
|
+
As of version 0.12, SSL certificates are verified when using the synchronous http client. If you need to disable this behaviour for any reason use:
|
86
|
+
|
87
|
+
``` ruby
|
88
|
+
Eventflit.default_client.sync_http_client.ssl_config.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
89
|
+
```
|
90
|
+
|
91
|
+
## Interacting with the Eventflit service
|
92
|
+
|
93
|
+
The Eventflit gem contains a number of helpers for interacting with the service. As a general rule, the library adheres to a set of conventions that we have aimed to make universal.
|
94
|
+
|
95
|
+
### Handling errors
|
96
|
+
|
97
|
+
Handle errors by rescuing `Eventflit::Error` (all errors are descendants of this error)
|
98
|
+
|
99
|
+
``` ruby
|
100
|
+
begin
|
101
|
+
Eventflit.trigger('a_channel', 'an_event', :some => 'data')
|
102
|
+
rescue Eventflit::Error => e
|
103
|
+
# (Eventflit::AuthenticationError, Eventflit::HTTPError, or Eventflit::Error)
|
104
|
+
end
|
105
|
+
```
|
106
|
+
|
107
|
+
### Logging
|
108
|
+
|
109
|
+
Errors are logged to `Eventflit.logger`. It will by default log at info level to STDOUT using `Logger` from the standard library, however you can assign any logger:
|
110
|
+
|
111
|
+
``` ruby
|
112
|
+
Eventflit.logger = Rails.logger
|
113
|
+
```
|
114
|
+
|
115
|
+
### Publishing events
|
116
|
+
|
117
|
+
An event can be published to one or more channels (limited to 10) in one API call:
|
118
|
+
|
119
|
+
``` ruby
|
120
|
+
Eventflit.trigger('channel', 'event', foo: 'bar')
|
121
|
+
Eventflit.trigger(['channel_1', 'channel_2'], 'event_name', foo: 'bar')
|
122
|
+
```
|
123
|
+
|
124
|
+
An optional fourth argument may be used to send additional parameters to the API, for example to [exclude a single connection from receiving the event](http://docs.eventflit.com/publisher_api_guide/publisher_excluding_recipients).
|
125
|
+
|
126
|
+
``` ruby
|
127
|
+
Eventflit.trigger('channel', 'event', {foo: 'bar'}, {socket_id: '123.456'})
|
128
|
+
```
|
129
|
+
|
130
|
+
#### Batches
|
131
|
+
|
132
|
+
It's also possible to send multiple events with a single API call (max 10
|
133
|
+
events per call on multi-tenant clusters):
|
134
|
+
|
135
|
+
``` ruby
|
136
|
+
Eventflit.trigger_batch([
|
137
|
+
{channel: 'channel_1', name: 'event_name', data: { foo: 'bar' }},
|
138
|
+
{channel: 'channel_1', name: 'event_name', data: { hello: 'world' }}
|
139
|
+
])
|
140
|
+
```
|
141
|
+
|
142
|
+
#### Deprecated publisher API
|
143
|
+
|
144
|
+
Most examples and documentation will refer to the following syntax for triggering an event:
|
145
|
+
|
146
|
+
``` ruby
|
147
|
+
Eventflit['a_channel'].trigger('an_event', :some => 'data')
|
148
|
+
```
|
149
|
+
|
150
|
+
This will continue to work, but has been replaced by `Eventflit.trigger` which supports one or multiple channels.
|
151
|
+
|
152
|
+
### Using the Eventflit REST API
|
153
|
+
|
154
|
+
This gem provides methods for accessing information from the [Eventflit REST API](https://docs.eventflit.com/rest_api). The documentation also shows an example of the responses from each of the API endpoints.
|
155
|
+
|
156
|
+
The following methods are provided by the gem.
|
157
|
+
|
158
|
+
- `Eventflit.channel_info('channel_name')` returns information about that channel.
|
159
|
+
|
160
|
+
- `Eventflit.channel_users('channel_name')` returns a list of all the users subscribed to the channel.
|
161
|
+
|
162
|
+
- `Eventflit.channels` returns information about all the channels in your Eventflit application.
|
163
|
+
|
164
|
+
### Asynchronous requests
|
165
|
+
|
166
|
+
There are two main reasons for using the `_async` methods:
|
167
|
+
|
168
|
+
* In a web application where the response from Eventflit is not used, but you'd like to avoid a blocking call in the request-response cycle
|
169
|
+
* Your application is running in an event loop and you need to avoid blocking the reactor
|
170
|
+
|
171
|
+
Asynchronous calls are supported either by using an event loop (eventmachine, preferred), or via a thread.
|
172
|
+
|
173
|
+
The following methods are available (in each case the calling interface matches the non-async version):
|
174
|
+
|
175
|
+
* `Eventflit.get_async`
|
176
|
+
* `Eventflit.post_async`
|
177
|
+
* `Eventflit.trigger_async`
|
178
|
+
|
179
|
+
It is of course also possible to make calls to eventflit via a job queue. This approach is recommended if you're sending a large number of events to eventflit.
|
180
|
+
|
181
|
+
#### With eventmachine
|
182
|
+
|
183
|
+
* Add the `em-http-request` gem to your Gemfile (it's not a gem dependency).
|
184
|
+
* Run the eventmachine reactor (either using `EM.run` or by running inside an evented server such as Thin).
|
185
|
+
|
186
|
+
The `_async` methods return an `EM::Deferrable` which you can bind callbacks to:
|
187
|
+
|
188
|
+
``` ruby
|
189
|
+
Eventflit.get_async("/channels").callback { |response|
|
190
|
+
# use response[:channels]
|
191
|
+
}.errback { |error|
|
192
|
+
# error is an instance of Eventflit::Error
|
193
|
+
}
|
194
|
+
```
|
195
|
+
|
196
|
+
A HTTP error or an error response from eventflit will cause the errback to be called with an appropriate error object.
|
197
|
+
|
198
|
+
#### Without eventmachine
|
199
|
+
|
200
|
+
If the eventmachine reactor is not running, async requests will be made using threads (managed by the httpclient gem).
|
201
|
+
|
202
|
+
An `HTTPClient::Connection` object is returned immediately which can be [interrogated](http://rubydoc.info/gems/httpclient/HTTPClient/Connection) to discover the status of the request. The usual response checking and processing is not done when the request completes, and frankly this method is most useful when you're not interested in waiting for the response.
|
203
|
+
|
204
|
+
|
205
|
+
## Authenticating subscription requests
|
206
|
+
|
207
|
+
It's possible to use the gem to authenticate subscription requests to private or presence channels. The `authenticate` method is available on a channel object for this purpose and returns a JSON object that can be returned to the client that made the request. More information on this authentication scheme can be found in the docs on <http://eventflit.com>
|
208
|
+
|
209
|
+
### Private channels
|
210
|
+
|
211
|
+
``` ruby
|
212
|
+
Eventflit.authenticate('private-my_channel', params[:socket_id])
|
213
|
+
```
|
214
|
+
|
215
|
+
### Presence channels
|
216
|
+
|
217
|
+
These work in a very similar way, but require a unique identifier for the user being authenticated, and optionally some attributes that are provided to clients via presence events:
|
218
|
+
|
219
|
+
``` ruby
|
220
|
+
Eventflit.authenticate('presence-my_channel', params[:socket_id],
|
221
|
+
user_id: 'user_id',
|
222
|
+
user_info: {} # optional
|
223
|
+
)
|
224
|
+
```
|
225
|
+
|
226
|
+
## Receiving WebHooks
|
227
|
+
|
228
|
+
A WebHook object may be created to validate received WebHooks against your app credentials, and to extract events. It should be created with the `Rack::Request` object (available as `request` in Rails controllers or Sinatra handlers for example).
|
229
|
+
|
230
|
+
``` ruby
|
231
|
+
webhook = Eventflit.webhook(request)
|
232
|
+
if webhook.valid?
|
233
|
+
webhook.events.each do |event|
|
234
|
+
case event["name"]
|
235
|
+
when 'channel_occupied'
|
236
|
+
puts "Channel occupied: #{event["channel"]}"
|
237
|
+
when 'channel_vacated'
|
238
|
+
puts "Channel vacated: #{event["channel"]}"
|
239
|
+
end
|
240
|
+
end
|
241
|
+
render text: 'ok'
|
242
|
+
else
|
243
|
+
render text: 'invalid', status: 401
|
244
|
+
end
|
245
|
+
```
|
246
|
+
|
247
|
+
## Push Notifications (BETA)
|
248
|
+
|
249
|
+
Eventflit now allows sending native notifications to iOS and Android devices. Check out the [documentation](https://docs.eventflit.com/push_notifications) for information on how to set up push notifications on Android and iOS. There is no additional setup required to use it with this library. It works out of the box with the same Eventflit instance. All you need are the same eventflit credentials.
|
250
|
+
|
251
|
+
### Sending native pushes
|
252
|
+
|
253
|
+
The native notifications API is hosted at `push.eventflit.com` and only accepts https requests.
|
254
|
+
|
255
|
+
You can send pushes by using the `notify` method, either globally or on the instance. The method takes two parameters:
|
256
|
+
|
257
|
+
- `interests`: An Array of strings which represents the interests your devices are subscribed to. These are akin to channels in the DDN with less of an epehemeral nature. Note that currently, you can only publish to, at most, _ten_ interests.
|
258
|
+
- `data`: The content of the notification represented by a Hash. You must supply either the `gcm` or `apns` key. For a detailed list of the acceptable keys, take a look at the [iOS](https://docs.eventflit.com/push_notifications/ios/server) and [Android](https://docs.eventflit.com/push_notifications/android/server) docs.
|
259
|
+
|
260
|
+
Example:
|
261
|
+
|
262
|
+
```ruby
|
263
|
+
data = {
|
264
|
+
apns: {
|
265
|
+
aps: {
|
266
|
+
alert: {
|
267
|
+
body: 'tada'
|
268
|
+
}
|
269
|
+
}
|
270
|
+
}
|
271
|
+
}
|
272
|
+
|
273
|
+
eventflit.notify(["my-favourite-interest"], data)
|
274
|
+
```
|
275
|
+
|
276
|
+
### Errors
|
277
|
+
|
278
|
+
Push notification requests, once submitted to the service are executed asynchronously. To make reporting errors easier, you can supply a `webhook_url` field in the body of the request. This will be used by the service to send a webhook to the supplied URL if there are errors.
|
279
|
+
|
280
|
+
For example:
|
281
|
+
|
282
|
+
```ruby
|
283
|
+
data = {
|
284
|
+
apns: {
|
285
|
+
aps: {
|
286
|
+
alert: {
|
287
|
+
body: "hello"
|
288
|
+
}
|
289
|
+
}
|
290
|
+
},
|
291
|
+
gcm: {
|
292
|
+
notification: {
|
293
|
+
title: "hello",
|
294
|
+
icon: "icon"
|
295
|
+
}
|
296
|
+
},
|
297
|
+
webhook_url: "http://yolo.com"
|
298
|
+
}
|
299
|
+
```
|
300
|
+
|
301
|
+
**NOTE:** This is currently a BETA feature and there might be minor bugs and issues. Changes to the API will be kept to a minimum, but changes are expected. If you come across any bugs or issues, please do get in touch via [support](support@eventflit.com) or create an issue here.
|
data/Rakefile
ADDED
data/eventflit.gemspec
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
require File.expand_path('../lib/eventflit/version', __FILE__)
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "eventflit"
|
7
|
+
s.version = Eventflit::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Eventflit"]
|
10
|
+
s.email = ["support@eventflit.com"]
|
11
|
+
s.homepage = "http://github.com/eventflit/eventflit-http-ruby"
|
12
|
+
s.summary = %q{Eventflit API client}
|
13
|
+
s.description = %q{Wrapper for eventflit.com REST api}
|
14
|
+
s.license = "MIT"
|
15
|
+
|
16
|
+
s.add_dependency "multi_json", "~> 1.0"
|
17
|
+
s.add_dependency 'pusher-signature', "~> 0.1.8"
|
18
|
+
s.add_dependency "httpclient", "~> 2.7"
|
19
|
+
s.add_dependency "jruby-openssl" if defined?(JRUBY_VERSION)
|
20
|
+
|
21
|
+
s.add_development_dependency "rspec", "~> 3.0"
|
22
|
+
s.add_development_dependency "webmock"
|
23
|
+
s.add_development_dependency "em-http-request", "~> 1.1.0"
|
24
|
+
s.add_development_dependency "addressable", "=2.4.0"
|
25
|
+
s.add_development_dependency "rake", "~> 10.4.2"
|
26
|
+
s.add_development_dependency "rack", "~> 1.6.4"
|
27
|
+
s.add_development_dependency "json", "~> 1.8.3"
|
28
|
+
|
29
|
+
s.files = `git ls-files`.split("\n")
|
30
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
31
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
32
|
+
s.require_paths = ["lib"]
|
33
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'eventflit'
|
3
|
+
require 'eventmachine'
|
4
|
+
require 'em-http-request'
|
5
|
+
|
6
|
+
# To get these values:
|
7
|
+
# - Go to https://panel.eventflit.com/
|
8
|
+
# - Click on Choose App.
|
9
|
+
# - Click on one of your apps
|
10
|
+
# - Click API Access
|
11
|
+
Eventflit.app_id = 'your_app_id'
|
12
|
+
Eventflit.key = 'your_key'
|
13
|
+
Eventflit.secret = 'your_secret'
|
14
|
+
|
15
|
+
|
16
|
+
EM.run {
|
17
|
+
deferrable = Eventflit['test_channel'].trigger_async('my_event', 'hi')
|
18
|
+
|
19
|
+
deferrable.callback { # called on success
|
20
|
+
puts "Message sent successfully."
|
21
|
+
EM.stop
|
22
|
+
}
|
23
|
+
deferrable.errback { |error| # called on error
|
24
|
+
puts "Message could not be sent."
|
25
|
+
puts error
|
26
|
+
EM.stop
|
27
|
+
}
|
28
|
+
}
|