Skip to content
This repository has been archived by the owner on Dec 12, 2021. It is now read-only.

Commit

Permalink
some initial functionality on PrivatePub class
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanb committed Apr 4, 2011
1 parent af69023 commit d8bbc40
Show file tree
Hide file tree
Showing 9 changed files with 124 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .gitignore
@@ -0,0 +1,5 @@
*.swp
**/*.swp
*.gem
Gemfile.lock
.bundle
1 change: 1 addition & 0 deletions .rspec
@@ -0,0 +1 @@
--color
1 change: 1 addition & 0 deletions .rvmrc
@@ -0,0 +1 @@
rvm use 1.9.2@private_pub --create
3 changes: 3 additions & 0 deletions Gemfile
@@ -0,0 +1,3 @@
source "http://rubygems.org"

gemspec
10 changes: 10 additions & 0 deletions Rakefile
@@ -0,0 +1,10 @@
require 'rubygems'
require 'rake'
require 'rspec/core/rake_task'

desc "Run RSpec"
RSpec::Core::RakeTask.new do |t|
t.verbose = false
end

task :default => :spec
49 changes: 48 additions & 1 deletion lib/private_pub.rb
@@ -1 +1,48 @@
# TODO
require "digest/sha1"
require "net/http"

class PrivatePub
class << self
def server=(server)
@config[:server] = server
end

def server
@config[:server]
end

def key_expiration=(key_expiration)
@config[:key_expiration] = key_expiration
end

def key_expiration
@config[:key_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",
:key_expiration => 60 * 60, # one hour
}
end

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

def publish(data)
Net::HTTP.post_form(URI.parse(PrivatePub.server), data)
end
end
reset_config
end
3 changes: 3 additions & 0 deletions private_pub.gemspec
Expand Up @@ -10,6 +10,9 @@ Gem::Specification.new do |s|
s.files = Dir["{lib,spec}/**/*", "[A-Z]*", "init.rb"] - ["Gemfile.lock"]
s.require_path = "lib"

s.add_development_dependency 'rspec', '~> 2.1.0'
s.add_development_dependency 'rails', '~> 3.0.0'

s.rubyforge_project = s.name
s.required_rubygems_version = ">= 1.3.4"
end
47 changes: 47 additions & 0 deletions spec/private_pub_spec.rb
@@ -0,0 +1,47 @@
require "spec_helper"

describe PrivatePub do
before(:each) do
PrivatePub.reset_config
end

it "has secret token, server, and key expiration settings" do
PrivatePub.secret_token = "secret token"
PrivatePub.secret_token.should == "secret token"
PrivatePub.server = "http://localhost/"
PrivatePub.server.should == "http://localhost/"
PrivatePub.key_expiration = 1000
PrivatePub.key_expiration.should == 1000
end

it "defaults server to localhost:9292/faye" do
PrivatePub.server.should == "http://localhost:9292/faye"
end

it "defaults key_expiration to 1 hour" do
PrivatePub.key_expiration.should == 60 * 60
end

it "defaults subscription timestamp to current time in milliseconds" do
time = Time.now
Time.stub!(:now).and_return(time)
PrivatePub.subscription[:timestamp].should == (time.to_f * 1000).round
end

it "includes channel and custom time in subscription" do
subscription = PrivatePub.subscription(:timestamp => 123, :channel => "hello")
subscription[:timestamp].should == 123
subscription[:channel].should == "hello"
end

it "does a sha1 digest of channel, timestamp, and secret token" do
PrivatePub.secret_token = "token"
subscription = PrivatePub.subscription(:timestamp => 123, :channel => "channel")
subscription[:key].should == Digest::SHA1.hexdigest("tokenchannel123")
end

it "publishes to server using Net::HTTP" do
Net::HTTP.should_receive(:post_form).with(URI.parse(PrivatePub.server), "hello world").and_return(:result)
PrivatePub.publish("hello world").should == :result
end
end
6 changes: 6 additions & 0 deletions spec/spec_helper.rb
@@ -0,0 +1,6 @@
require 'rubygems'
require 'bundler/setup'
Bundler.require(:default)

RSpec.configure do |config|
end

0 comments on commit d8bbc40

Please sign in to comment.