module Simplepub::InterfaceMethods

Public Instance Methods

faye_app(options = {}) click to toggle source

Returns the Faye Rack application. Any options given are passed to the Faye::RackAdapter.

# File lib/simplepub/interface_methods.rb, line 52
def faye_app(options = {})
  options = {:mount => "/faye", :timeout => 45, :extensions => [FayeExtension.new]}.merge(options)
  Faye::RackAdapter.new(options)
end
message(channel, data) click to toggle source

Returns a message hash for sending to Faye

# File lib/simplepub/interface_methods.rb, line 27
def message(channel, data)
  message = {:channel => channel, :data => {:channel => channel}, :ext => {:simplepub_token => config[:secret_token]}}
  if data.kind_of? String
    message[:data][:eval] = data
  else
    message[:data][:data] = data
  end
  message
end
publish_message(message) click to toggle source

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

# File lib/simplepub/interface_methods.rb, line 10
def publish_message(message)
  raise Error, "No server specified, ensure simplepub.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 => message.to_json)

  http = Net::HTTP.new(url.host, url.port)
  http.use_ssl = url.scheme == "https"

  http.ca_path = config.ca_path if config.ca_path?
  http.ca_file = config.ca_file if config.ca_file?

  http.start {|h| h.request(form)}
end
publish_to(channel, data) click to toggle source

Publish the given data to a specific channel. This ends up sending a Net::HTTP POST request to the Faye server.

# File lib/simplepub/interface_methods.rb, line 5
def publish_to(channel, data)
  publish_message(message(channel, data))
end
rackup_file() click to toggle source
# File lib/simplepub/interface_methods.rb, line 57
def rackup_file
  File.expand_path('../rack_config.ru', __FILE__)
end
signature_expired?(timestamp) click to toggle source

Determine if the signature has expired given a timestamp.

# File lib/simplepub/interface_methods.rb, line 46
def signature_expired?(timestamp)
  timestamp < ((Time.now.to_f - config[:signature_expiration])*1000).round if config[:signature_expiration]
end
subscription(options = {}) click to toggle source

Returns a subscription hash to pass to the Simplepub.sign call in JavaScript. Any options passed are merged to the hash.

# File lib/simplepub/interface_methods.rb, line 39
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