Skip to content
This repository was archived by the owner on Dec 12, 2021. It is now read-only.

Commit b4b9147

Browse files
committed
adding some documentation to methods
1 parent 9c69e73 commit b4b9147

File tree

7 files changed

+36
-17
lines changed

7 files changed

+36
-17
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ The Ruby `subscribe_to` helper call is still necessary with this approach to gra
8282

8383
The configuration is set separately for each environment in the generated `config/private_pub.yml` file. Here are the options.
8484

85-
* `server`: The URL to use for the Faye server.
85+
* `server`: The URL to use for the Faye server such as `http://localhost:9292/faye`.
8686
* `secret_token`: A secret hash to secure the server. Can be any string.
8787
* `signature_expiration`: The length of time in seconds before a subscription signature expires. If this is not set there is no expiration. Note: if Faye is on a separate server from the Rails app, the system clocks must be in sync for the expiration to work properly.
8888

@@ -102,7 +102,7 @@ The `subscribe_to` helper will output the following script which subscribes the
102102
</script>
103103
```
104104

105-
The signature and timestamp checked on the Faye server to ensure users are only able to access channels you subscribe them too. The signature will automatically expire after the time specified in the configuration.
105+
The signature and timestamp checked on the Faye server to ensure users are only able to access channels you subscribe them to. The signature will automatically expire after the time specified in the configuration.
106106

107107
The `publish_to` method will send a post request to the Faye server (using `Net::HTTP`) instructing it to send the given data back to the browser.
108108

lib/private_pub.rb

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,30 @@ class Error < StandardError; end
1010
class << self
1111
attr_reader :config
1212

13+
# Resets the configuration to the default (empty hash)
1314
def reset_config
1415
@config = {}
1516
end
1617

18+
# Loads the configuration from a given YAML file and environment (such as production)
1719
def load_config(filename, environment)
1820
yaml = YAML.load_file(filename)[environment.to_s]
1921
raise ArgumentError, "The #{environment} environment does not exist in #{filename}" if yaml.nil?
2022
yaml.each { |k, v| config[k.to_sym] = v }
2123
end
2224

23-
def subscription(options = {})
24-
sub = {:server => config[:server], :timestamp => (Time.now.to_f * 1000).round}.merge(options)
25-
sub[:signature] = Digest::SHA1.hexdigest([config[:secret_token], sub[:channel], sub[:timestamp]].join)
26-
sub
27-
end
28-
25+
# Publish the given data to a specific channel. This ends up sending
26+
# a Net::HTTP POST request to the Faye server.
2927
def publish_to(channel, data)
3028
publish_message(message(channel, data))
3129
end
3230

31+
# Sends the given message hash to the Faye server using Net::HTTP.
32+
def publish_message(message)
33+
Net::HTTP.post_form(URI.parse(config[:server]), :message => message.to_json)
34+
end
35+
36+
# Returns a message hash for sending to Faye
3337
def message(channel, data)
3438
message = {:channel => channel, :data => {:channel => channel}, :ext => {:private_pub_token => config[:secret_token]}}
3539
if data.kind_of? String
@@ -40,14 +44,20 @@ def message(channel, data)
4044
message
4145
end
4246

43-
def publish_message(message)
44-
Net::HTTP.post_form(URI.parse(config[:server]), :message => message.to_json)
47+
# Returns a subscription hash to pass to the PrivatePub.sign call in JavaScript.
48+
def subscription
49+
sub = {:server => config[:server], :timestamp => (Time.now.to_f * 1000).round}.merge(options)
50+
sub[:signature] = Digest::SHA1.hexdigest([config[:secret_token], sub[:channel], sub[:timestamp]].join)
51+
sub
4552
end
4653

54+
# Determine if the signature has expired given a timestamp.
4755
def signature_expired?(timestamp)
4856
timestamp < ((Time.now.to_f - config[:signature_expiration])*1000).round if config[:signature_expiration]
4957
end
5058

59+
# Returns the Faye Rack application.
60+
# Any options given are passed to the Faye::RackAdapter.
5161
def faye_app(options = {})
5262
options = {:mount => "/faye", :timeout => 45, :extensions => [FayeExtension.new]}.merge(options)
5363
Faye::RackAdapter.new(options)

lib/private_pub/engine.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22

33
module PrivatePub
44
class Engine < Rails::Engine
5+
# Loads the private_pub.yml file if it exists.
56
initializer "private_pub.config" do
67
path = Rails.root.join("config/private_pub.yml")
78
PrivatePub.load_config(path, Rails.env) if path.exist?
89
end
910

11+
# Adds the ViewHelpers into ActionView::Base
1012
initializer "private_pub.view_helpers" do
1113
ActionView::Base.send :include, ViewHelpers
1214
end

lib/private_pub/faye_extension.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
module PrivatePub
2+
# This class is an extension for the Faye::RackAdapter.
3+
# It is used inside of PrivatePub.faye_app.
24
class FayeExtension
5+
# Callback to handle incoming Faye messages. This authenticates both
6+
# subscribe and publish calls.
37
def incoming(message, callback)
48
if message["channel"] == "/meta/subscribe"
59
authenticate_subscribe(message)
@@ -11,6 +15,7 @@ def incoming(message, callback)
1115

1216
private
1317

18+
# Ensure the subscription signature is correct and that it has not expired.
1419
def authenticate_subscribe(message)
1520
subscription = PrivatePub.subscription(:channel => message["subscription"], :timestamp => message["ext"]["private_pub_timestamp"])
1621
if message["ext"]["private_pub_signature"] != subscription[:signature]
@@ -20,6 +25,7 @@ def authenticate_subscribe(message)
2025
end
2126
end
2227

28+
# Ensures the secret token is correct before publishing.
2329
def authenticate_publish(message)
2430
if PrivatePub.config[:secret_token].nil?
2531
raise Error, "No secret_token config set, ensure private_pub.yml is loaded properly."

lib/private_pub/view_helpers.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
module PrivatePub
22
module ViewHelpers
3+
# Publish the given data or block to the client by sending
4+
# a Net::HTTP POST request to the Faye server. If a block
5+
# or string is passed in, it is evaluated as JavaScript
6+
# on the client. Otherwise it will be converted to JSON
7+
# for use in a JavaScript callback.
38
def publish_to(channel, data = nil, &block)
49
PrivatePub.publish_to(channel, data || capture(&block))
510
end
611

12+
# Subscribe the client to the given channel. This generates
13+
# some JavaScript calling PrivatePub.sign with the subscription
14+
# options.
715
def subscribe_to(channel)
816
subscription = PrivatePub.subscription(:channel => channel)
917
content_tag "script", :type => "text/javascript" do

spec/fixtures/private_pub.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,3 @@ production:
66
server: http://example.com/faye
77
secret_token: PRODUCTION_SECRET_TOKEN
88
signature_expiration: 600
9-
no_signature_expiration:
10-
signature_expiration:

spec/private_pub_spec.rb

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,6 @@
2626
PrivatePub.config[:signature_expiration].should == 600
2727
end
2828

29-
it "supports a nil signature_expiration via a blank value in the configuration file" do
30-
PrivatePub.load_config("spec/fixtures/private_pub.yml", :no_signature_expiration)
31-
PrivatePub.config[:signature_expiration].should be_nil
32-
end
33-
3429
it "raises an exception if an invalid environment is passed to load_config" do
3530
lambda {
3631
PrivatePub.load_config("spec/fixtures/private_pub.yml", :test)

0 commit comments

Comments
 (0)