Module: PrivatePubPlus
- Defined in:
- lib/private_pub_plus.rb,
lib/private_pub_plus/engine.rb,
lib/private_pub_plus/version.rb,
lib/private_pub_plus/faye_extension.rb,
lib/generators/private_pub_plus/install_generator.rb
Defined Under Namespace
Modules: Generators Classes: Engine, Error, FayeExtension
Constant Summary collapse
- VERSION =
"1.0.1"
Class Attribute Summary collapse
-
.config ⇒ Object
readonly
Returns the value of attribute config.
Class Method Summary collapse
-
.faye_app(options = {}) ⇒ Object
Returns the Faye Rack application.
-
.load_config(filename, environment) ⇒ Object
Loads the configuration from a given YAML file and environment (such as production).
-
.message(channel, data) ⇒ Object
Returns a message hash for sending to Faye.
-
.publish_message(message) ⇒ Object
Sends the given message hash to the Faye server using Net::HTTP.
-
.publish_to(channel, data, &block) ⇒ Object
Publish the given data to a specific channel.
-
.reset_config ⇒ Object
Resets the configuration to the default (empty hash).
-
.signature_expired?(timestamp) ⇒ Boolean
Determine if the signature has expired given a timestamp.
-
.subscription(options = {}) ⇒ Object
Returns a subscription hash to pass to the PrivatePubPlus.sign call in JavaScript.
Class Attribute Details
.config ⇒ Object (readonly)
Returns the value of attribute config.
13 14 15 |
# File 'lib/private_pub_plus.rb', line 13 def config @config end |
Class Method Details
.faye_app(options = {}) ⇒ Object
Returns the Faye Rack application. Any options given are passed to the Faye::RackAdapter.
79 80 81 82 |
# File 'lib/private_pub_plus.rb', line 79 def faye_app( = {}) = {:mount => "/faye", :timeout => 45, :extensions => [FayeExtension.new]}.merge() Faye::RackAdapter.new() end |
.load_config(filename, environment) ⇒ Object
Loads the configuration from a given YAML file and environment (such as production)
21 22 23 24 25 |
# File 'lib/private_pub_plus.rb', line 21 def load_config(filename, environment) yaml = YAML.load_file(filename)[environment.to_s] raise ArgumentError, "The #{environment} environment does not exist in #{filename}" if yaml.nil? yaml.each { |k, v| config[k.to_sym] = v } end |
.message(channel, data) ⇒ Object
Returns a message hash for sending to Faye
54 55 56 57 58 59 60 61 62 |
# File 'lib/private_pub_plus.rb', line 54 def (channel, data) = {:channel => channel, :data => {:channel => channel}, :ext => {:private_pub_token => config[:secret_token]}} if data.kind_of? String [:data][:eval] = data else [:data][:data] = data end end |
.publish_message(message) ⇒ Object
Sends the given message hash to the Faye server using Net::HTTP.
40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/private_pub_plus.rb', line 40 def () raise Error, "No server specified, ensure private_pub.yml was loaded properly." unless config[:server] url = URI.parse(config[:server]) form = Net::HTTP::Post.new(url.path.empty? ? '/' : url.path) form.set_form_data(:message => .to_json) http = Net::HTTP.new(url.host, url.port) http.read_timeout = config[:timeout] if config[:timeout].present? && config[:timeout].is_a?(Integer) http.use_ssl = url.scheme == "https" http.start {|h| h.request(form)} end |
.publish_to(channel, data, &block) ⇒ Object
Publish the given data to a specific channel. This ends up sending a Net::HTTP POST request to the Faye server.
29 30 31 32 33 34 35 36 37 |
# File 'lib/private_pub_plus.rb', line 29 def publish_to(channel, data, &block) m = nil if block_given? m = block.call(channel, data) else m = (channel, data) end (m) end |
.reset_config ⇒ Object
Resets the configuration to the default (empty hash)
16 17 18 |
# File 'lib/private_pub_plus.rb', line 16 def reset_config @config = {} end |
.signature_expired?(timestamp) ⇒ Boolean
Determine if the signature has expired given a timestamp.
73 74 75 |
# File 'lib/private_pub_plus.rb', line 73 def signature_expired?() < ((Time.now.to_f - config[:signature_expiration])*1000).round if config[:signature_expiration] end |
.subscription(options = {}) ⇒ Object
Returns a subscription hash to pass to the PrivatePubPlus.sign call in JavaScript. Any options passed are merged to the hash.
66 67 68 69 70 |
# File 'lib/private_pub_plus.rb', line 66 def subscription( = {}) sub = {:server => config[:server], :timestamp => (Time.now.to_f * 1000).round}.merge() sub[:signature] = Digest::SHA1.hexdigest([config[:secret_token], sub[:channel], sub[:timestamp]].join) sub end |