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

Commit d8bbc40

Browse files
committed
some initial functionality on PrivatePub class
1 parent af69023 commit d8bbc40

File tree

9 files changed

+124
-1
lines changed

9 files changed

+124
-1
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
*.swp
2+
**/*.swp
3+
*.gem
4+
Gemfile.lock
5+
.bundle

.rspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
--color

.rvmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rvm use 1.9.2@private_pub --create

Gemfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
source "http://rubygems.org"
2+
3+
gemspec

Rakefile

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
require 'rubygems'
2+
require 'rake'
3+
require 'rspec/core/rake_task'
4+
5+
desc "Run RSpec"
6+
RSpec::Core::RakeTask.new do |t|
7+
t.verbose = false
8+
end
9+
10+
task :default => :spec

lib/private_pub.rb

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,48 @@
1-
# TODO
1+
require "digest/sha1"
2+
require "net/http"
3+
4+
class PrivatePub
5+
class << self
6+
def server=(server)
7+
@config[:server] = server
8+
end
9+
10+
def server
11+
@config[:server]
12+
end
13+
14+
def key_expiration=(key_expiration)
15+
@config[:key_expiration] = key_expiration
16+
end
17+
18+
def key_expiration
19+
@config[:key_expiration]
20+
end
21+
22+
def secret_token=(secret_token)
23+
@config[:secret_token] = secret_token
24+
end
25+
26+
def secret_token
27+
@config[:secret_token]
28+
end
29+
30+
def reset_config
31+
@config = {
32+
:server => "http://localhost:9292/faye",
33+
:key_expiration => 60 * 60, # one hour
34+
}
35+
end
36+
37+
def subscription(options = {})
38+
sub = {:timestamp => (Time.now.to_f * 1000).round}.merge(options)
39+
sub[:key] = Digest::SHA1.hexdigest([secret_token, sub[:channel], sub[:timestamp]].join)
40+
sub
41+
end
42+
43+
def publish(data)
44+
Net::HTTP.post_form(URI.parse(PrivatePub.server), data)
45+
end
46+
end
47+
reset_config
48+
end

private_pub.gemspec

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ Gem::Specification.new do |s|
1010
s.files = Dir["{lib,spec}/**/*", "[A-Z]*", "init.rb"] - ["Gemfile.lock"]
1111
s.require_path = "lib"
1212

13+
s.add_development_dependency 'rspec', '~> 2.1.0'
14+
s.add_development_dependency 'rails', '~> 3.0.0'
15+
1316
s.rubyforge_project = s.name
1417
s.required_rubygems_version = ">= 1.3.4"
1518
end

spec/private_pub_spec.rb

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
require "spec_helper"
2+
3+
describe PrivatePub do
4+
before(:each) do
5+
PrivatePub.reset_config
6+
end
7+
8+
it "has secret token, server, and key expiration settings" do
9+
PrivatePub.secret_token = "secret token"
10+
PrivatePub.secret_token.should == "secret token"
11+
PrivatePub.server = "http://localhost/"
12+
PrivatePub.server.should == "http://localhost/"
13+
PrivatePub.key_expiration = 1000
14+
PrivatePub.key_expiration.should == 1000
15+
end
16+
17+
it "defaults server to localhost:9292/faye" do
18+
PrivatePub.server.should == "http://localhost:9292/faye"
19+
end
20+
21+
it "defaults key_expiration to 1 hour" do
22+
PrivatePub.key_expiration.should == 60 * 60
23+
end
24+
25+
it "defaults subscription timestamp to current time in milliseconds" do
26+
time = Time.now
27+
Time.stub!(:now).and_return(time)
28+
PrivatePub.subscription[:timestamp].should == (time.to_f * 1000).round
29+
end
30+
31+
it "includes channel and custom time in subscription" do
32+
subscription = PrivatePub.subscription(:timestamp => 123, :channel => "hello")
33+
subscription[:timestamp].should == 123
34+
subscription[:channel].should == "hello"
35+
end
36+
37+
it "does a sha1 digest of channel, timestamp, and secret token" do
38+
PrivatePub.secret_token = "token"
39+
subscription = PrivatePub.subscription(:timestamp => 123, :channel => "channel")
40+
subscription[:key].should == Digest::SHA1.hexdigest("tokenchannel123")
41+
end
42+
43+
it "publishes to server using Net::HTTP" do
44+
Net::HTTP.should_receive(:post_form).with(URI.parse(PrivatePub.server), "hello world").and_return(:result)
45+
PrivatePub.publish("hello world").should == :result
46+
end
47+
end

spec/spec_helper.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
require 'rubygems'
2+
require 'bundler/setup'
3+
Bundler.require(:default)
4+
5+
RSpec.configure do |config|
6+
end

0 commit comments

Comments
 (0)