Module: Simplepub::InterfaceMethods

Included in:
Simplepub
Defined in:
lib/simplepub/interface_methods.rb

Instance Method Summary collapse

Instance Method Details

#faye_app(options = {}) ⇒ Object

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



52
53
54
55
# 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) ⇒ Object

Returns a message hash for sending to Faye



27
28
29
30
31
32
33
34
35
# 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) ⇒ Object

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

Raises:



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 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) ⇒ Object

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



5
6
7
# File 'lib/simplepub/interface_methods.rb', line 5

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

#rackup_fileObject



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

def rackup_file
  File.expand_path('../rack_config.ru', __FILE__)
end

#signature_expired?(timestamp) ⇒ Boolean

Determine if the signature has expired given a timestamp.

Returns:

  • (Boolean)


46
47
48
# 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 = {}) ⇒ Object

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



39
40
41
42
43
# 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