Module: Danthes
- Defined in:
- lib/danthes.rb,
lib/danthes/engine.rb,
lib/danthes/version.rb,
lib/generators/danthes.rb,
lib/danthes/view_helpers.rb,
lib/danthes/faye_extension.rb,
lib/generators/danthes/install_generator.rb,
lib/generators/danthes/redis_install_generator.rb
Defined Under Namespace
Modules: Generators, ViewHelpers Classes: Engine, Error, FayeExtension
Constant Summary collapse
- ACCEPTED_KEYS =
List of accepted options in config file
%w(adapter server secret_token mount signature_expiration timeout)
- REDIS_ACCEPTED_KEYS =
List of accepted options in redis config file
%w(host port password database namespace socket)
- DEFAULT_OPTIONS =
Default options
{:mount => "/faye", :timeout => 60, :extensions => [FayeExtension.new]}
- VERSION =
'1.0.3'
Class Attribute Summary collapse
-
.config ⇒ Object
readonly
Returns the value of attribute config.
-
.env ⇒ Object
Returns the value of attribute env.
Class Method Summary collapse
-
.faye_app ⇒ Object
Returns the Faye Rack application.
-
.load_config(filename) ⇒ Object
Loads the configuration from a given YAML file.
-
.load_redis_config(filename) ⇒ Object
Loads the options from a given YAML file.
-
.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) ⇒ Object
Publish the given data to a specific channel.
- .server_url ⇒ Object
-
.signature_expired?(timestamp) ⇒ Boolean
Determine if the signature has expired given a timestamp.
-
.startup ⇒ Object
Resets the configuration to the default Set environment.
-
.subscription(options = {}) ⇒ Object
Returns a subscription hash to pass to the PrivatePub.sign call in JavaScript.
Class Attribute Details
.config ⇒ Object (readonly)
Returns the value of attribute config.
13 14 15 |
# File 'lib/danthes.rb', line 13 def config @config end |
.env ⇒ Object
Returns the value of attribute env.
14 15 16 |
# File 'lib/danthes.rb', line 14 def env @env end |
Class Method Details
.faye_app ⇒ Object
Returns the Faye Rack application.
103 104 105 |
# File 'lib/danthes.rb', line 103 def faye_app Faye::RackAdapter.new(config) end |
.load_config(filename) ⇒ Object
Loads the configuration from a given YAML file
37 38 39 40 41 42 |
# File 'lib/danthes.rb', line 37 def load_config(filename) yaml = ::YAML.load_file(filename)[env] raise ArgumentError, "The #{environment} environment does not exist in #{filename}" if yaml.nil? (yaml.keys - ACCEPTED_KEYS).each {|k| yaml.delete(k)} yaml.each {|k, v| config[k.to_sym] = v} end |
.load_redis_config(filename) ⇒ Object
Loads the options from a given YAML file
45 46 47 48 49 50 51 52 53 |
# File 'lib/danthes.rb', line 45 def load_redis_config(filename) require 'faye/redis' yaml = YAML.load_file(filename)[env] # default redis options = {:type => Faye::Redis, :host => 'localhost', :port => 6379} (yaml.keys - REDIS_ACCEPTED_KEYS).each {|k| yaml.delete(k)} yaml.each {|k, v| [k.to_sym] = v} config[:engine] = end |
.message(channel, data) ⇒ Object
Returns a message hash for sending to Faye
75 76 77 78 79 80 81 82 83 |
# File 'lib/danthes.rb', line 75 def (channel, data) = {:channel => channel, :data => {:channel => channel}, :ext => {:danthes_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.
62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/danthes.rb', line 62 def () raise Error, "No server specified, ensure danthes.yml was loaded properly." unless config[:server] url = URI.parse(server_url) 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.use_ssl = url.scheme == "https" http.start {|h| h.request(form)} end |
.publish_to(channel, data) ⇒ Object
Publish the given data to a specific channel. This ends up sending a Net::HTTP POST request to the Faye server.
57 58 59 |
# File 'lib/danthes.rb', line 57 def publish_to(channel, data) ((channel, data)) end |
.server_url ⇒ Object
85 86 87 |
# File 'lib/danthes.rb', line 85 def server_url [config[:server], config[:mount].gsub(/^\//,'')].join('/') end |
.signature_expired?(timestamp) ⇒ Boolean
Determine if the signature has expired given a timestamp.
98 99 100 |
# File 'lib/danthes.rb', line 98 def signature_expired?() < ((Time.now.to_f - config[:signature_expiration])*1000).round if config[:signature_expiration] end |
.startup ⇒ Object
Resets the configuration to the default Set environment
27 28 29 30 31 32 33 34 |
# File 'lib/danthes.rb', line 27 def startup @config = DEFAULT_OPTIONS.dup @env = if defined? Rails Rails.env else ENV["RAILS_ENV"] || "development" end end |
.subscription(options = {}) ⇒ Object
Returns a subscription hash to pass to the PrivatePub.sign call in JavaScript. Any options passed are merged to the hash.
91 92 93 94 95 |
# File 'lib/danthes.rb', line 91 def subscription( = {}) sub = {:server => server_url, :timestamp => (Time.now.to_f * 1000).round}.merge() sub[:signature] = Digest::SHA1.hexdigest([config[:secret_token], sub[:channel], sub[:timestamp]].join) sub end |