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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 96aeadda779247388258eae123b5e6143b130d58fbe45a2464ad60892758ad1c
4
- data.tar.gz: 0ebe43ad01e9575c6174eb171444c530b580c1bc46027b5cd719681223d9b13a
3
+ metadata.gz: 0b291fd539b70b967dda6fcc5ed3f150be77b80700e2a4f647cdd5e15fbeb9f4
4
+ data.tar.gz: 943f83644b4478a7a10a64ec690742b1d031e7b9e32bb4091fd2ff1ba99d9ea2
5
5
  SHA512:
6
- metadata.gz: 1dbd06fd44e4b33beee3805e441243ff49a4fa150da3ad67cdb74b1fdb74c8708fefaddb03384fb891a90925da2d7cee5d4458e8a7e7bea08087fad37a44d20b
7
- data.tar.gz: 41ed9bd800e2e8f2f5bb6bac7b25a11b0b4296c9210cd5557a5d5f114fb282d1a160847e0ab91e30764a254b3340a97238434dd561da5b0c5184746f246fc810
6
+ metadata.gz: fbf8d0d00ff174e1226ebbd2a887bcc03690bb71cc1e1e615c5761ed2bff58f7c71cdbb2b537e666f5b240cbda9a60e04cacba7dc3c26a366b647b1fa4bd478c
7
+ data.tar.gz: feb70dd29c6b31e4c8d1e55d72f18411d213e32c303fcc50f2f499a8fe3899a22c28a74fea6850a073c07dc3af7a8b906d5eadc87e93a954701262c1441bae29
data/.gitignore CHANGED
@@ -1 +1,2 @@
1
1
  .DS_Store
2
+ *.gem
data/CHANGES ADDED
@@ -0,0 +1,2 @@
1
+ 1.0.0 Initial Release
2
+ 1.0.1 Added logging
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://cloud.docker.com/repository/registry-1.docker.io/amerlescucodez/docker-faye-redis
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:1.0.0
48
+ image: amerlescucodez/docker-faye-redis:latest
29
49
  links:
30
50
  - redis
31
51
  depends_on:
32
52
  - redis
33
- restart: always
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
- ## Installation into Rails
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
- ```ruby
59
- gem "faye_service", "~> 0.0.1", :git => "git@github.com:amerlescucodez/faye-service-gem.git"
60
- ```
83
+ ## Installation into Rails
61
84
 
62
- > Create the configuration file:
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
- Command Line:
87
+ > Open your `Gemfile` and add the following dependency:
65
88
 
66
89
  ```ruby
67
- touch config/faye_service.yml
90
+ gem "faye_service", "~> 1.0.1"
68
91
  ```
69
92
 
70
- Edit `config/faye_service.yml` to:
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
- > Make sure the dependency is being loaded on application boot:
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
  ```
@@ -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
- Gem::Specification.new do |gem|
4
- gem.authors = ["Andrei Merlescu"]
5
- gem.email = ["andrei+github@merlescu.net"]
6
- gem.description = gem.summary = "Simple interface for Faye service messaging."
7
- gem.homepage = "https://github.com/amerlescucodez/faye-service-gem"
8
- gem.license = "MIT"
9
- gem.files = `git ls-files | grep -Ev '^(myapp|examples)'`.split("\n")
10
- gem.name = "faye_service"
11
- gem.require_paths = ["lib"]
12
- gem.version = FayeService::VERSION
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
@@ -1,54 +1,19 @@
1
- require 'net/http'
2
- require 'faye_service/publisher'
3
- require 'faye_service/version'
1
+ # frozen_string_literal: true
2
+ require 'logging'
4
3
 
5
4
  module FayeService
5
+ end
6
6
 
7
- # Shortuct for current working directory
8
- def FayeService.path
9
- Dir.pwd
10
- end #/def
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
- # Reads the YAML config file and extracts the faye_service->auth_token definition
38
- # => If undefined, return empty string
39
- def FayeService.auth_token
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
- end #/module
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
@@ -0,0 +1,10 @@
1
+ require 'logging'
2
+ module FayeService
3
+ class << self
4
+ def logger
5
+ @logger ||= Logging.logger(STDOUT)
6
+ @logger.level = :warn
7
+ return @logger
8
+ end #/def
9
+ end #/class
10
+ end #/module
@@ -1,40 +1,72 @@
1
+ require 'net/http'
2
+ require 'json'
1
3
  module FayeService
2
- module Publisher
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
- # channel => String
7
- # => Channel must be structured like:
8
- # /channel/name
9
- # /parent/child
10
- # /parent/child1/child2
11
- # /parent/child1/child2/child3
12
- # /parent/child1/child2/child3/child4
13
- # /parent/child1/child2/child3/child4/child5
14
- # /parent/child1/child2/child3/child4/child5/child6
15
- # => Channels can only go 6 children deep
16
- # => Channels must be between 3 and 32 characters in length
17
- # => Channels can only contain abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUBWXYV0123456789-
18
- #
19
- # message => String, Array, or Hash
20
- # => Will be parsed into JSON
21
- # => Should be less than 10KB in size (no programmed limit)
22
- def publish(channel,message)
23
- if channel =~ %r{^(/[\w-]{3,32}\/[\w-]{3,32}?(\/[\w-]{3,32})?(\/[\w-]{3,32})?(\/[\w-]{3,32})?(\/[\w-]{3,32})?(\/[\w-]{3,32})?)$}
24
- message = {
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::auth_token,
29
- :auth_service => FayeService::auth_service
37
+ :auth_token => FayeService.config.auth_token,
38
+ :auth_service => FayeService.config.auth_service
30
39
  }
31
40
  }
32
- uri = URI.parse(FayeService::url)
33
- http_result = Net::HTTP.post_form(uri, :message => message.to_json)
34
- return http_result.body
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 #/module
39
- end #/module
40
-
71
+ end
72
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FayeService
4
+ class Railtie < ::Rails::Railtie #:nodoc:
5
+ # Doesn't actually do anything. Just keeping this hook point, mainly for compatibility
6
+ initializer 'faye_service' do
7
+ end
8
+ end
9
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module FayeService
2
- VERSION = "1.0.0"
3
- end
4
+ VERSION = '1.0.1'
5
+ end #/module
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.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-24 00:00:00.000000000 Z
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: