Module: Simplepub::InterfaceMethods
- Included in:
- Simplepub
- Defined in:
- lib/simplepub/interface_methods.rb
Instance Method Summary collapse
-
#faye_app(options = {}) ⇒ Object
Returns the Faye Rack application.
-
#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.
- #rackup_file ⇒ Object
-
#signature_expired?(timestamp) ⇒ Boolean
Determine if the signature has expired given a timestamp.
-
#subscription(options = {}) ⇒ Object
Returns a subscription hash to pass to the Simplepub.sign call in JavaScript.
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( = {}) = {:mount => "/faye", :timeout => 45, :extensions => [FayeExtension.new]}.merge() Faye::RackAdapter.new() 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 (channel, data) = {:channel => channel, :data => {:channel => channel}, :ext => {:simplepub_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.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/simplepub/interface_methods.rb', line 10 def () 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 => .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) ((channel, data)) end |
#rackup_file ⇒ Object
57 58 59 |
# File 'lib/simplepub/interface_methods.rb', line 57 def rackup_file File.('../rack_config.ru', __FILE__) end |
#signature_expired?(timestamp) ⇒ Boolean
Determine if the signature has expired given a timestamp.
46 47 48 |
# File 'lib/simplepub/interface_methods.rb', line 46 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 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( = {}) 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 |