lib/private_pub.rb in private_pub-0.1.0 vs lib/private_pub.rb in private_pub-0.2.0
- old
+ new
@@ -1,56 +1,45 @@
require "digest/sha1"
require "net/http"
require "private_pub/faye_extension"
+require "private_pub/railtie" if defined? Rails
-class PrivatePub
+module PrivatePub
class Error < StandardError; end
class << self
- def server=(server)
- @config[:server] = server
- end
+ attr_reader :config
- def server
- @config[:server]
- end
-
- def signature_expiration=(signature_expiration)
- @config[:signature_expiration] = signature_expiration
- end
-
- def signature_expiration
- @config[:signature_expiration]
- end
-
- def secret_token=(secret_token)
- @config[:secret_token] = secret_token
- end
-
- def secret_token
- @config[:secret_token]
- end
-
def reset_config
@config = {
:server => "http://localhost:9292/faye",
:signature_expiration => 60 * 60, # one hour
}
end
+ 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
+
def subscription(options = {})
sub = {:timestamp => (Time.now.to_f * 1000).round}.merge(options)
- sub[:signature] = Digest::SHA1.hexdigest([secret_token, sub[:channel], sub[:timestamp]].join)
+ sub[:signature] = Digest::SHA1.hexdigest([config[:secret_token], sub[:channel], sub[:timestamp]].join)
sub
end
def publish(data)
- Net::HTTP.post_form(URI.parse(PrivatePub.server), data)
+ Net::HTTP.post_form(URI.parse(config[:server]), data)
end
def faye_extension
FayeExtension.new
+ end
+
+ def signature_expired?(timestamp)
+ timestamp < ((Time.now.to_f - config[:signature_expiration])*1000).round if config[:signature_expiration]
end
end
reset_config
end