Module: PrivatePub

Defined in:
lib/private_pub.rb,
lib/private_pub/engine.rb,
lib/private_pub/view_helpers.rb,
lib/private_pub/faye_extension.rb,
lib/generators/private_pub/install_generator.rb

Defined Under Namespace

Modules: Generators, ViewHelpers Classes: Engine, Error, FayeExtension

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configObject (readonly)

Returns the value of attribute config.



11
12
13
# File 'lib/private_pub.rb', line 11

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.



63
64
65
66
# File 'lib/private_pub.rb', line 63

def faye_app(options = {})
  options = {:mount => "/faye", :timeout => 45, :extensions => [FayeExtension.new]}.merge(options)
  Faye::RackAdapter.new(options)
end

.load_config(filename, environment) ⇒ Object

Loads the configuration from a given YAML file and environment (such as production)

Raises:

  • (ArgumentError)


19
20
21
22
23
# File 'lib/private_pub.rb', line 19

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



38
39
40
41
42
43
44
45
46
# File 'lib/private_pub.rb', line 38

def message(channel, data)
  message = {:channel => channel, :data => {:channel => channel}, :ext => {:private_pub_token => config[:secret_token]}}
  if data.kind_of? String
    message[:data][:eval] = data
  else
    message[:data][:data] = data
  end
  message
end

.publish_message(message) ⇒ Object

Sends the given message hash to the Faye server using Net::HTTP.

Raises:



32
33
34
35
# File 'lib/private_pub.rb', line 32

def publish_message(message)
  raise Error, "No server specified, ensure private_pub.yml was loaded properly." unless config[:server]
  Net::HTTP.post_form(URI.parse(config[:server]), :message => message.to_json)
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.



27
28
29
# File 'lib/private_pub.rb', line 27

def publish_to(channel, data)
  publish_message(message(channel, data))
end

.reset_configObject

Resets the configuration to the default (empty hash)



14
15
16
# File 'lib/private_pub.rb', line 14

def reset_config
  @config = {}
end

.signature_expired?(timestamp) ⇒ Boolean

Determine if the signature has expired given a timestamp.

Returns:

  • (Boolean)


57
58
59
# File 'lib/private_pub.rb', line 57

def signature_expired?(timestamp)
  timestamp < ((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 PrivatePub.sign call in JavaScript. Any options passed are merged to the hash.



50
51
52
53
54
# File 'lib/private_pub.rb', line 50

def subscription(options = {})
  sub = {:server => config[:server], :timestamp => (Time.now.to_f * 1000).round}.merge(options)
  sub[:signature] = Digest::SHA1.hexdigest([config[:secret_token], sub[:channel], sub[:timestamp]].join)
  sub
end