faye_service 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 96aeadda779247388258eae123b5e6143b130d58fbe45a2464ad60892758ad1c
4
+ data.tar.gz: 0ebe43ad01e9575c6174eb171444c530b580c1bc46027b5cd719681223d9b13a
5
+ SHA512:
6
+ metadata.gz: 1dbd06fd44e4b33beee3805e441243ff49a4fa150da3ad67cdb74b1fdb74c8708fefaddb03384fb891a90925da2d7cee5d4458e8a7e7bea08087fad37a44d20b
7
+ data.tar.gz: 41ed9bd800e2e8f2f5bb6bac7b25a11b0b4296c9210cd5557a5d5f114fb282d1a160847e0ab91e30764a254b3340a97238434dd561da5b0c5184746f246fc810
data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ .DS_Store
data/README.md ADDED
@@ -0,0 +1,178 @@
1
+ # Faye Service Gem
2
+
3
+ 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
+
5
+ ## Prerequisites
6
+
7
+ > 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
+
9
+ **Github Repo**: https://github.com/amerlescucodez/faye-docker
10
+
11
+ **Docker Container**: https://cloud.docker.com/repository/registry-1.docker.io/amerlescucodez/docker-faye-redis
12
+
13
+ > You can use this `docker-compose.yaml` template to get the necessary requirements running locally:
14
+
15
+ ```
16
+ version: "3.7"
17
+
18
+ services:
19
+ redis:
20
+ image: redis:alpine
21
+ ports:
22
+ - 6379:6379
23
+ restart: always
24
+ networks:
25
+ - primary
26
+
27
+ faye:
28
+ image: amerlescucodez/docker-faye-redis:1.0.0
29
+ links:
30
+ - redis
31
+ depends_on:
32
+ - redis
33
+ restart: always
34
+ ports:
35
+ - 4242:4242
36
+ env_file:
37
+ - .faye.env.development
38
+ networks:
39
+ - primary
40
+
41
+
42
+ networks:
43
+ primary:
44
+ ```
45
+
46
+ > Then you should be able to execute:
47
+
48
+ ```
49
+ docker-compose up -d
50
+ ```
51
+
52
+ > And then be able to access https://localhost:4242/faye/client.js
53
+
54
+ ## Installation into Rails
55
+
56
+ > Open your `Gemfile` and add the following dependency:
57
+
58
+ ```ruby
59
+ gem "faye_service", "~> 0.0.1", :git => "git@github.com:amerlescucodez/faye-service-gem.git"
60
+ ```
61
+
62
+ > Create the configuration file:
63
+
64
+ Command Line:
65
+
66
+ ```ruby
67
+ touch config/faye_service.yml
68
+ ```
69
+
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
+ ```
78
+
79
+ > Make sure the dependency is being loaded on application boot:
80
+
81
+ Edit `config/application.rb`:
82
+
83
+ ```ruby
84
+ require 'faye_service'
85
+ ```
86
+
87
+ ## Sending Messages
88
+
89
+ #### From A Controller
90
+
91
+ `app/controllers/sample_controller.rb`
92
+
93
+ ```ruby
94
+ class SampleController < ApplicationController
95
+ def index
96
+ FayeService.publish("/channel/name","message")
97
+ end
98
+ end
99
+ ```
100
+
101
+ #### From A Model
102
+ `app/models/sample.rb`
103
+
104
+ ```ruby
105
+ class Sample
106
+ def call_with_nothing
107
+ FayeService.publish("/channel/name","static message")
108
+ end
109
+
110
+ def call_to_user_channel_with(user_id,message)
111
+ FayeService.publish("/channel/user/#{user_id}", message) if user_id.instance_of?(String)
112
+ end
113
+
114
+ def call_with_big_message
115
+ message = {
116
+ :friends => %w{Claire Walt Ryland Sylvia Mekhi Lili},
117
+ :enemies => %w{Batman Robbin Garfield Snoopy}
118
+ }
119
+ FayeService.publish("/channel/name",message)
120
+ end
121
+ end
122
+ ```
123
+
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
+ ## Receiving Messages
138
+
139
+ ### Include Faye's main JS file
140
+
141
+ `app/views/layout/application.html.erb`
142
+
143
+ ```ruby
144
+ <!DOCTYPE html>
145
+ <html>
146
+ <head>
147
+ <title>WebSocketsExample</title>
148
+ <%= stylesheet_link_tag "application", :media => "all" %>
149
+ <%= javascript_include_tag "http://localhost:4242/faye/client.js","application" %>
150
+ <%= csrf_meta_tags %>
151
+ </head>
152
+ <body>
153
+
154
+ <%= yield %>
155
+
156
+ </body>
157
+ </html>
158
+ ```
159
+
160
+ ### Subscribe to Faye Channel within a Controller's View
161
+
162
+ `app/views/sample/index.html.erb`
163
+
164
+ ```ruby
165
+ <h2>Push Notification Example</h2>
166
+ <script type="text/javascript">
167
+ $(function() {
168
+ var faye = new Faye.Client('http://localhost:4242/faye');
169
+ faye.subscribe("/my/awesome/channel", function(data) {
170
+ alert(data);
171
+ });
172
+ });
173
+ </script>
174
+ ```
175
+
176
+ > `data` is a variable accessible with in the block of `faye.subscribe` that will either contain JSON data or a string. The best practice section in the Faye Server documentation states that you should always send `json` messages to keep things consistant. It'll make your life easier.
177
+
178
+
@@ -0,0 +1,13 @@
1
+ require File.expand_path('../lib/faye_service/version', __FILE__)
2
+
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
13
+ end
@@ -0,0 +1,40 @@
1
+ module FayeService
2
+ module Publisher
3
+ class << self
4
+ # 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 = {
25
+ :channel => channel,
26
+ :data => message,
27
+ :ext => {
28
+ :auth_token => FayeService::auth_token,
29
+ :auth_service => FayeService::auth_service
30
+ }
31
+ }
32
+ uri = URI.parse(FayeService::url)
33
+ http_result = Net::HTTP.post_form(uri, :message => message.to_json)
34
+ return http_result.body
35
+ end #/if
36
+ end #/def
37
+ end #/class
38
+ end #/module
39
+ end #/module
40
+
@@ -0,0 +1,3 @@
1
+ module FayeService
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,54 @@
1
+ require 'net/http'
2
+ require 'faye_service/publisher'
3
+ require 'faye_service/version'
4
+
5
+ module FayeService
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
36
+
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
53
+
54
+ end #/module
metadata ADDED
@@ -0,0 +1,49 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: faye_service
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Andrei Merlescu
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-08-24 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Simple interface for Faye service messaging.
14
+ email:
15
+ - andrei+github@merlescu.net
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".gitignore"
21
+ - README.md
22
+ - faye-service.gemspec
23
+ - lib/faye_service.rb
24
+ - lib/faye_service/publisher.rb
25
+ - lib/faye_service/version.rb
26
+ homepage: https://github.com/amerlescucodez/faye-service-gem
27
+ licenses:
28
+ - MIT
29
+ metadata: {}
30
+ post_install_message:
31
+ rdoc_options: []
32
+ require_paths:
33
+ - lib
34
+ required_ruby_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ required_rubygems_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ requirements: []
45
+ rubygems_version: 3.0.6
46
+ signing_key:
47
+ specification_version: 4
48
+ summary: Simple interface for Faye service messaging.
49
+ test_files: []