faye_service 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/CHANGES +2 -0
- data/README.md +48 -37
- data/faye-service.gemspec +15 -11
- data/lib/faye_service.rb +14 -49
- data/lib/faye_service/config.rb +28 -0
- data/lib/faye_service/logger.rb +10 -0
- data/lib/faye_service/publisher.rb +61 -29
- data/lib/faye_service/railtie.rb +9 -0
- data/lib/faye_service/version.rb +4 -2
- metadata +27 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0b291fd539b70b967dda6fcc5ed3f150be77b80700e2a4f647cdd5e15fbeb9f4
|
4
|
+
data.tar.gz: 943f83644b4478a7a10a64ec690742b1d031e7b9e32bb4091fd2ff1ba99d9ea2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fbf8d0d00ff174e1226ebbd2a887bcc03690bb71cc1e1e615c5761ed2bff58f7c71cdbb2b537e666f5b240cbda9a60e04cacba7dc3c26a366b647b1fa4bd478c
|
7
|
+
data.tar.gz: feb70dd29c6b31e4c8d1e55d72f18411d213e32c303fcc50f2f499a8fe3899a22c28a74fea6850a073c07dc3af7a8b906d5eadc87e93a954701262c1441bae29
|
data/.gitignore
CHANGED
data/CHANGES
ADDED
data/README.md
CHANGED
@@ -1,14 +1,34 @@
|
|
1
1
|
# Faye Service Gem
|
2
2
|
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/faye_service.svg)](https://badge.fury.io/rb/faye_service)
|
4
|
+
|
3
5
|
This gem can extend a Ruby project such as Rails, Sinatra, Padrino, Rack, etc. It provides a simple way to communicate with any Faye server instance.
|
4
6
|
|
7
|
+
## To Use This Gem
|
8
|
+
|
9
|
+
Add it to your `Gemfile`
|
10
|
+
|
11
|
+
```
|
12
|
+
gem 'faye_service', '~> 1.0.1'
|
13
|
+
```
|
14
|
+
|
15
|
+
Or install it locally on your system:
|
16
|
+
|
17
|
+
```
|
18
|
+
gem install faye_service
|
19
|
+
```
|
20
|
+
|
5
21
|
## Prerequisites
|
6
22
|
|
7
23
|
> You need to have at least one instance of Faye running locally on your machine. If you do not have an instance of Faye to connect to, consider using this docker container and this Github repo:
|
8
24
|
|
9
25
|
**Github Repo**: https://github.com/amerlescucodez/faye-docker
|
10
26
|
|
11
|
-
**Docker Container**: https://
|
27
|
+
**Docker Container**: https://hub.docker.com/r/amerlescucodez/docker-faye-ruby
|
28
|
+
|
29
|
+
```
|
30
|
+
docker pull amerlescucodez/docker-faye-ruby
|
31
|
+
```
|
12
32
|
|
13
33
|
> You can use this `docker-compose.yaml` template to get the necessary requirements running locally:
|
14
34
|
|
@@ -25,16 +45,23 @@ services:
|
|
25
45
|
- primary
|
26
46
|
|
27
47
|
faye:
|
28
|
-
image: amerlescucodez/docker-faye-redis:
|
48
|
+
image: amerlescucodez/docker-faye-redis:latest
|
29
49
|
links:
|
30
50
|
- redis
|
31
51
|
depends_on:
|
32
52
|
- redis
|
33
|
-
restart:
|
53
|
+
restart: unless-stopped
|
34
54
|
ports:
|
35
55
|
- 4242:4242
|
56
|
+
- 4443:4443
|
57
|
+
expose:
|
58
|
+
- 4242
|
59
|
+
- 4443
|
36
60
|
env_file:
|
37
61
|
- .faye.env.development
|
62
|
+
volumes:
|
63
|
+
- ./host/path/to/ssl/certificates:/etc/ssl/certs/faye
|
64
|
+
- ./host/path/to/faye/tokens:/usr/local/faye/tokens
|
38
65
|
networks:
|
39
66
|
- primary
|
40
67
|
|
@@ -51,37 +78,29 @@ docker-compose up -d
|
|
51
78
|
|
52
79
|
> And then be able to access https://localhost:4242/faye/client.js
|
53
80
|
|
54
|
-
|
55
|
-
|
56
|
-
> Open your `Gemfile` and add the following dependency:
|
81
|
+
**Note**: If you're placing Faye inside a docker-compose file that also includes your application, you need to make sure that
|
57
82
|
|
58
|
-
|
59
|
-
gem "faye_service", "~> 0.0.1", :git => "git@github.com:amerlescucodez/faye-service-gem.git"
|
60
|
-
```
|
83
|
+
## Installation into Rails
|
61
84
|
|
62
|
-
|
85
|
+
**Important Note**: If you wish to use SSL with Faye, please change all occurences of `http://localhost:4242` to `https://localhost:4443` (or to the specified values in your instance of faye.) Please read this documentation about SSL support: https://github.com/amerlescucodez/faye-docker-ruby#generate-ssl-certificate
|
63
86
|
|
64
|
-
|
87
|
+
> Open your `Gemfile` and add the following dependency:
|
65
88
|
|
66
89
|
```ruby
|
67
|
-
|
90
|
+
gem "faye_service", "~> 1.0.1"
|
68
91
|
```
|
69
92
|
|
70
|
-
|
71
|
-
|
72
|
-
```yaml
|
73
|
-
faye_service:
|
74
|
-
url: http://localhost:4242/faye
|
75
|
-
auth_token: YOUR_TOKEN
|
76
|
-
auth_service: YOUR TOKEN
|
77
|
-
```
|
93
|
+
> Create the faye configuration file:
|
78
94
|
|
79
|
-
|
80
|
-
|
81
|
-
Edit `config/application.rb`:
|
95
|
+
Edit `config/initializers/faye.rb`:
|
82
96
|
|
83
97
|
```ruby
|
84
98
|
require 'faye_service'
|
99
|
+
FayeService.configure do |config|
|
100
|
+
config.url = "http://localhost:4242"
|
101
|
+
config.auth_token = ""
|
102
|
+
config.auth_service = ""
|
103
|
+
end
|
85
104
|
```
|
86
105
|
|
87
106
|
## Sending Messages
|
@@ -121,19 +140,6 @@ class Sample
|
|
121
140
|
end
|
122
141
|
```
|
123
142
|
|
124
|
-
`app/controllers/sample_controller.rb`
|
125
|
-
|
126
|
-
```ruby
|
127
|
-
class SampleController < ApplicationController
|
128
|
-
def index
|
129
|
-
sample = Sample.new
|
130
|
-
sample.call_with_nothing
|
131
|
-
sample.call_with_user_channel(session[:user_id], params[:message])
|
132
|
-
sample.call_with_big_message
|
133
|
-
end
|
134
|
-
end
|
135
|
-
```
|
136
|
-
|
137
143
|
## Receiving Messages
|
138
144
|
|
139
145
|
### Include Faye's main JS file
|
@@ -166,9 +172,14 @@ end
|
|
166
172
|
<script type="text/javascript">
|
167
173
|
$(function() {
|
168
174
|
var faye = new Faye.Client('http://localhost:4242/faye');
|
169
|
-
faye.subscribe("/my/awesome/channel", function(data) {
|
175
|
+
varsubscription = faye.subscribe("/my/awesome/channel", function(data) {
|
170
176
|
alert(data);
|
171
177
|
});
|
178
|
+
subscription.then(function(message){
|
179
|
+
console.log("Successfully subscribed to Faye.");
|
180
|
+
}, function(error){
|
181
|
+
console.log("Error connecting to Faye.");
|
182
|
+
});
|
172
183
|
});
|
173
184
|
</script>
|
174
185
|
```
|
data/faye-service.gemspec
CHANGED
@@ -1,13 +1,17 @@
|
|
1
|
-
require File.expand_path('../lib/faye_service/version', __FILE__)
|
1
|
+
# require File.expand_path('../lib/faye_service/version', __FILE__)
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "faye_service/version"
|
2
4
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.authors = ["Andrei Merlescu"]
|
8
|
+
s.email = ["andrei+github@merlescu.net"]
|
9
|
+
s.description = s.summary = "Simple interface for Faye service messaging."
|
10
|
+
s.homepage = "https://github.com/amerlescucodez/faye-service-gem"
|
11
|
+
s.license = "MIT"
|
12
|
+
s.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
13
|
+
s.name = "faye_service"
|
14
|
+
s.require_paths = ["lib"]
|
15
|
+
s.version = FayeService::VERSION
|
16
|
+
s.add_runtime_dependency 'logging', '~> 2.2', '>= 2.2.2'
|
13
17
|
end
|
data/lib/faye_service.rb
CHANGED
@@ -1,54 +1,19 @@
|
|
1
|
-
|
2
|
-
require '
|
3
|
-
require 'faye_service/version'
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require 'logging'
|
4
3
|
|
5
4
|
module FayeService
|
5
|
+
end
|
6
6
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
# Loads the config/faye_service.yml file and returns its YAML instance
|
13
|
-
#
|
14
|
-
# config/faye_service.yml
|
15
|
-
# =======================
|
16
|
-
# faye_service:
|
17
|
-
# url: http://0.0.0.0:8080/faye
|
18
|
-
# auth_token: APPLICATION_TOKEN
|
19
|
-
# auth_service: SERVICE_NAME
|
20
|
-
# return_response: no
|
21
|
-
def FayeService.config
|
22
|
-
# Requires inside the rails/etc application a file called config/faye_service.yml
|
23
|
-
if File.exists?(File.join(path, "config", "faye_service.yml"))
|
24
|
-
config = YAML.load_file(File.join(path, "config","faye_service.yml"))
|
25
|
-
return config
|
26
|
-
else
|
27
|
-
raise(StandardError,"No faye_service.yml file inside the config directory.")
|
28
|
-
end #/if-else
|
29
|
-
end #/def
|
30
|
-
|
31
|
-
# Reads the YAML config file and extracts the faye_service->url definition
|
32
|
-
# => If undefined, raise a Ruby standard error since this gem depends on its definition
|
33
|
-
def FayeService.url
|
34
|
-
FayeService.config["faye_service"]["url"]
|
35
|
-
end #/def
|
7
|
+
begin
|
8
|
+
require 'rails'
|
9
|
+
rescue LoadError
|
10
|
+
# do nothing
|
11
|
+
end #/begin-rescue
|
36
12
|
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
FayeService.config["faye_service"]["auth_token"]
|
41
|
-
end #/def
|
42
|
-
|
43
|
-
# Reads the YAML config file and extracts the faye_service->auth_service definition
|
44
|
-
# => If undefined, return empty string
|
45
|
-
def FayeService.auth_service
|
46
|
-
FayeService.config["faye_service"]["auth_service"]
|
47
|
-
end #/def
|
48
|
-
|
49
|
-
# alias of FayeService::Publisher.publish inside lib/faye_service/publisher.rb
|
50
|
-
def FayeService.publish(channel, message)
|
51
|
-
FayeService::Publisher.publish(channel, message)
|
52
|
-
end #/def
|
13
|
+
require 'faye_service/config'
|
14
|
+
require 'faye_service/logger'
|
15
|
+
require 'faye_service/publisher'
|
53
16
|
|
54
|
-
|
17
|
+
if defined? ::Rails::Railtie
|
18
|
+
require 'faye_service/railtie'
|
19
|
+
end #/if
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module FayeService
|
2
|
+
class << self
|
3
|
+
def configure
|
4
|
+
yield config
|
5
|
+
end #/def
|
6
|
+
|
7
|
+
def config
|
8
|
+
@_config ||= Config.new
|
9
|
+
end #/def
|
10
|
+
end #/class
|
11
|
+
|
12
|
+
class Config
|
13
|
+
attr_accessor :url, :auth_token, :auth_service, :origin, :use_ssl
|
14
|
+
attr_writer :param_name
|
15
|
+
|
16
|
+
def initialize
|
17
|
+
@url = "http://localhost:4242"
|
18
|
+
@auth_token = nil
|
19
|
+
@auth_service = nil
|
20
|
+
@origin = "http://localhost:3000"
|
21
|
+
@use_ssl = false
|
22
|
+
end #/def
|
23
|
+
|
24
|
+
def param_name
|
25
|
+
@param_name.respond_to?(:call) ? @param_name.call : @param_name
|
26
|
+
end #/def
|
27
|
+
end #/class
|
28
|
+
end #/module
|
@@ -1,40 +1,72 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
require 'json'
|
1
3
|
module FayeService
|
2
|
-
|
4
|
+
class Publisher
|
3
5
|
class << self
|
4
6
|
# Creates a new HTTP Request with the channel and message to broadcast to any potential channel subscribers
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
7
|
+
#
|
8
|
+
# channel => String
|
9
|
+
# => Channel must be structured like:
|
10
|
+
# /channel/name
|
11
|
+
# /parent/child
|
12
|
+
# /parent/child1/child2
|
13
|
+
# /parent/child1/child2/child3
|
14
|
+
# /parent/child1/child2/child3/child4
|
15
|
+
# /parent/child1/child2/child3/child4/child5
|
16
|
+
# /parent/child1/child2/child3/child4/child5/child6
|
17
|
+
# identifier-identifier-identifier
|
18
|
+
# identifier-identifier-identifier-identifier
|
19
|
+
# identifier-identifier-identifier-identifier-identifier
|
20
|
+
# identifier-identifier-identifier-identifier-identifier-identifier
|
21
|
+
# identifier-identifier-identifier-identifier-identifier-identifier-identifier
|
22
|
+
# => Channels can only go 6 children deep
|
23
|
+
# => Channels must be between 3 and 32 characters in length
|
24
|
+
# => Channels can only contain abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUBWXYV0123456789-
|
25
|
+
#
|
26
|
+
# message => String, Array, or Hash
|
27
|
+
# => Will be parsed into JSON
|
28
|
+
# => Should be less than 10KB in size (no programmed limit)
|
29
|
+
|
30
|
+
def publish(channel, message)
|
31
|
+
FayeService.logger.debug "Attempting to send #{message.length} bytes to #{channel}."
|
32
|
+
if channel =~ %r{^(/[\w-]{3,32}\/[\w-]{3,32}?(\/[\w-]{3,32})?(\/[\w-]{3,32})?(\/[\w-]{3,32})?(\/[\w-]{3,32})?(\/[\w-]{3,32})?)$} || channel =~ %r{^([\w-]{3,32}[\w-]{3,32}?([\w-]{3,32})?([\w-]{3,32})?([\w-]{3,32})?([\w-]{3,32})?([\w-]{3,32})?)$}
|
33
|
+
payload = {
|
25
34
|
:channel => channel,
|
26
35
|
:data => message,
|
27
36
|
:ext => {
|
28
|
-
:auth_token => FayeService
|
29
|
-
:auth_service => FayeService
|
37
|
+
:auth_token => FayeService.config.auth_token,
|
38
|
+
:auth_service => FayeService.config.auth_service
|
30
39
|
}
|
31
40
|
}
|
32
|
-
|
33
|
-
|
34
|
-
|
41
|
+
headers = {
|
42
|
+
"Origin": FayeService.config.origin
|
43
|
+
}
|
44
|
+
uri = URI.parse(FayeService.config.url)
|
45
|
+
response = nil
|
46
|
+
Net::HTTP.start(uri.host, uri.port, use_ssl: FayeService.config.use_ssl) do |http|
|
47
|
+
req = Net::HTTP::Post.new(uri)
|
48
|
+
req['Content-Type'] = 'application/json'
|
49
|
+
req['Referrer'] = FayeService.config.origin
|
50
|
+
req.body = payload.to_json
|
51
|
+
response = http.request(req)
|
52
|
+
end #/block
|
53
|
+
return {
|
54
|
+
success: true,
|
55
|
+
uri: uri.to_json,
|
56
|
+
channel: channel,
|
57
|
+
data: message,
|
58
|
+
info: "Sending message to #{channel} via #{uri}.",
|
59
|
+
response: response
|
60
|
+
}
|
61
|
+
else
|
62
|
+
return {
|
63
|
+
success: false,
|
64
|
+
channel: channel,
|
65
|
+
data: message,
|
66
|
+
error: "Invalid channel name provided: #{channel}"
|
67
|
+
}
|
35
68
|
end #/if
|
36
69
|
end #/def
|
37
70
|
end #/class
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
71
|
+
end
|
72
|
+
end
|
data/lib/faye_service/version.rb
CHANGED
metadata
CHANGED
@@ -1,15 +1,35 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: faye_service
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrei Merlescu
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-08-
|
12
|
-
dependencies:
|
11
|
+
date: 2019-08-31 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: logging
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '2.2'
|
20
|
+
- - ">="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 2.2.2
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - "~>"
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '2.2'
|
30
|
+
- - ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 2.2.2
|
13
33
|
description: Simple interface for Faye service messaging.
|
14
34
|
email:
|
15
35
|
- andrei+github@merlescu.net
|
@@ -18,10 +38,14 @@ extensions: []
|
|
18
38
|
extra_rdoc_files: []
|
19
39
|
files:
|
20
40
|
- ".gitignore"
|
41
|
+
- CHANGES
|
21
42
|
- README.md
|
22
43
|
- faye-service.gemspec
|
23
44
|
- lib/faye_service.rb
|
45
|
+
- lib/faye_service/config.rb
|
46
|
+
- lib/faye_service/logger.rb
|
24
47
|
- lib/faye_service/publisher.rb
|
48
|
+
- lib/faye_service/railtie.rb
|
25
49
|
- lib/faye_service/version.rb
|
26
50
|
homepage: https://github.com/amerlescucodez/faye-service-gem
|
27
51
|
licenses:
|